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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:31:36+00:00 2026-05-27T22:31:36+00:00

Having a generic repository like public class Repository<T> where T: Entity<T> { /*anything else*/

  • 0

Having a generic repository like

public class Repository<T> 
    where T: Entity<T> 
{ 
    /*anything else*/ 
}

should concrete repositories per agregation root like

class ProductRepository : Repository<Product> 
{
} 

class CategoryRepository : Repository<Category> 
{ 
}

be created?

Also how do I use DI (Ninject) with generic implementation of repository.

Samples are apreciated!

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-05-27T22:31:36+00:00Added an answer on May 27, 2026 at 10:31 pm

    I see a lot of misuse of generics in the questions in SO and while it does not necessarily refer to your question (although it will help to clarify), I think once for all write a summary here.

    Generics is a typeless reuse of the behaviour (almost, since you can limit the types using restrictions). If a behaviour is shared by all types (or those limited to restrictions) you use the generics.

    However if implementation of each generic type needs to be individually implemented, then using generics does not help and is reduced to a decoration – and for me it is bad design.

    OK, let’s have an example:

    interface IConverter<T1, T2>
    {
        T1 Convert(T2 t2);
    }
    

    This looks like a good generics (converting one type to another), but I have to implement all such combinations of types as different converters. And a IConverter<Apple, Orange> does not make any sense. Hence generics here is misleading and BAD.

    Now going back to repositories. There are 100s of articles on this particular issue (lots of controversies) but this is personal my take:

    Usually it is not recommended to use generic repositories. However, in my personal experience I use generic repository to implement common stuff (implemented by a base repo) and then use individual ones if there is any additional:

    interface IRepository<T>
    {
        T GetById(int id);
        IEnumerable<T> GetAll();
        void Save(T t);
    }
    
    class BaseRepository<T> : IRepository<T>
    {
       ... // implement common methods
    }
    
    interface IProductRepository : IRepository<T>
    {
        IEnumerable<T> GetTop5BestSellingProducts();    
    }
    
    class ProductRepository : BaseRepository<T>, IProductRepository 
    {
        ... // implement important methods
    }
    

    NOTE

    Some believe repository must allow for criteria to be passed to the repository (Eric Evans DDD book) but I again do not feel I have to agree to that. I prefer a domain-significant declaration on the repository (e.g. GetTopFiveBestSellingProducts or GetTopBestSellingProducts) unless there are 100s of such like in a report writer which is only 1% of cases.


    Second question:

    I do not use Ninject but for any DI, here is how you do it

     // PSEUDOCODE !!!
     container.Register<IProductRepository , ProductRepository>(); 
    

    depending on DI framework, it can be slightly different.

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

Sidebar

Related Questions

I have two generic save methods in a repository class: public void Save<T>(T entity)
I have two Generic lists having objects of class : class Subject { public
I have a generic base repository class for LINQ-to-Entities that can do things like
Having these generic interface and class: interface TestIntf<T extends TestIntf<T>> { void test(T param);
Having the following generic class that would contain either string, int, float, long as
Let's say I have a Generic Repository class along the lines of the following:
What is the benefit of having generic constructor for a non-generic class? Java spec
I have a generic repository and would like to implement a generic GetById method.
I'm having a problem with a generic class I'm creating in Scala. I have
I am creating a generic class to hold widgets and I am having trouble

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.