c# - Should I be using a Generic Repository with Entity Framework 5? -


i'm using entity framework generic repository , unit of work pattern. model similar 1 described in this article

i've used generic repositories in past , enjoyed global functionality can provide. however, seems i'm running more problems every single day when comes using entity framework. these problems seem arise more when comes handling parent/children/junction relationships.

using generic repository ef starting leave bad taste in mouth, , i'm beginning think using generic repository ef wrong approach.

could please steer me in right direction?

the approach of article can become pain, because have generic repository , generic iunitofwork in ef , creating specific repository each type removes benefit of generic!

i posting here sample of how have generic repository , iunitofwork, can have nice repository!

public interface iunitofwork : idisposable {     void save();     void save(saveoptions saveoptions); }  public interface irepository<tentity> : idisposable tentity : class {     iunitofwork session { get; }     ilist<tentity> getall();     ilist<tentity> getall(expression<func<tentity, bool>> predicate);     bool add(tentity entity);     bool delete(tentity entity);     bool update(tentity entity);     bool isvalid(tentity entity); } 

and implementation like:

public class repository : component, irepository {      protected dbcontext session;      public virtual iunitofwork session     {                 {             if (session == null)                 throw new invalidoperationexception("a session iunitofwork repositório não está instanciada.");             return (session iunitofwork);         }     }      public virtual dbcontext context     {                 {             return session;         }     }      public repository(iunitofwork instance)     {         setsession(instance);     }      public ilist<tentity> getall<tentity>() tentity : class     {         return session.set<tentity>().tolist();     }      public ilist<tentity> getall<tentity>(expression<func<tentity, bool>> predicate) tentity : class     {         return session.set<tentity>().where(predicate).tolist();     }      public bool add<tentity>(tentity entity) tentity : class     {         if (!isvalid(entity))             return false;         try         {             session.set(typeof(tentity)).add(entity);             return session.entry(entity).getvalidationresult().isvalid;         }         catch (exception ex)         {             if (ex.innerexception != null)                 throw new exception(ex.innerexception.message, ex);             throw new exception(ex.message, ex);         }     }      public bool delete<tentity>(tentity entity) tentity : class     {         if (!isvalid(entity))             return false;         try         {             session.set(typeof(tentity)).remove(entity);             return session.entry(entity).getvalidationresult().isvalid;         }         catch (exception ex)         {             if (ex.innerexception != null)                 throw new exception(ex.innerexception.message, ex);             throw new exception(ex.message, ex);         }     }      public bool update<tentity>(tentity entity) tentity : class     {         if (!isvalid(entity))             return false;         try         {             session.set(typeof(tentity)).attach(entity);             session.entry(entity).state = entitystate.modified;             return session.entry(entity).getvalidationresult().isvalid;         }         catch (exception ex)         {             if (ex.innerexception != null)                 throw new exception(ex.innerexception.message, ex);             throw new exception(ex.message, ex);         }     }      public virtual bool isvalid<tentity>(tentity value) tentity : class     {         if (value == null)             throw new argumentnullexception("a entidade não pode ser nula.");         return true;     }      public void setsession(iunitofwork session)     {         setunitofwork(session);     }      protected internal void setunitofwork(iunitofwork session)     {         if (!(session dbcontext))             throw new argumentexception("a instância iunitofwork deve um dbcontext.");         setdbcontext(session dbcontext);     }      protected internal void setdbcontext(dbcontext session)     {         if (session == null)             throw new argumentnullexception("dbcontext: instance");         if (!(session iunitofwork))             throw new argumentexception("a instância dbcontext deve implementar interface iunitofwork.");         this.session = session;     }  } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -