Skip to content

Repository files navigation

AutoMapper

Based on the original .NET AutoMapper, keeping a workaround for the static mapping methods which were removed with v9.

Installation

NuGet: https://www.nuget.org/packages/DXSdata.AutoMapper

Usage

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingAutoMapper.DXSdata;namespaceMapperTest{classProgram{staticvoidMain(){//If DB classes reside in other namespace, assembly etc.//Mapper.Assembly = nameof(MyAlternativeDbContext);//For more complex types, e.g nested custom objects which AutoMapper cannot determine automatically//As an alternative, you can also use Attribute Mapping: https://docs.automapper.org/en/latest/Attribute-mapping.html//Mapper.CustomMappings.Add(typeof(MyCustomSubclass), typeof(MyCustomSubclassViewModel)); //or: //Mapper.CustomMappings.Add<MyCustomSubclass, MyCustomSubclassViewModel>();//Optional advanced configuration; see belowMapper.OnConfiguring+=AdvancedConf;varlist1=newList<MyClass1>{newMyClass1(),newMyClass1(),newMyClass1()};vardto=list1.Map<List<MyClass2>>();vardto2=Mapper.Map<List<MyClass2>>(list1);vardto3=Mapper.Map<MyClass2[]>(list1);vardto4=list1.MapL<MyClass2>();//for conveniencevardto5=Mapper.MapL<MyClass2>(list1);vardto6=list1.AsQueryable().ProjectTo<List<MyClass2>>();//for DB queriesvardto7=Mapper.ProjectTo<List<MyClass2>>(list1.AsQueryable());}staticvoidAdvancedConf(objectsender,MapperConfiguringEventArgse){//e.Configuration.CreateMap<MyViewModel, My>().ForMember(my => my.MyEquipment, opt => opt.MapFrom(dto => MyUnflattenMethod<MyEquipment>(dto.Id, dto.Equipment)));//e.Configuration.CreateMap<My, MyViewModel>().ForMember(dto => dto.Equipment, opt => opt.MapFrom(my => MyFlattenMethod(my.MyEquipment)));}}}

Notes

Nested objects

If you use nested objects, like MyClass1Object.MyClass2Object.Var1, please use the approach "Attribute Mapping". Otherwise you will get a "missing maps" error. You can also use Mapper.CustomMappings (see example above).

Multiple projects

If the attributed classes are not within your default project assembly, please set Mapper.Assembly manually.

E.g. if your DB context classes reside in another project called MyContext, set:

Mapper.Assembly = nameof(MyContext);

Links

Website: https://www.dxsdata.com/2019/12/automapper-with-static-extensionmethods-for-net-standard

About

Based on the original .NET AutoMapper, keeping a workaround for the static mapping methods which were removed with v9.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages