Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////// Model classes /////////////////////////////////////////////////////////////
- public class _ModelBaseMaster
- {
- public int id{get;set;}
- public string name{get;set;}
- }
- public class Master1: _ModelBaseMaster{}
- ////////////////////////// Generic repository for DAL /////////////////////////////////////////////
- public class RepositoryMaster<T> where T : _ModelBaseMaster
- {
- // This is the generic repository for master tables which will handle CRUD & fetch
- public RepositoryMaster(MyDBContext context){...}
- ...
- }
- ///////////////////////// Service Interface and its implementation /////////////////////////////////
- public interface IMasterService<T> : IBaseService where T: _IModelBaseMaster
- {
- List<T> FetchAll();
- void Add(T mObj);
- ...
- }
- public class MasterService<T> : _ServiceBase, IMasterService<T> where T : _ModelBaseMaster
- {
- IRepositoryMaster<T> mstrRepo;
- public MasterService(IRepositoryMaster<T> mRepository)
- {
- mstrRepo = mRepository;
- }
- public List<T> FetchAll()
- {
- IQueryable<T> data = mstrRepo.FetchAll();
- // I can do - List<_ModelBaseMaster> result = data.Cast<_ModelBaseMaster>().ToList();
- ...
- return data.Cast<T>().ToList();
- }
- public void Add(T mObj)
- {
- mstrRepo.Insert(mObj); // can it be this simple - and work for ALL master tables ?
- }
- }
- /////////////////////////// usage in MVC controller's action /////////////////////////////////////
- // Now I want to be able to initialize as -
- MasterService<_ModelBaseMaster> mstrService = new RepositoryMaster<Master1>(context);
- // And finally -
- List<_ModelBaseMaster> genericList = mstrService.FetchAll();
- //- this will prevent me from lengthy switch case for each master table
Add Comment
Please, Sign In to add comment