如何从多个表有效地创建视图模型

时间:2014-06-03 23:27:53

标签: c# sql-server strongly-typed-dataset

我有以下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; }

    }

每个属性可以是StringCatalogModelIntCatalogModel,具体取决于每个表的标识:

Idinteger

的模型
public class IntCatalogModel
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }

Idstring

的模型
    public class StringCatalogModel
    {
        public string Id { get; set; }
        public string Description { get; set; }
    }

好的,所以魔术发生在这里......我将从我的存储过程5中选择(对于每个目录),然后我必须从那里创建EmployeeCatalogModel

我现在拥有的是:

using(DbCommand cmd1 = _db.GetStoredProcCommand("sp_employee_catalogs_get")) {

    // instantiate my view model class
    EmployeeCatalogModel model = new EmployeeCatalogModel();

    using(DataSet ds = _db.ExecuteDataSet(cmd1)) {

      // loop through each table so I can instantiate the 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 I have to know what if I need to create an item of IntCatalogModel or StringCatalogModel and added to its corresponding Collection
          // ?? any better way to do this????


        }

      }

    }
}

以更好的方式执行此操作的任何方式?

0 个答案:

没有答案