C# Linq-to-SQL Create a Generic Repository -
i have few repositories this
public class departmentsrepository { private personnelactionformdatacontext db = new personnelactionformdatacontext(); /// <summary> /// returns departments /// </summary> /// <returns>an iquerable of departments</returns> public iqueryable<department> getall() { return db.departments; } /// <summary> /// department id /// </summary> /// <param name="id">id of department</param> /// <returns>a null department if nothing found</returns> public department get(int id) { return db.departments.singleordefault(d => d.id == id); } /// <summary> /// department department name /// </summary> /// <param name="department">name of department</param> /// <returns>a null department if nothing found</returns> public department get(string department) { return db.departments.singleordefault(d => d.departmentname == department); } /// <summary> /// add department /// </summary> /// <param name="department"></param> public void add(department department) { db.departments.insertonsubmit(department); }
i'd have sort of generic base class save me typing, started here
public class genericrepository<t> { private personnelactionformdatacontext db = new personnelactionformdatacontext(); public iqueryable<t> getall() { return db. } }
how access collection of ts return them? possible?
thanks help.
for linq-to-sql should work:
public iqueryable<t> getresultl2s<t>() t : class { mydatacontext db = new mydatacontext(); return db.gettable<t>(); }
or, if you're using entity framework, you'd like:
public iqueryable<t> getresultef<t>() { yourentities db = new yourentities(); return db.createquery<t>(string.format("[{0}s]", typeof(t).name)); }
that assumes entity sets can pluralized tacking s on. if that's not case, check out system.data.entity.design.pluralizationservices.pluralizationservice
Comments
Post a Comment