c# - ASP.NET Core 2.0 repository pattern with a generic base class -


i'm implementing repository pattern in asp.net core 2.0 application.

i have baserepository class follows:

public class baserepository<tentity> tentity : class {     private applicationdbcontext _context;     private dbset<tentity> _entity;      public baserepository(applicationdbcontext context)     {         _context = context;         _entity = _context.set<tentity>();     }      public ilist<tentity> getall()     {         return _entity.tolist();     }      public async task<ilist<tentity>> getallasync()     {         return await _entity.tolistasync();     } } 

then i've implemented 2 concrete repositories (i'm testing):

public class coursesubjectrepository : baserepository<coursesubject> {     public coursesubjectrepository(applicationdbcontext context) : base(context)     {     } }  public class themerepository : baserepository<theme> {     public themerepository (applicationdbcontext context) : base(context)     {     } } 

coursesubject , theme poco classes representing entities in ef core code first database.

to have repositories available in 1 place, i've implemented kind of factory (kid of because gets instances di container):

public class repositoryfactory {     public repositoryfactory(iserviceprovider serviceprovider)     {         _provider = serviceprovider;     }      private iserviceprovider _provider;      public themerepository getthemerepo()     {         return _provider.getservice<themerepository>();     }      public coursesubjectrepository getcoursesubjectrepository()     {         return _provider.getservice<coursesubjectrepository>();     } } 

and @ startup configureservice:

services.addscoped<themerepository>(); services.addscoped<coursesubjectrepository>();  services.addscoped<repositoryfactory>(); 

now have 2 questions:

1.- way of implementing , consuming repository pattern on asp.net core?

2.- have many entities in database , want know if way add repositories create class each of them , add di .addscoped. mean, have generic class representing repositories (they'll have same methods), nice add class baserepository di , "somehow" concrete repository instance using this:

controllerconstructor(baserepository<theme> themes) 

  1. your repository implementation great, add defining interface baserepository , each of children, depend on abstraction , not implementation.

  2. it not necessary define repository each table of database, 1 each entity or main table should created, dependent tables or value object can accessed browsing in properties of entity.

i think best way di repositories add them 1 one asp.net core scoped


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -