Question

I am interesting in looking at ways that I could improve the use of the below UnitOfWork class. As you can see it currently doesn't have a UnitOfWork interface so when I am using this in my MVC controllers I have to create a new object which makes my controllers dependent on this class.

I would prefer to be able to use Ninject to inject this dependency by passing an interface to the constructor of my controllers, my problem is this class currently doesn't meet the Open/Closed principle and I am interested in anyone's suggestions on how to improve that. I presume I also need some way of passing the repositories into this unit of work, but I am not entirely sure how to go about it.

Any help would be appreciated, thanks.

/// <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);
    }
}
Was it helpful?

Solution

It looks like you don't want repository instances in your UOW class. I think this post addresses your question: Multiple generic repositories in unitofwork?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top