Skip to content

.NET - AutoMapper

License

Notifications You must be signed in to change notification settings

dimitrietataru/netcore-automapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 

Repository files navigation

.NET - AutoMapper

Install

PM> Install-Package AutoMapper -Version 10.1.1
PM> Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 8.1.1

Configure Profiles

public class AutoMapperProfile : AutoMapper.Profile
{
    public AutoMapperProfile()
    {
        CreateMap<TSource, TDestionation>();
        CreateMap<TDestionation, TSource>();
        
        CreateMap<Foo, Bar>();
        CreateMap<Bar, Foo>();
        
        CreateMap<Fizz, Buzz>().ReverseMap();
        
        CreateMap<FooDto, Foo>()
            .IgnoreAllPropertiesWithAnInaccessibleSetter()
            .ForMember(
                destination => destination.StringValue,
                options => options.MapFrom(source => source.StringValue))
            .ForMember(
                destination => destination.IntValue,
                options => options.MapFrom(source => source.IntValue))
            .ForMember(
                destination => destination.DoubleValue,
                options => options.MapFrom(source => source.DoubleValue))
            .ForMember(
                destination => destination.BooleanValue,
                options => options.MapFrom(source => source.BooleanValue))
            .AfterMap(
                (source, destination) =>
                {
                    // ..
                })
            .ForAllOtherMembers(options => options.Ignore());
    }
}

Configure Dependency Injection

using AutoMapper;

public void ConfigureServices(IServiceCollection services)
{
    // ..

    services.AddAutoMapper(typeof(Startup));
    
    // ..
}