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

  • Home
  • SEARCH
  • 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 6898167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:15:41+00:00 2026-05-27T07:15:41+00:00

I am making progress but still struggling with Unit of Work in a multi

  • 0

I am making progress but still struggling with Unit of Work in a multi layer MVC app. Looking at the example here: http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application the UoW wraps all of the Repositories and provides each with a copy of the same dbcontext. Then the controller can use the Repositories with something like:

var courses = unitOfWork.CourseRepository.Get(includeProperties: "Department");

Now suppose You have a Service layer which accesses the Repositories instead. You could configure it so that it has a dependency on an IUnitOfWork implementation, then pass in an EfUnitOfWork implementation via Unity. Then when the Service completes some task it can call unitOfWork.context.SaveChanges(). But this approach hides the real dependencies for the Service; the repositories it needs. It also means that testing the Service requires you build a full UoW.

So I was thinking there must be a different approach and am wondering if one of the following or what I mentioned above (or something else!) is the correct approach:

  • Service takes in the same repository arguments and also an IUnitOfWork. The repositories are wired up with a copy of dbContext courtesy of Unity. The EfUnitOfWork is also wired with the same copy. The Service can then use the Repositories as before and once finished use EfUnitOfWork to commit.
  • Service just takes in an IUnitOfWork but sets up its required Repositories by passing to them a copy of the passed in IUnitOfWork.dbcontext

Please help!

James

  • 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-05-27T07:15:42+00:00Added an answer on May 27, 2026 at 7:15 am

    Right so having explored the issue futher I have come to the following conclusion so thought I would document here to help others or so that i can be corrected should my findings be wrong.

    DbContext is a Unit of Work. I only need to pass this Unit of Work into the implemented EFRepository classes. It does not need to go into the Service classes. So how does a Service class call context.SaveChanges() to ensure all related changes are coordinated when it does not have an instance of DbContext? Well it calls EFRepository.Save() which looks like the following:

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

    With this approach, Service classes depend only on Repositories. This will be clear and can be mocked for testing. When Unity injects the required Repository objects into a Service, it can provide each Repository with the same DbContext. In addition, only Repositories have access to the DbContext.

    All of this may be obvious but it had me stumped. Or it may be plain wrong, in which case please let me know!

    James

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

Sidebar

Related Questions

I am working on my first iPhone app and making good progress. But there
I was making some good progress on my App, but ran into a problem
I've been struggling along with sockets, making OK progress, but I keep running into
I'm still pretty new to C++, and I've been making progress in making my
I am making good progress with my first Rails app with a lot of
UPDATE: I'm making progress, but this is hard! The test text will be valid[REGEX_EMAIL|REGEX_PASSWORD|REGEX_TEST]
I've asked questions about Quartz.NET a lot today, but I'm making progress in understand
Making good progress on rails now, but hit a snag on heroku deployment. After
Working on my first FB app and making good progress. I know that non-testers
For those who have seen my other questions: I am making progress but I

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.