Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC存储库模式-处理多个存储库?_C#_Asp.net Mvc_Entity Framework_Repository_Repository Pattern - Fatal编程技术网

C# MVC存储库模式-处理多个存储库?

C# MVC存储库模式-处理多个存储库?,c#,asp.net-mvc,entity-framework,repository,repository-pattern,C#,Asp.net Mvc,Entity Framework,Repository,Repository Pattern,在我的应用程序中,我尝试使用,但不使用通用存储库和工作单元来应用存储库模式 我关心的是如何处理。目前,我的应用程序使用标准的Dispose()controller方法处理DbContext: LibraryContext db = new LibraryContext(); // ... // protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); }

在我的应用程序中,我尝试使用,但不使用通用存储库和工作单元来应用存储库模式

我关心的是如何处理。目前,我的应用程序使用标准的
Dispose()
controller方法处理
DbContext

LibraryContext db = new LibraryContext();
//
...
//
protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        db.Dispose();
    }
    base.Dispose(disposing);
}
但是如何处置多个存储库呢?例如,我有三个:bookRepository、
userRepository
collectionRepository
。然后我是否应该在方法中处理它们,例如:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        bookRepository.Dispose();
        userRepository.Dispose();
        collectionRepository.Dispose();
    }
    base.Dispose(disposing);
}
这是正确的方法吗?
谢谢您的回答。

您可以创建由其他人扩展的基本存储库。在基本存储库的ctor中,您可以初始化DbContext类,并且当您想要处置时,可以调用base.dispose。应该是这样的:

public class BaseRepository<T> where T : BaseEntityWithId, new()
{
    //Represent the context of the database.
    public DbContext myContext { get; set; }

    //Represent a virtual table of the database.
    protected IDbSet<T> DbSet { get; set; }

    //Represents base constructor of the base repository.
    public BaseRepository()
    {
        this.myContext = new Context();
        this.DbSet = this.Context.Set<T>();
    }

    public IObjectContextAdapter GetObjectContextAdapter()
    {
        return (IObjectContextAdapter)this.Context;
    }

    public virtual void Dispose()
    {
        if (this.Context != null)
        {
            this.Context.Dispose();
        }
    }
}
公共类BaseRepository,其中T:BaseEntityWithId,new() { //表示数据库的上下文。 public DbContext myContext{get;set;} //表示数据库的虚拟表。 受保护的IDbSet DbSet{get;set;} //表示基本存储库的基本构造函数。 公共基本存储库() { this.myContext=新上下文(); this.DbSet=this.Context.Set(); } 公共IObjectContextAdapter GetObjectContextAdapter() { 返回(IObjectContextAdapter)this.Context; } 公共虚拟void Dispose() { 如果(this.Context!=null) { this.Context.Dispose(); } } }
如果您真的不想为一个Dispose()方法创建基本存储库,您应该逐个处理它们。

您可以创建由其他方法扩展的基本存储库。在基本存储库的ctor中,您可以初始化DbContext类,并且当您想要处置时,可以调用base.dispose。应该是这样的:

public class BaseRepository<T> where T : BaseEntityWithId, new()
{
    //Represent the context of the database.
    public DbContext myContext { get; set; }

    //Represent a virtual table of the database.
    protected IDbSet<T> DbSet { get; set; }

    //Represents base constructor of the base repository.
    public BaseRepository()
    {
        this.myContext = new Context();
        this.DbSet = this.Context.Set<T>();
    }

    public IObjectContextAdapter GetObjectContextAdapter()
    {
        return (IObjectContextAdapter)this.Context;
    }

    public virtual void Dispose()
    {
        if (this.Context != null)
        {
            this.Context.Dispose();
        }
    }
}
公共类BaseRepository,其中T:BaseEntityWithId,new() { //表示数据库的上下文。 public DbContext myContext{get;set;} //表示数据库的虚拟表。 受保护的IDbSet DbSet{get;set;} //表示基本存储库的基本构造函数。 公共基本存储库() { this.myContext=新上下文(); this.DbSet=this.Context.Set(); } 公共IObjectContextAdapter GetObjectContextAdapter() { 返回(IObjectContextAdapter)this.Context; } 公共虚拟void Dispose() { 如果(this.Context!=null) { this.Context.Dispose(); } } }
如果你真的不想为一个Dispose()方法创建基本存储库,你应该逐个处理它们。

他仍然有同样的问题,他必须调用Dispose.base三次!是的,但他问“这是正确的方法吗”,我的回答是不,不是。它可以工作,但是他跳过了重要的模式和最佳实践。他仍然有同样的问题,他必须给dispose.base打三次电话!是的,但他问“这是正确的方法吗”,我的回答是不,不是。它会起作用,但他跳过了重要的模式和最佳实践。不,我没有使用Unity。不,我没有使用Unity。