C# 改进这个工作类单元的方法,涉及到打开/关闭原理和依赖项注入/控制反转

C# 改进这个工作类单元的方法,涉及到打开/关闭原理和依赖项注入/控制反转,c#,dependency-injection,repository-pattern,unit-of-work,open-closed-principle,C#,Dependency Injection,Repository Pattern,Unit Of Work,Open Closed Principle,我很感兴趣的是如何改进下面UnitOfWork类的使用。 正如您所看到的,它目前没有UnitOfWork接口,所以当我在MVC控制器中使用它时,我必须创建一个新对象,使我的控制器依赖于这个类 我希望能够使用Ninject通过将接口传递给我的控制器的构造函数来注入这种依赖性,我的问题是这个类目前不符合开放/封闭原则,我对任何人关于如何改进的建议都感兴趣。我想我还需要某种方式将存储库传递到这个工作单元中,但我不完全确定如何进行 任何帮助都将不胜感激,谢谢 /// <summary> //

我很感兴趣的是如何改进下面UnitOfWork类的使用。 正如您所看到的,它目前没有UnitOfWork接口,所以当我在MVC控制器中使用它时,我必须创建一个新对象,使我的控制器依赖于这个类

我希望能够使用Ninject通过将接口传递给我的控制器的构造函数来注入这种依赖性,我的问题是这个类目前不符合开放/封闭原则,我对任何人关于如何改进的建议都感兴趣。我想我还需要某种方式将存储库传递到这个工作单元中,但我不完全确定如何进行

任何帮助都将不胜感激,谢谢

/// <summary>
/// The unit of work maintains the list of repositories and coordinates changes using the EF CodeFirst data context.
/// This will remove concurrency issues with multiple repositories initialising new contexts within the same HTTP request scope.
/// Instead all transactions are done through the unit of work and that is used to call SaveChanges on the DbContext.
/// </summary>
public class ERSUnitOfWork : IDisposable
{
    private ERSDbContext context = new ERSDbContext();
    private GenericRepository<Recipe> recipeRepository;
    private GenericRepository<Member> memberRepository;
    private GenericRepository<Course> courseRepository;
    private GenericRepository<Cuisine> cuisineRepository;
    private GenericRepository<Review> reviewRepository;

    public GenericRepository<Recipe> RecipeRepository
    {
        get
        {
            if (this.recipeRepository == null)
            {
                this.recipeRepository = new GenericRepository<Recipe>(context);
            }
            return recipeRepository;
        }
    }

    public GenericRepository<Member> MemberRepository
    {
        get
        {
            if (this.memberRepository == null)
            {
                this.memberRepository = new GenericRepository<Member>(context);
            }
            return memberRepository;
        }
    }

    public GenericRepository<Course> CourseRepository
    {
        get
        {
            if (this.courseRepository == null)
            {
                this.courseRepository = new GenericRepository<Course>(context);
            }
            return courseRepository;
        }
    }

    public GenericRepository<Cuisine> CuisineRepository
    {
        get
        {

            if (this.cuisineRepository == null)
            {
                this.cuisineRepository = new GenericRepository<Cuisine>(context);
            }
            return cuisineRepository;
        }
    }

    public GenericRepository<Review> ReviewRepository
    {
        get
        {
            if (this.reviewRepository == null)
            {
                this.reviewRepository = new GenericRepository<Review>(context);
            }
            return reviewRepository;
        }
    }

    public void Save()
    {
        context.SaveChanges();
    }

    private bool disposed = false;

    /// <summary>
    /// Calls dispose on the DbContext, giving a disposing argument
    /// to distinguish from the public Dispose method that is required for the IDisposable interface
    /// </summary>
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    /// <summary>
    /// Calls the custom UnitOfWork Dispose() function instead and tells the garbage collector
    /// to suppress finalisation of the object, i.e. freeing up its resources
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
//
///工作单元使用EF CodeFirst数据上下文维护存储库列表并协调更改。
///这将消除多个存储库在同一HTTP请求范围内初始化新上下文时的并发问题。
///相反,所有事务都是通过工作单元完成的,工作单元用于在DbContext上调用SaveChanges。
/// 
公共类erUnitOfWork:IDisposable
{
private-ERSDbContext=new-ERSDbContext();
私人一般储蓄;
私有一般存储成员存储库;
私人普通储蓄课程储蓄;
私人通用存储库;
私人一般保存性评论;
一般公共储蓄
{
得到
{
if(this.recipeRepository==null)
{
this.recipeRepository=新的通用存储(上下文);
}
回报的回报;
}
}
公共一般存储成员存储库
{
得到
{
if(this.memberRepository==null)
{
this.memberRepository=新的GenericRepository(上下文);
}
返回存储库;
}
}
公共普通存储课程存储
{
得到
{
if(this.courseRepository==null)
{
this.courseRepository=新的通用存储(上下文);
}
回归过程假设;
}
}
公共通用存储库
{
得到
{
if(this.cuisineRepository==null)
{
this.cuisineRepository=新的通用存储库(上下文);
}
返回菜谱库;
}
}
公共一般报告性评论
{
得到
{
if(this.reviewRepository==null)
{
this.reviewRepository=新的通用存储(上下文);
}
返回回顾性假设;
}
}
公共作废保存()
{
SaveChanges();
}
私有布尔=假;
/// 
///调用DbContext上的dispose,并给出disposing参数
///区别于IDisposable接口所需的public Dispose方法
/// 
受保护的虚拟void Dispose(bool disposing)
{
如果(!this.disposed)
{
如果(处置)
{
context.Dispose();
}
}
这是真的;
}
/// 
///改为调用自定义UnitOfWork Dispose()函数并告诉垃圾收集器
///抑制对象的最终确定,即释放其资源
/// 
公共空间处置()
{
处置(真实);
总干事(本);
}
}

看起来您不希望UOW类中有存储库实例。我想这篇文章解决了你的问题:

+1年,这可能是我最想做的,我也不喜欢这样做的想法,但我想不出其他任何东西。谢谢