c# - Generic Repository with UnitOfWork with IoC Custom Methods -


i have generic repository pattern unitofwork , using ioc. other base methods used repository, have custom methods. instead of implementing whole irepository methods again, have inherited genericrepository class.

here unitofwork implementation:

public interface iunitofwork<t> : idisposable t : dbcontext {     int save();     t context { get; } }  public class unitofwork<t> : iunitofwork<t> t : mycontext, new() {     private readonly t _context;      public unitofwork()     {         _context = new t();     }      public unitofwork(t context)     {         _context = context;     }      public int save()     {         return _context.savechanges();     }       public t context     {                 {             return _context;         }     }      public void dispose()     {         _context.dispose();     } } 

this repository implementation:

    public interface igenericrepository {     iqueryable<t> all<t>() t : class;     void remove<t>(int id)where t : class;     void remove<t>(t entity) t : class;     void removerange<t>(ilist<t> entities) t : class;     t find<t>(int id) t : class;     void add<t>(t entity) t : class;     void addrange<t>(ilist<t> entities) t : class;     void update<t>(t entity) t : class;     int savechanges(); }    public class genericrepository<c> : igenericrepository c : mycontext {     protected readonly c _context;      public genericrepository(iunitofwork<c> unitofwork)     {         _context = unitofwork.context;     }      public int savechanges()     {        return  _context.savechanges();     }      public iqueryable<t> all<t>() t : class     {         return _context.set<t>();     }      public void remove<t>(int id) t : class     {         t entity = _context.set<t>().find(id);         if (entity != null)         {             _context.set<t>().remove(entity);         }     }      public void remove<t>(t entity) t : class     {         if (entity != null)         {             _context.set<t>().remove(entity);         }     }      public void removerange<t>(ilist<t> entities) t : class     {         if (entities.count > 0)         {             _context.set<t>().removerange(entities);         }     }      public t find<t>(int id) t : class     {         return _context.set<t>().find(id);     }      public void add<t>(t entity) t : class     {         _context.set<t>().add(entity);     }      public void addrange<t>(ilist<t> entities) t : class     {         _context.set<t>().addrange(entities);     }      public void update<t>(t entity) t : class     {         _context.set<t>().attach(entity);         _context.entry(entity).state = system.data.entity.entitystate.modified;     } } 

here example of custom-repository:

public interface iuseraccountrepository : igenericrepository {     useraccount find(string email, string password);     bool checkduplicate(string email); }   public class useraccountrepository<c> : genericrepository<c> c : csharpassigmentcontext, iuseraccountrepository {     protected readonly c _context;      public useraccountrepository(iunitofwork<c> unitofwork)     {         _context = unitofwork.context;     }      public int savechanges()     {        return  _context.savechanges();     }      /// <summary>     /// find user email , password     /// </summary>     public useraccount find(string email, string password)     {         return _context.set<useraccount>().where(ua => ua.email == email && ua.password == password).firstordefault(null);     }      /// <summary>     /// check wether user exists or not     /// </summary>     public bool checkduplicate(string email)     {         return _context.set<useraccount>().any(ua => ua.email == email);     }      public iqueryable<t> all<t>() t : class     {         return _context.set<t>();     }      public void remove<t>(int id) t : class     {         t entity = _context.set<t>().find(id);         if (entity != null)         {             _context.set<t>().remove(entity);         }     }      public void remove<t>(t entity) t : class     {         if (entity != null)         {             _context.set<t>().remove(entity);         }     }      public void removerange<t>(ilist<t> entities) t : class     {         if (entities.count > 0)         {             _context.set<t>().removerange(entities);         }     }      public t find<t>(int id) t : class     {         return _context.set<t>().find(id);     }      public void add<t>(t entity) t : class     {         _context.set<t>().add(entity);     }      public void addrange<t>(ilist<t> entities) t : class     {         _context.set<t>().addrange(entities);     }      public void update<t>(t entity) t : class     {         _context.set<t>().attach(entity);         _context.entry(entity).state = system.data.entity.entitystate.modified;     } 

here unity ioc code:

    public static class unityconfig {     public static void registercomponents()     {         var container = new unitycontainer();          //unitofwork , genericrepository         container.registertype(typeof(iunitofwork<csharpassigmentcontext>),typeof(unitofwork<csharpassigmentcontext>), new hierarchicallifetimemanager());         container.registertype(typeof(igenericrepository), typeof(genericrepository<csharpassigmentcontext>), new hierarchicallifetimemanager());          //i keep receiving compile error here         container.registertype(typeof(iuseraccountrepository), typeof(useraccountrepository<csharpassigmentcontext>), new hierarchicallifetimemanager());          //services         container.registertype(typeof(iusersaccountsservice), typeof(usersaccountsservice), new transientlifetimemanager());          dependencyresolver.setresolver(new unitydependencyresolver(container));     } } 

as mentioned in code, keep getting compile time error following code:

container.registertype(typeof(iuseraccountrepository), typeof(useraccountrepository<csharpassigmentcontext>), new hierarchicallifetimemanager()); 

the error is:

the type mycontext can't used type parameter c in generic type or method. there no implicate reference conversion mycontext imyclassrepository.

how solve error? implementation correct custom repositories?

the foundation wrong. generic repository should have generic entity parameter. "generic" repository have methods generic parameter t, there's no guarantee whatsoever same entity used. interface should like:

public interface igenericrepository<tentity> tentity : class {     iqueryable<tentity> all();     void remove(int id);     void remove(tentity entity);     void removerange(ilist<tentity> entities);     tentity find(int id);     void add(tentity entity);     void addrange(ilist<tentity> entities);     void update(tentity entity);     int savechanges(); } 

in fact, once decide want uow/repository layer on top of dbcontext/dbset, don't see reason differently this standard example. there see similar generic repository, besides uow contains several repositories.

having done that, call "useraccountrepository", should service contains uow, can injected ioc container:

public interface iuseraccountservice // no repository! {     useraccount find(string email, string password);     bool checkduplicate(string email); } 

example implementation:

public class useraccountservice : iuseraccountservice {     private readonly iunitofwork<csharpassigmentcontext> _unitofwork;      public useraccountservice(iunitofwork<csharpassigmentcontext> unitofwork)     {         this._unitofwork = unitofwork;     } 

you see in uow/repository implementation context isn't exposed. that's 1 of purposes of abstraction layer.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -