c# - Linking entities with repositories -
i new repository pattern, have next project construction -
dal -> core -> web
i can asp.net mvc listing customers , users individually, need list of customers assigned each user (customers.primaryuser) on user model.
how relations entity framework show in models?
dal
contains entity framework model
userservice.cs
public class userservice : servicebase<iusermodel>, iuserservice { public userservice() : this(new userrepository()) { } private iuserrepository _userrepository; public userservice(iuserrepository userrepository) { _userrepository = userrepository ?? new userrepository(); } protected override type logprefix { { homecoming this.gettype(); } } public userlistviewmodel getuserlist(int pagesize, int currentpage) { seek { if ((currentpage == 0) || (pagesize == 0)) homecoming null; iqueryable<user> query = _userrepository.getqueryable(); userlistviewmodel model = new userlistviewmodel(); if (model.totalpagecount != 1) query = query.orderby(x => x.surname).skip((currentpage - 1) * pagesize).take(pagesize); model.userlist = new list<usermodel>(); automapper.mapper.createmap<user, usermodel>() .formember(dest => dest.id, opt => opt.mapfrom(src => src.id)); model.userlist = automapper.mapper.map(query.tolist(), model.userlist); homecoming model; } grab (system.exception e) { this.logerror("error getting user list", e); homecoming null; } } public usermodel getsingle(expression<func<usermodel, bool>> wherecondition) { throw new notimplementedexception(); } public void add(usermodel entity) { throw new notimplementedexception(); } public void delete(usermodel entity) { throw new notimplementedexception(); } public void update(usermodel entity) { throw new notimplementedexception(); } public ilist<usermodel> getall(expression<func<usermodel, bool>> wherecondition) { throw new notimplementedexception(); } public ilist<usermodel> getall() { throw new notimplementedexception(); } public iqueryable<usermodel> query(expression<func<usermodel, bool>> wherecondition) { throw new notimplementedexception(); } public long count(expression<func<usermodel, bool>> wherecondition) { throw new notimplementedexception(); } public long count() { throw new notimplementedexception(); } } usermodel.cs
public class usermodel : iusermodel { private icustomerservice _customerservice; public usermodel() : this(new customerservice()) { } public usermodel(icustomerservice customerservice) { _customerservice = customerservice; } public int id { get; set; } [displayname("employee number")] public string employeenumber { get; set; } public string firstname { get; set; } public string surname { get; set; } public string position { get; set; } [displayname("email address")] public string email { get; set; } public string password { get; set; } public usertype usertype { get; set; } public userstatus userstatus { get; set; } public datetime datecreated { get; set; } public int createdby { get; set; } public datetime lastupdated { get; set; } public int lastupdateby { get; set; } [displayname("full name")] public string surnamefirstname { { homecoming surname + ", " + firstname; } } } customermodel.cs
public class customermodel { public int id { get; set; } public string companyname { get; set; } public int billingaddress { get; set; } public string notes { get; set; } public int customertype { get; set; } public int refsource { get; set; } public datetime refdate { get; set; } public string refperson { get; set; } public int reftype { get; set; } public string refnotes { get; set; } public int primaryuser { get; set; } public datetime datecreated { get; set; } public int createdby { get; set; } public datetime lastupdated { get; set; } public int lastupdateby { get; set; } }
make client list kid of user:
then can pull / map list within client service.
start adding customerlist user viewmodel:
public class usermodel : iusermodel { private icustomerservice _customerservice;
public usermodel() : this(new customerservice()) { } public usermodel(icustomerservice customerservice) { _customerservice = customerservice; } public int id { get; set; } .... public ienumerable<customermodel> customerlist { get; set; } and in userservice add together client repo user repo
private iuserrepository _userrepository; private icustomerrepository _customerrepository; public userservice(iuserrepository userrepository) { _userrepository = userrepository ?? new userrepository(); _customerrepository = _customerrepository ?? new customerrepository(); } and finally, after pull in users, utilize customerrepo
model.userlist = new list<usermodel>(); automapper.mapper.createmap<user, usermodel>() .formember(dest => dest.id, opt => opt.mapfrom(src => src.id)); model.userlist = automapper.mapper.map(query.tolist(), model.userlist); foreach ( var user in model.userlist ) { var custlist = _customerrepository.getcustomersforuser(user.id).tolist(); user.customerlist = automapper.mapper.createmap<custlist, ienumerable<customermodel>>(); } homecoming model; implementing getcustomersforuser(userid)
public iqueryable<customer> getcustomersforuser(int userid) { homecoming custcntxt.customers.where(c=>c.primaryuser == userid); } c# asp.net-mvc entity-framework asp.net-mvc-4 repository-pattern
No comments:
Post a Comment