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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:45:03+00:00 2026-05-26T21:45:03+00:00

We are starting to develop a small framework in our company, to share code

  • 0

We are starting to develop a small framework in our company, to share code between different applications. For data access we are using EF4. We have a custom DbContext class and a generic Repository:

public class RMDbContext : DbContext
{
    // ....
}

public interface IRepository 
{
    IQueryable<T> All();
    void Delete(T entity) where T : class;
    void Add(T entity) where T : class;
    void Update(T entity) where T : class;
    int SaveChanges();
    void RollbackChanges();
}

The problem here is how to implement the repository, using our custom DbContext class (RMDbContext). My co-worker thinks that the best way is to let RMDbContext implement the IRepository interface:

public class RMDbContext : DbContext, IRepository
{
    // ....
}

To be honest I don’t like this approach, because the context is tied to a specific contract (IRepository). IMO it’s better to create a repository implementation that uses the RMDbContext, something like this:

public class Repository<T> : IRepository where T : RMDbContext, new()
{
    protected readonly RMDbContext context;

    public class Repository()
    {
         context = new T();
    }

    // ....
}

What do you think about these 2 approaches? Which one would you choose, and why?

  • 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-26T21:45:04+00:00Added an answer on May 26, 2026 at 9:45 pm

    What we did at work was to implement a pattern like this:

    interface ICRUD<T> : ICreatable<T>, IRetrievable<T>, IUpdatable<T>, IDeletable<T>
    {
    }
    
    interface ICreatable<T>
    {
        T Create();
    }
    
    interface IRetrieve<T>
    {
        T Retrieve(params object[] keys);
    }
    
    interface IUpdatable<T>
    {
        void Update(T existing);
    }
    
    interface ICreatable<T>
    {
        void Delete(T existing);
    }
    

    And then we created an Entity-powered base repository:

    public abstract class BaseRepository<TModel, TEntities> where TEntities : IDbSet<TModel>
    {
        protected TEntities Entities {get; set;}
        protected DbContext Db {get; set;}
    
        public BaseRepository (DbContext db, TEntities entities)
        {
            Db = db;
            Entities = entities;
        }
    
        public virtual TModel Create() { return Entities.Create (); }
        public virtual TModel Retrieve (params object[] keys) { return Entities.Find (keys); }
        public virtual void Update (TModel existing) { Db.Entry(existing).State = Modified; }
        public virtual void Delete (TModel existing) { Db.Entry(existing).State = Removed; }
    }
    

    If you notice, the BaseRepository doesn’t actually use ICRUD, just has identical method signatures. Since we code to interfaces, this lets us use a lot of shared code without exposing functionality we don’t want with the base classes. A developer is free to implement the data store however they wish (ICRUD can talk to a webservice, for instance) with no knowledge of Entity, or they’re also free to augment behavior provided by the BaseRepository by overriding any of the provided methods and doing something differently.

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

Sidebar

Related Questions

I am starting to develop a website using CakePHP for my framework, I've literally
I am starting to develop a facebook game using ASP.NET + Silverlight Which architecture
I'm starting to develop an application using MySQL and although I've developed apps before
I've started using Vim to develop Perl scripts and am starting to find it
I'm starting to develop applications for android, but I can not draw advanced interfaces
I'm starting to develop a facebook application using Django. I'm trying to choose the
I am starting to develop Android applications and I would like to know if
I am starting to develop a complex web application using S#arp Architectur and the
I'm starting to develop a mobile version of a website that we run. Our
I'm using VS2008 to develop a project that I'm starting to test under Mono.

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.