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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:17:41+00:00 2026-05-12T10:17:41+00:00

We are developing an ASP.NET MVC application, and are now building the repository/service classes.

  • 0

We are developing an ASP.NET MVC application, and are now building the repository/service classes. I’m wondering if there are any major advantages to creating a generic IRepository interface that all repositories implement, vs. each Repository having its own unique interface and set of methods.

For example: a generic IRepository interface might look like (taken from this answer):

public interface IRepository : IDisposable
{
    T[] GetAll<T>();
    T[] GetAll<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter);
    T GetSingle<T>(Expression<Func<T, bool>> filter, List<Expression<Func<T, object>>> subSelectors);
    void Delete<T>(T entity);
    void Add<T>(T entity);
    int SaveChanges();
    DbTransaction BeginTransaction();
}

Each Repository would implement this interface, for example:

  • CustomerRepository:IRepository
  • ProductRepository:IRepository
  • etc.

The alternate that we’ve followed in prior projects would be:

public interface IInvoiceRepository : IDisposable
{
    EntityCollection<InvoiceEntity> GetAllInvoices(int accountId);
    EntityCollection<InvoiceEntity> GetAllInvoices(DateTime theDate);
    InvoiceEntity GetSingleInvoice(int id, bool doFetchRelated);
    InvoiceEntity GetSingleInvoice(DateTime invoiceDate, int accountId); //unique
    InvoiceEntity CreateInvoice();
    InvoiceLineEntity CreateInvoiceLine();
    void SaveChanges(InvoiceEntity); //handles inserts or updates
    void DeleteInvoice(InvoiceEntity);
    void DeleteInvoiceLine(InvoiceLineEntity);
}

In the second case, the expressions (LINQ or otherwise) would be entirely contained in the Repository implementation, whoever is implementing the service just needs to know which repository function to call.

I guess I don’t see the advantage of writing all the expression syntax in the service class and passing to the repository. Wouldn’t this mean easy-to-messup LINQ code is being duplicated in many cases?

For example, in our old invoicing system, we call

InvoiceRepository.GetSingleInvoice(DateTime invoiceDate, int accountId)

from a few different services (Customer, Invoice, Account, etc). That seems much cleaner than writing the following in multiple places:

rep.GetSingle(x => x.AccountId = someId && x.InvoiceDate = someDate.Date);

The only disadvantage I see to using the specific approach is that we could end up with many permutations of Get* functions, but this still seems preferable to pushing the expression logic up into the Service classes.

What am I missing?

  • 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-12T10:17:41+00:00Added an answer on May 12, 2026 at 10:17 am

    This is an issue as old as the Repository pattern itself. The recent introduction of LINQ’s IQueryable, a uniform representation of a query, has caused a lot of discussion about this very topic.

    I prefer specific repositories myself, after having worked very hard to build a generic repository framework. No matter what clever mechanism I tried, I always ended up at the same problem: a repository is a part of the domain being modeled, and that domain is not generic. Not every entity can be deleted, not every entity can be added, not every entity has a repository. Queries vary wildly; the repository API becomes as unique as the entity itself.

    A pattern I often use is to have specific repository interfaces, but a base class for the implementations. For example, using LINQ to SQL, you could do:

    public abstract class Repository<TEntity>
    {
        private DataContext _dataContext;
    
        protected Repository(DataContext dataContext)
        {
            _dataContext = dataContext;
        }
    
        protected IQueryable<TEntity> Query
        {
            get { return _dataContext.GetTable<TEntity>(); }
        }
    
        protected void InsertOnCommit(TEntity entity)
        {
            _dataContext.GetTable<TEntity>().InsertOnCommit(entity);
        }
    
        protected void DeleteOnCommit(TEntity entity)
        {
            _dataContext.GetTable<TEntity>().DeleteOnCommit(entity);
        }
    }
    

    Replace DataContext with your unit-of-work of choice. An example implementation might be:

    public interface IUserRepository
    {
        User GetById(int id);
    
        IQueryable<User> GetLockedOutUsers();
    
        void Insert(User user);
    }
    
    public class UserRepository : Repository<User>, IUserRepository
    {
        public UserRepository(DataContext dataContext) : base(dataContext)
        {}
    
        public User GetById(int id)
        {
            return Query.Where(user => user.Id == id).SingleOrDefault();
        }
    
        public IQueryable<User> GetLockedOutUsers()
        {
            return Query.Where(user => user.IsLockedOut);
        }
    
        public void Insert(User user)
        {
            InsertOnCommit(user);
        }
    }
    

    Notice the public API of the repository does not allow users to be deleted. Also, exposing IQueryable is a whole other can of worms – there are as many opinions as belly buttons on that topic.

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

Sidebar

Related Questions

I'm developing a ASP.NET MVC application. There is little space for me to put
I am developing an asp.net mvc application, which has these enity classes: public class
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am developing an asp.net mvc application where user1 could delete data records which
I am developing an ASP .Net MVC application and on my dev machine, the
I'm developing an ASP.NET MVC application that will send the user a confirmation email.
We are developing an ASP.NET MVC Application that currently uses it's own database ApplicationData
I have been developing an asp.net mvc application where i need to make large
I am developing asp.net MVC 3 application from the standart template, with included AccountController
I'm developing an ASP.NET MVC 2 application that connects to some services to do

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.