Difference between revisions of "AutoMapper"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:MVC Category:Core == Installation == Command line dotnet add package AutoMapper dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection --v...")
 
Line 23: Line 23:
 
== Mapping Profile ==
 
== Mapping Profile ==
 
'''Profile''' is in '''AutoMapper''' namespace  
 
'''Profile''' is in '''AutoMapper''' namespace  
<pre class="brush:CSharp;">
+
<pre class="brush:csharp;">
 
public class MappingProfile : Profile  
 
public class MappingProfile : Profile  
 
{
 
{

Revision as of 16:41, 5 April 2017


Installation

Command line

dotnet add package AutoMapper
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection --version 1.2.0

PM>

Install-Package AutoMapper

Add to Dependency Injection

In startup.cs -> ConfigurationServices add this line

services.AddAutoMapper();

add

using AutoMapper;

usage

inject as IMapper mapper

public MakesController(VegaDbContext context, IMapper mapper)
...

Map like below. First parameter is source second is target and in parentheses, we provide the object

mapper.Map<List<Make>, List<MakeResource>>(makes)

Mapping Profile

Profile is in AutoMapper namespace

public class MappingProfile : Profile 
{
  public MappingProfile
  {
    CreateMap<Make, MakeResource>();
    CreateMap<Model, ModelResource>();
  }
}
Mapping profile is loaded during startup