c# - how to efficiently create a view model from multiple tables -
i have following viewmodel:
public class employeecatalogmodel { public list<stringcatalogmodel> employeetype { get; set; } public list<intcatalogmodel> jobtitle { get; set; } public list<intcatalogmodel> profitcenter { get; set; } public list<intcatalogmodel> location { get; set; } public list<intcatalogmodel> holidaytemplate { get; set; } }
each property can stringcatalogmodel
or intcatalogmodel
depending on identity of each table:
model id
integer
public class intcatalogmodel { public int id { get; set; } public string description { get; set; } }
model id
string
public class stringcatalogmodel { public string id { get; set; } public string description { get; set; } }
ok, magic happens here... bring stored procedure 5 selects (for each catalog) , have create employeecatalogmodel
there.
what have is:
using(dbcommand cmd1 = _db.getstoredproccommand("sp_employee_catalogs_get")) { // instantiate view model class employeecatalogmodel model = new employeecatalogmodel(); using(dataset ds = _db.executedataset(cmd1)) { // loop through each table can instantiate corresponding collection foreach(datatable dt in ds.tables){ switch(dt.tablename){ case "employeetype": model.employeetype = new list<stringcatalogmodel>(); break; case "jobtitle": model.jobtitle = new list<intcatalogmodel>(); break; } foreach(datarow dr in dt.rows){ // here have know if need create item of intcatalogmodel or stringcatalogmodel , added corresponding collection // ?? better way this???? } } } }
any way perform in better way?
Comments
Post a Comment