1

I am from java background. Started coding the REST webservices from 3 weeks. I have been reading a lot about the MVVM design pattern for the Web API's. Every blog and every stackoverflow question explain that View Model is also a model that maps the front end/ the client. But, they does not talk about mapping the Domain model with the view model.

I am trying to understand mapping a domain model with the view model. Please take time to explain in detail.

View:

Sample view looks like below

{
  "userid":"nvkjnvn",
  "applicationid":"kjcnasdkjcnknc",
  "settingkey":"mykey",
  "settingvalue":"30",
  "setting label":"mylabel",
  "isactive":"yes",
  "updatedon":"2017-06-22"
}

Model Class:

public class Setting
{
    public string settingid { get; set; }
    public string settingkey { get; set; }
    public string settingValue { get; set; }
    public string isActive { get; set; }
    public string updatedOn { get; set; }
}

public class Application
{
    public string app_id { get; set; }
    public string name { get; set; }
    public List<Setting> settings { get; set; }
}

public class UserSetting
{
    public string userid { get; set; }
    public List<Application> applications { get; set; }
}

ModelView:

public class UserSettingModelView
{
   public string UserID{get; set;}
   public string ApplicationID{get; set;}
   public string SettingKey{get; set;}
   public string SettingValue{get; set;}
   public string Group{get; set;}
   public string SettingLabel{get; set;}
   public bool IsActive{get; set;}
   public Date UpdatedOn{get; set;}
}

How to map the Model View with Domain Model.

wandermonk
  • 6,856
  • 6
  • 43
  • 93

1 Answers1

0

This should be simply mapping properties from multiple domain classes to one view model class. Refer this question: Should I transform Entity (Persistent) objects to DTO objects?

AutoMapper is great tool in this case. Your property names does not match exact, so you may need to do much configurations in AutoMapper.

Other alternative is to manually map the instances.

userSettingModelView.UserID = userSetting.userid;
userSettingModelView.ApplicationID = application.app_id;
userSettingModelView.SettingKey = ...........
userSettingModelView.SettingValue = ...........
userSettingModelView.Group = ...........
userSettingModelView.SettingLabel = ...........
userSettingModelView.IsActive = ...........
userSettingModelView.UpdatedOn = ...........
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141