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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:13:30+00:00 2026-05-14T21:13:30+00:00

I am looking into creating an Entity Framework 4 generic repository for a new

  • 0

I am looking into creating an Entity Framework 4 generic repository for a new ASP.NET MVC project i am working on. I have been looking at various tutorials and they all seem to use the Unit of Work pattern …

From what i have been reading, EF is using this already within the ObjectContext and you are simply extending this to make your own Units of Work.

Source: http://dotnet.dzone.com/news/using-unit-work-pattern-entity?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+(.NET+Zone)

Why would one go to the effort of doing this?
Is this the preferred way of working with generic repositories?

Many thanks,
Kohan.

  • 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-14T21:13:31+00:00Added an answer on May 14, 2026 at 9:13 pm

    This is not the way I would work with generic repositories. First of all, I would share ObjectContext between ClassARepository, CalssBRepository and other repositories in current request. Using IOC container, using injection and per request behavior is recommended:

    This is how my generic repositories look like:

    public interface IRepository<T>
    {
        //Retrieves list of items in table
        IQueryable<T> List();
        IQueryable<T> List(params string[] includes);
        //Creates from detached item
        void Create(T item);
        void Delete(int id);
        T Get(int id);
        T Get(int id, params string[] includes);
        void SaveChanges();
    }
    
    public class Repository<T> : IRepository<T> where T : EntityObject
    {
        private ObjectContext _ctx;
    
        public Repository(ObjectContext ctx)
        {
            _ctx = ctx;
        }
    
    
        private static string EntitySetName
        {
            get
            {
                return String.Format(@"{0}Set", typeof(T).Name);
            }
        }
    
        private ObjectQuery<T> ObjectQueryList()
        {
            var list = _ctx.CreateQuery<T>(EntitySetName);
            return list;
        }
    
        #region IRepository<T> Members
    
        public IQueryable<T> List()
        {
            return ObjectQueryList().OrderBy(@"it.ID").AsQueryable();
        }
    
        public IQueryable<T> List(params string[] includes)
        {
            var list = ObjectQueryList();
    
            foreach(string include in includes)
            {
                list = list.Include(include);
            }
    
            return list;
        }
    
        public void Create(T item)
        {
            _ctx.AddObject(EntitySetName, item);
        }
    
        public void Delete(int id)
        {
            var item = Get(id);
            _ctx.DeleteObject(item);
        }
    
        public T Get(int id)
        {
            var list = ObjectQueryList();
            return list.Where("ID = @0", id).First();
        }
    
        public T Get(int id, params string[] includes)
        {
            var list = List(includes);
            return list.Where("ID = @0", id).First();
        }
    
        public void SaveChanges()
        {
            _ctx.SaveChanges();
        }
    
        #endregion
    
    }
    

    ObjectContext is injected through constructor. List() methods return IQueryable for further processing in business layer (service) objects. Service layer returns List or IEnumerable, so there is no deferred execution in views.

    This code was created using EF1. EF4 version can be a little different and simpler.

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

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer is this what you are after? finding the position of… May 14, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer Free on-line editors for GWT: http://bootdiskrevolution.com/gwtlab-1.0.2/Gwtlab.html - gwt-lab simplifies the… May 14, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer You are probably better off making what you want to… May 14, 2026 at 11:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.