asp.net mvc - c# generic types and repository pattern -


i working on c# mvc application using repository pattern. using unity ioc in order implement repository within mvc application.

i have created following generic type goal of minimizing code replication throughout applications. here sample:

public abstract class genericrepository<t, c> : idisposable,igenericrepository<t> t : class c : dbcontext, new()   public async void addobjectasync<t>(t entity) t : class     {         try         {             // validate that there not existing object in database.             var x = _context.entry(entity);             if (!exists(entity))             {                 _context.set<t>().add(entity); // add entry if not exist.                 _context.savechanges();             }             else             {                 updateobjectasync(entity); // update entry if did exist.             }         }         catch         {             throw;         } 

this working great when use tpc or tph strategy, not play tpt strategy. example:

public abstract class component {     [key]     public guid componentid { get; set; }      public string manufacturer { get; set; } } public class specialcomponent : component {     public string extraspecialproperty { get; set; } } 

my implementation within componentrepository:

public class componentrepository : genericrepository<component, happydbcontext>, icomponentrepository { public async void addcomponentasync<t>(t component) t : class     {         try         {              if(component != null)             {                 addobjectasync(component);             }             else             {             throw new argumentnullexception();             }          }         catch (argumentnullexception ex)         {             _logger.writeerrorlogasync(ex);             throw ex;         }     } 

the first line of code block having trouble with. in order initialize type genericrepository need insert class t. in case dbcontext c same not of concern.

how can implement generic pattern work inherited types? if small number of items less worried it, not case in situation. think there better way handle this, reaching out input.

it looks might have case of being "over-generic".

making dbcontext generic seems waste of time if it's hard coded in 2 places. i.e. genericrepository<component, happydbcontext> , where c : dbcontext, new(). strong typing field _context , injecting happydbcontext using unity strong typed repo's might make more sense.

also moving generic entity type down method level rather class might simplify things well:

public interface igenericrepository {     void addobjectasync<t>(t entity) t : class; }  public class genericrepository : igenericrepository {     private readonly dbcontext _context;      public genericrepository(dbcontext context)     {         _context = context;     }      ....      public async void addobjectasync<t>(t entity) t : class     {         // validate that there not existing object in database.         var x = _context.entry(entity);         if (!exists(entity))         {             _context.set<t>().add(entity); // add entry if not exist.             _context.savechanges();         }         else         {             updateobjectasync(entity); // update entry if did exist.         }     } }  public interface icomponentrepository {     void addcomponentasync(component component);     // or void addcomponentasync<t>(t component);     // depends if you'll reusing componentrepository      // types inherit component still have individual tables...      // little difficult tell example. }  public class componentrepository : genericrepository, icomponentrepository {     public componentrepository(dbcontext context) : base(context) { }      ...      public async void addcomponentasync(component component)     {         try         {             if (component == null) throw new argumentnullexception();             addobjectasync(component);         }         catch (argumentnullexception ex)         {             _logger.writeerrorlogasync(ex);             throw ex;         }     } } 

hopefully helps


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -