c# - Is it possible to create a generic Repository class for all my objects? -
i have following poco class repository pattern implementation. if model big enough make sense make generic 1 implementation needs done.
is possible? can please show me how?
public class position { [databasegenerated(system.componentmodel.dataannotations.databasegeneratedoption.identity)] public int positionid { get; set; } [stringlength(20, minimumlength=3)] public string name { get; set; } public int yearsexperiencerequired { get; set; } public virtual icollection<applicantposition> applicantposition { get; set; } } public interface ipositionrepository { void createnewposition(position contacttocreate); void deleteposition(int id); position getpositionbyid(int id); ienumerable<position> getallpositions(); int savechanges(); ienumerable<position> getpositionbycustomexpression(expression<func<position, bool>> predicate); } public class positionrepository : ipositionrepository { private hrcontext _db = new hrcontext(); public positionrepository(hrcontext context) { if (context == null) throw new argumentnullexception("context"); _db = context; } public position getpositionbyid(int id) { return _db.positions.firstordefault(d => d.positionid == id); } public ienumerable<position> getallposition() { return _db.positions.tolist(); } public void createnewposition(position positiontocreate) { _db.positions.add(positiontocreate); _db.savechanges(); } public int savechanges() { return _db.savechanges(); } public void deleteposition(int id) { var postodel = getpositionbyid(id); _db.positions.remove(postodel); _db.savechanges(); } /// <summary> /// lets suppose have field called name, years of experience, , department. /// how can create generic way in 1 simple method allow caller of method pass /// 1, 2 or 3 parameters. /// </summary> /// <returns></returns> public ienumerable<position> getpositionbycustomexpression(expression<func<position, bool>> predicate) { return _db.positions.where(predicate); } private bool disposed = false; protected virtual void dispose(bool disposing) { if (!this.disposed) { if (disposing) { _db.dispose(); } } this.disposed = true; } public void dispose() { dispose(true); gc.suppressfinalize(this); } }
yes, can here one:
using system; using system.collections.generic; using system.linq; using system.text; using system.data.entity; using system.data; namespace nodes.data.repository { public class baserepository<tentity>:irepository<tentity> tentity : class { internal sampledbcontext context; internal dbset<tentity> dbset; public baserepository(sampledbcontext context) { this.context = context; this.dbset = context.set<tentity>(); } public virtual tentity getbyid(object id) { return dbset.find(id); } public virtual void insert(tentity entity) { dbset.add(entity); } public virtual void delete(object id) { tentity entitytodelete = dbset.find(id); delete(entitytodelete); } public virtual void deleteall(list<tentity> entities) { foreach (var entity in entities) { this.delete(entity); } } public virtual void delete(tentity entitytodelete) { if (context.entry(entitytodelete).state == entitystate.detached) { dbset.attach(entitytodelete); } dbset.remove(entitytodelete); } public virtual void update(tentity entitytoupdate) { dbset.attach(entitytoupdate); context.entry(entitytoupdate).state = entitystate.modified; } public iqueryable<tentity> find(system.linq.expressions.expression<func<tentity, bool>> predicate) { return dbset.where(predicate); } } }
unitofwork:
public class unitofwork { private sampledbcontext context = new sampledbcontext(); private iuserrepository userrepository; #region publicproperties public iuserrepository userrepository { { if (this.userrepository == null) { userrepository repo = new userrepository(context); } return userrepository; } } #endregion public void save() { context.savechanges(); } private bool disposed = false; protected virtual void dispose(bool disposing) { if (!this.disposed) { if (disposing) { context.dispose(); } } this.disposed = true; } public void dispose() { dispose(true); gc.suppressfinalize(this); } }
link download template project wrote.
http://amrelgarhy.com/wp-content/uploads/2011/10/treenodeswebsitetemplate_v1.zip
Comments
Post a Comment