How to use automapper
Install it using Nuget
Classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UsingAutoMapper.Models
{
public class Scope
{
public int ScopeId { get; set; }
public string ScopeName { get; set; }
public string ScopeDisplay1 { get; set; }
public
List<TScopeType> ScopeTypes1 { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UsingAutoMapper.Models
{
public class ScopeViewModel
{
public int ScopeId { get; set; }
public string ScopeName { get; set; }
/// <summary>
/// Id + Name
/// </summary>
public string ScopeDisplay { get; set; }
public
List<TScopeTypeViewModel> ScopeTypes { get; set; }
}
}
Whole code
private void UseAutoMapper()
{
ScopeViewModel model = new ScopeViewModel()
{
ScopeId = 1,
ScopeName = "ScopeOne",
};
//
model.ScopeDisplay = model.ScopeId + " - " + model.ScopeName;
Scope scope = new Scope();
// model = scope;
Mapper.Initialize(cfg =>
{
cfg.CreateMap<ScopeViewModel, Scope>();
});
var sourceWithMapper =
Mapper.Map<Scope>(model);
}
0 comments:
Post a Comment