c# - Entity Framework with Generic Repository and Unit Of work -


i using entity framework generic repository in 1 of application. need integrate unit of work. bit confused best way add unit of work generic repository pattern without effect on performance of application. can me this.

i have added generic repository code , other repository code below.

generic repository:

using system; using system.data.entity; using system.data.entity.validation; using system.linq; using system.threading.tasks; using entityframeworkdemo.entity; using entityframeworkdemo.models; using entityframeworkdemo.repository.unitofwork;  namespace entityframeworkdemo.repository {     public class baserepository<t> : idisposable t : basemodel     {       internal efcontext db;      public baserepository()     {         db = new efcontext();     }      /// <summary>     /// initializes new instance of <see cref="baserepository"/> class.     /// </summary>     /// <param name="context">the context.</param>     public baserepository(efcontext context)     {         db = context;     }      /// <summary>     /// gets instance.     /// </summary>     /// <returns></returns>     public iqueryable<t> getall()     {         return db.set<t>().where(t => !t.deletedon.hasvalue);     }        /// <summary>     /// gets specified identifier.     /// </summary>     /// <param name="id">the identifier.</param>     /// <returns></returns>     public t get(long? id)     {         return db.set<t>().find(id); ;     }      /// <summary>     /// gets specified identifier.     /// </summary>     /// <param name="id">the identifier.</param>     /// <returns></returns>     public task<t> getasync(long? id)     {         return db.set<t>().findasync(id);     }      /// <summary>     /// inserts specified current.     /// </summary>     /// <param name="current">the current.</param>     /// <returns></returns>     public async task<t> insert(t current)     {         db.set<t>().add(current);          try         {             await db.savechangesasync();         }         catch (dbentityvalidationexception e)         {             foreach (var eve in e.entityvalidationerrors)             {                 console.writeline("entity of type \"{0}\" in state \"{1}\" has following validation errors:",                     eve.entry.entity.gettype().name, eve.entry.state);                 foreach (var ve in eve.validationerrors)                 {                     console.writeline("- property: \"{0}\", error: \"{1}\"",                         ve.propertyname, ve.errormessage);                 }             }             throw;         }          return await db.set<t>().firstasync();     }      /// <summary>     /// inserts specified current.     /// </summary>     /// <param name="current">the current.</param>     /// <returns></returns>     public async task update(t current)     {         db.entry<t>(current).state = system.data.entity.entitystate.modified;          try         {             await db.savechangesasync();         }         catch (dbentityvalidationexception e)         {             foreach (var eve in e.entityvalidationerrors)             {                 console.writeline("entity of type \"{0}\" in state \"{1}\" has f  ollowing validation errors:",                     eve.entry.entity.gettype().name, eve.entry.state);                 foreach (var ve in eve.validationerrors)                 {                     console.writeline("- property: \"{0}\", error: \"{1}\"",                         ve.propertyname, ve.errormessage);                 }             }             throw;         }      }      /// <summary>     /// deletes specified identifier.     /// </summary>     /// <param name="id">the identifier.</param>     /// <returns></returns>     public async task delete(long? id)     {         var current = await this.getasync(id);         if (current != null)         {             current.deletedon = datetime.now;             db.entry<t>(current).state = system.data.entity.entitystate.modified;             await db.savechangesasync();         }     }      /// <summary>     /// deletes specified identifier permanently.     /// </summary>     /// <param name="id">the identifier.</param>     /// <returns></returns>     public async task deletepermanently(long? id)     {         var current = await this.getasync(id);         if (current != null)         {             db.set<t>().remove(current);             await db.savechangesasync();         }     }      /// <summary>     /// performs application-defined tasks associated freeing, releasing, or resetting unmanaged resources.     /// </summary>     public void dispose()     {         dispose(true);         gc.suppressfinalize(this);     }      /// <summary>     /// finalizes instance of <see cref="baserepository"/> class.     /// </summary>     ~baserepository()     {         dispose(false);     }      /// <summary>     /// releases unmanaged , - optionally - managed resources.     /// </summary>     /// <param name="disposing"><c>true</c> release both managed , unmanaged resources; <c>false</c> release unmanaged resources.</param>     protected virtual void dispose(bool disposing)     {         if (disposing)         {             if (db != null)             {                 db.dispose();                 db = null;             }         }     } } 

}

student repository :

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using entityframeworkdemo.models;  namespace entityframeworkdemo.repository {     public class studentrepository : baserepository<student>     {     public studentrepository()         : base()     { }      } } 

controller:

 public class homecontroller : controller {     private studentrepository studentrepo = new studentrepository();      public async task<actionresult> index()     {         var test = studentrepo.getall();          return view();     }     protected override void dispose(bool disposing)     {         if (disposing)         {             studentrepo.dispose();         }         base.dispose(disposing);     } } 

you might want know entity framework context implementation of unit of work pattern (when context.savechanges() perform unit of work). pretty sure, in 90% of cases there no need additional implementation on it, unless want abstract data layer concrete datasource/dataaccess. must say, if don't need have abstraction, don't need repository pattern @ all.

anyways, question broad. should describe intentions on why want use repository pattern in app.


Comments