thiru
thiru

Reputation: 173

Map two viewmodels which are foreign key related MVC

I am trying to map two viewmodel classes PackageViewModel and CompanyViewModel in mvc. In PackageViewModel:

 public int PackageID { get; set; }
    public int CompanyID { get; set; }
    public string PackageName { get; set; }
    public string PackageDesc { get; set; }
    public int BranchID { get; set; }
    public bool IsActive { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }

and in CompanyView

 public int CompanyID { get; set; }
    public string CompanyName{ get; set; }
    public string CompanyCode { get; set; }
    public bool IsActive { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate{ get; set; }

here companyID in PackageViewmodel was foreign key related to Companyid in company table. this was done in database...

now i am displaying the package details in which i need companyName...

I have tried like this:

var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<PackageViewModel, CompanyViewModel>();
                });
                var mapper = config.CreateMapper();
                var dest = mapper.Map<PackageViewModel, CompanyViewModel>(new PackageViewModel());

please help me.. thank you..

Upvotes: 0

Views: 417

Answers (1)

Rodrigo Couto
Rodrigo Couto

Reputation: 61

You don't give more details about your desire, but I'll try to answer.

  • The first problem is: You don't have a complete "compatibility" between models, this is required to automatic mapper (by naming conventions). Recommendation is write a custom type converter or inline functions, there are excelent examples in automapper documentation.

  • If you don't access data in your resolver, because in your exemple there are properties in one model(Company) not present in other model (Package), retrieve of database. The recommendation is: Inject your dependencies in constructor of your CustomResoler and retrieve data to resolve properties

Its a draft of possible solutions, I don't know about your data access layer.


// Current configuration upgrade
var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<PackageViewModel, CompanyViewModel>()
       .ConvertUsing<MyCustomTypeConverter>()
});
// Ps: Not necessary send a new PackageViewModel


// The custom Type Converter
public sealed class MyCustomTypeConverter : ITypeConverter<PackageViewModel, CompanyViewModel>
{
    public CompanyViewModel Convert(ResolutionContext context)
    {
          // If necessary you access your data layer
          var companyData = AnyDataAccessLayer.GetById(context.SourceValue.CompanyID);

          var newCompanyViewModel =  new CompanyViewModel
          {
           // Mix database info with you PackageViewModel info
          }
          return newCompanyViewModel;
    }
}
  • It's possible that you want keep a part automatic and resolve only outside from naming conventions, this way It's possible use of method .AfterMapper.

Using .AfterMap method


// Current configuration upgrade
var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<PackageViewModel, CompanyViewModel>()
       .AfterMap((source, destination) =>
       {
           // If necessary you access your data layer
           var companyData = AnyDataAccessLayer.GetById(source.CompanyID);

           // Resolve properties
           destination.CompanyName = companyData.CompanyName;
           destination.CompanyCode = companyData.CompanyCode;

       });
});
// Ps: Not necessary a CustomTypeConverter

Upvotes: 1

Related Questions