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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:13:08+00:00 2026-06-04T21:13:08+00:00

I am learning EF and have seen many examples, and during my learning I

  • 0

I am learning EF and have seen many examples, and during my learning I came to know about using repository and unit of work patterns. I got why to use repository but I do not have understanding of unit of work really is.

Having no understanding is making DAL understanding difficult. Kindly guide me.

Thanks

  • 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-04T21:13:09+00:00Added an answer on June 4, 2026 at 9:13 pm

    The DataContext or ObjectContext is the Unit of Work.

    So, your DAL will save, delete and retrieve objects and your DataContext/ObjectContext will keep track of your objects, manage transactions and apply changes.

    This is an example just to illustrate the idea of the solution.

    using(var context = new ObjectContext()) { // Unit of Work
        var repo = new ProductRepository(context);
        var product = repo.GetXXXXXXX(...);
        ...
    
        // Do whatever tracking you want to do with the object context. For instance:
        // if( error == false) { 
        //     context.DetectChanges();
        //     context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
        // }
    }
    

    And your repository will look like:

    public abstract class Repository {
    
        public Respository(ObjectContext context){
            CurrentContext = context;
        }
    
        protected ObjectContext CurrentContext { get; private set; } 
    }
    
    public class ProductRespository : Repository {
        public ProductRespository(ObjectContext context) : base(context){
        }
    
        public Product GetXXXXXX(...){
            return CurrentContext... ; //Do something with the context
        }
    }    
    

    Another way is to put the unit of work (Object context) globally:

    You need to define what will be your unit of work scope. For this example, it will be a web request. In a real world implementation, I’d use dependency injection for that.

    public static class ContextProvider {
    
        public static ObjectContext CurrentContext {
            get { return HttpContext.Items["CurrentObjectContext"];
        }
    
        public static void OpenNew(){
            var context = new ObjectContext();
            HttpContext.Items["CurrentObjectContext"] = context; 
        }
    
        public static void CloseCurrent(){
            var context = CurrentContext;
            HttpContext.Items["CurrentObjectContext"] = null;
            // Do whatever tracking you want to do with the object context. For instance:
            // if( error == false) { 
            //     context.DetectChanges();
            //     context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            // }
            context.Dispose();
        }
    }
    

    In this example, ObjectContext is the unit of work and it will live in the current request. In your global asax you could add:

    protected void Application_BeginRequest(object sender, EventArgs e){
        ContextProvider.OpenNew();
    }
    
    protected void Application_EndRequest(object sender, EventArgs e){
        ContextProvider.CloseCurrent();
    }
    

    In your Repositories, you just call ContextProvider.CurrentContext

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

Sidebar

Related Questions

I'm learning javascript for a new project. I have seen many tutorials about javascript
I am learning Javascript, I have been using PHP for about 10 years so
hello I'm learning php and i have seen many empty Function in many other
I have seen examples of Objective-C code apparently doing the same/similar checks using different
I am very new to C# but I am fast learning. I have seen
Below my code for installing vcider. I am learning chef but have not seen
I have been learning about collision in phisics programming , and i have one
I have started learning concurrency and threads in Java. I know the basics of
I'm fairly new to programming and from learning I have seen different ways of
I am beginner to freeswitch.While learning this I have seen the terms 'leg A'

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.