what are the benefits of using base Interface in c# -
i m working application in base inteface has been created below
public interface ibaserepository : idisposable { bool isunitofwork { get; set; } void savechanges(); }
then other interfaces extend interface as
public interface icourserepository : ibaserepository { course getcoursebyid(int id); list<course> getcourses(); coursemodule getcoursemodulebyid(int id); }
just wondering advantage of approach
the base repository allows specify behavior want repository contracts have without repeating in every imyentityrepository
create.
even more fun though implementing base repository , specifying generic code generic operations:
public abstract class baserepository<tentity> : irepository<tentity> tentity : class { private dbcontext _context; public repository(dbcontext context) { if (context == null) { throw new argumentnullexception("context"); } _context = context; } protected dbcontext dbcontext { { return _context; } } public void create(tentity entity) { if (entity == null) { throw new argumentnullexception("entity"); } dbcontext.set<tentity>().add(entity); dbcontext.savechanges(); } public tentity getbyid(int id) { return dbcontext.set<tentity>().find(id); } public void delete(tentity entity) { if (entity == null) { throw new argumentnullexception("entity"); } dbcontext.set<tentity>().attach(entity); dbcontext.set<tentity>().remove(entity); dbcontext.savechanges(); } public void update(tentity entity) { if (entity == null) { throw new argumentnullexception("entity"); } dbcontext.set<tentity>().attach(entity); dbcontext.entry(entity).state = entitystate.modified; dbcontext.savechanges(); }
etc., have myentityreporitory extend baserepository
Comments
Post a Comment