Automapper
a)Install Automapper Package from Nuget
b)Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
c) Service\Configuration
public class ServiceProfile : Profile
{
public ServiceProfile()
{
CreateMap<QuestionnaireServiceModel, QuestionnaireViewModel>();
CreateMap<InstallationRange, InstallationTypes>()
.ForMember(dest => dest.InstallTypeId, opts => opts.MapFrom(src => src.InstallTypeId))
.ForMember(dest => dest.InstallTypeDesc, opts => opts.MapFrom(src => src.InstallTypeDesc))
.ForMember(dest => dest.IsActive, opts => opts.MapFrom(src => src.IsActive));
CreateMap<CustomerInput, CustomerEntityInput>().AfterMap((src, dest) =>
{
dest.Address1 = src.AddressLine1;
dest.Address2 = src.AddressLine2;
});
}
}
d) Controller.cs
public class QuestionnaireController : Controller
{
{
private readonly IConfiguration _config;
private readonly IQuestionnaireService _questionnaireService;
private readonly IMapper _mapper;
public QuestionnaireController(IConfiguration config, IQuestionnaireService questionnaireService, IMapper mapper)
{
_config = config;
_questionnaireService = questionnaireService;
_mapper = mapper;
}
public ViewResult Index(QuestionnaireViewModel qModel=null)
{
if (qModel!=null && !string.IsNullOrEmpty(qModel.QuestionnaireTitle))
{
return View(qModel);
}
else
{
QuestionnaireServiceModel listQuestionnaire = _questionnaireService.GetAllQuestionnaire().Result;
var questionnaireViewModel = _mapper.Map<QuestionnaireViewModel>(listQuestionnaire);
return View(questionnaireViewModel);
}
}
}
No comments:
Post a Comment