Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:27:26+00:00 2026-06-11T09:27:26+00:00

I am interesting in looking at ways that I could improve the use of

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:27:27+00:00Added an answer on June 11, 2026 at 9:27 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently looking at different ways to use various web/internet technologies in order to
As a build engineer, I'm constantly looking for new and interesting ways to improve
I have an interesting problem and I was looking for a solution; Hoping that
I'm looking at using the jQuery MetaData Plugin. Looks very interesting but... <li class=someclass
Interesting one here. I have an ASP.NET 1.1 project that contains a web service
An interesting question arose today. Let's say I have a .NET object that implements
I'm dealing with an interesting issue. I have biometric system that uses John Daugman's
Have an interesting problem and am looking for the right solution. We have around
I've been looking for ways to improve the overall attractiveness of my iPhone applications.
I have an interesting problem which I've been looking into and would appreciate some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.