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 880489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:06:25+00:00 2026-05-15T12:06:25+00:00

I have started to look at the Entity Framework for a project I am

  • 0

I have started to look at the Entity Framework for a project I am doing and going down the road of using a BLL against it via the the repository pattern. As I understand it for each entity I should create a repository for it so I would have

public class UserRepository : IRepository<User>
{ ... }

and

public class AccountRepository : IRepository<Account>
{ ... }

With the examples I have seen it is common practice to create the entities context within a using statement and perform gets, updates and saves etc within.

using(var ctx = new AppEntities()
{
    //do whatever
    ctx.SaveChanges();
}

For simple access to the repository this would be ok, but what if I wanted to compose up an interaction between 2 (or more) repositories within by BLL…

public void SaveSomethingMoreComplex()
{
    //BLL here stuff like validation etc

    _userRepository.GetSomeData();
    _accountRepository.SaveSomeData(account);
    _userRepository.SaveSomeMore(user);

    // Probably should have one final save that affects both repositories???
    // Should be in a transaction scope also?
}

Would it be best to use the same AppEntities instance for both repositories?

Also in this example a final save probably should be at the end of the block rather than having 2 like in my example and part of a transaction?

If I do use the same instance then is it safe to inject that in the constructor of the repositories and have it live for the lifetime of the app or is there some reason the examples I have seen tend to create and dispose within a single method call?

Thanks for any help provided.

  • 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-15T12:06:25+00:00Added an answer on May 15, 2026 at 12:06 pm

    This is actually not that unusual an issue when dealing with the Repository pattern, and what it boils down to is providing a way to explicitly manage the lifecycle of your unit of work (which in the case of entity framework is your context).

    You didn’t specify if your doing web or windows development, but in the context of web development it is not uncommon to set the lifecycle of your unit of work to a single request. So when your request starts, you create your context, and then when it is over you can call SaveChanges (or whatever it is for entity framework) and that would apply the changes to all of the entities you were messing with during the course of the request.

    In a windows/service context you’ll probably want to set up some sort of explicit life cycle management for your unit or work, so you can define the scope of the UoW based on what you are doing. I tend to like the Conversation metaphor for wrapping UoW operations, which means I can use something like this:

    using(Conversation.Start())
    {
        // mess with the entities
    } // Dispose on the object returned from Start will 
      // Save Changes and close the session
    

    Of course that is glossing over some exception management stuff in there, which you would want to have so you can roll-back changes in the case of a failure.

    As far as implementation goes, it kinda depends on your infrastructure. I typically use an IoC container, so I’ll have the call to Conversation.Start() create my Unit Of Work for me, and set up the IoC to return that specific instance, so when I create my Repositories they get the current UoW. You could also make some factory methods on Conversation, so that you could get your Repository instances from the conversation. Kinda depends on the API you want to have available.

    Hope this helps.

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

Sidebar

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.