« Mapster » : différence entre les versions
Apparence
(20 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 2 : | Ligne 2 : | ||
= Links = | = Links = | ||
* [https://github.com/MapsterMapper/Mapster Mapster on GitHub] | * [https://github.com/MapsterMapper/Mapster Mapster on GitHub] | ||
* [https://github.com/MapsterMapper/Mapster/wiki Mapster wiki on GitHub] | |||
= Mapping = | = Mapping = | ||
<kode lang='cs'> | <kode lang='cs'> | ||
// create | // create destination from data from source | ||
var | var destination = source.Adapt<Destination>(); | ||
// update | // update destination with data from source | ||
source.Adapt(destination); | |||
</kode> | </kode> | ||
= [https://github.com/MapsterMapper/Mapster/wiki/Configuration Configuration] = | = [https://github.com/MapsterMapper/Mapster/wiki/Configuration Configuration] = | ||
<kode lang='cs'> | <kode lang='cs'> | ||
// global config | |||
TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true); | |||
// config the mapping from Item to ItemDto | |||
TypeAdapterConfig<Item, ItemDto> // src, dest | TypeAdapterConfig<Item, ItemDto> // src, dest | ||
.NewConfig() // create new map config (overwrite existing ones) | .NewConfig() // create new map config (overwrite existing ones) | ||
Ligne 20 : | Ligne 25 : | ||
.Map(dest => dest.Prop2, src => $"{0} {1}", src.Prop2, src.Prop3)); // custom mapping for Prop2 | .Map(dest => dest.Prop2, src => $"{0} {1}", src.Prop2, src.Prop3)); // custom mapping for Prop2 | ||
</kode> | </kode> | ||
== IRegister == | |||
<filebox fn='Program.cs'> | |||
// scan the assembly to load the classes implementing IRegister | |||
TypeAdapterConfig.GlobalSettings.Scan(typeof(ItemMappingRegister).Assembly); | |||
// load only on register | |||
var register = new ItemMappingRegister(); | |||
var config = new TypeAdapterConfig(); | |||
config.Apply(register); | |||
</filebox> | |||
<filebox fn='MappingRegisters/ItemMappingRegister.cs'> | |||
public class ItemMappingRegister : IRegister | |||
{ | |||
public void Register(TypeAdapterConfig config) => config.NewConfig<CreateUpdateItemRequest, Item>(); | |||
} | |||
</filebox> | |||
= [https://github.com/MapsterMapper/Mapster/wiki/Dependency-Injection Dependency Injection] = | |||
{{info | Useful if | |||
* you migrate from Automapper to Mapster and you do not want to change your code | |||
* you need some injected services in your mappings}} | |||
<kode lang='bash'> | |||
dotnet add package Mapster.DependencyInjection | |||
</kode> | |||
<filebox fn='Program.cs'> | |||
var config = new TypeAdapterConfig(); | |||
config.Scan(typeof(ItemMappingRegister).Assembly); | |||
config.Compile(); | |||
services.AddSingleton(config); | |||
services.AddScoped<IMapper, ServiceMapper>(); | |||
</filebox> | |||
<filebox fn='MappingRegisters/ItemMappingRegister.cs'> | |||
public class ItemMappingRegister : IRegister | |||
{ | |||
public void Register(TypeAdapterConfig config) | |||
=> config | |||
.NewConfig<ItemDto, Item>() | |||
.Map(dest => dest.Id, src => MapContext.Current.GetService<SomeService>().Do(src.Id)); | |||
} | |||
</filebox> | |||
<filebox fn='MyService.cs'> | |||
public class MyService(IMapper mapper) | |||
{ | |||
public void Do() | |||
{ | |||
var itemDto = mapper.Map<ItemDto>(item); | |||
} | |||
} | |||
</filebox> | |||
* [https://stackoverflow.com/questions/61172885/mapster-global-configuration-with-dependency-injection Mapster Global Configuration with Dependency Injection] | |||
= Installation = | = Installation = |
Dernière version du 18 janvier 2025 à 23:21
Links
Mapping
// create destination from data from source var destination = source.Adapt<Destination>(); // update destination with data from source source.Adapt(destination); |
Configuration
// global config TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true); // config the mapping from Item to ItemDto TypeAdapterConfig<Item, ItemDto> // src, dest .NewConfig() // create new map config (overwrite existing ones) .ForType() // OR create a map config if no one exists, otherwise append to th existing one .Ignore(dest => dest.Prop1) // do not map Prop1 .Map(dest => dest.Prop2, src => $"{0} {1}", src.Prop2, src.Prop3)); // custom mapping for Prop2 |
IRegister
Program.cs |
// scan the assembly to load the classes implementing IRegister TypeAdapterConfig.GlobalSettings.Scan(typeof(ItemMappingRegister).Assembly); // load only on register var register = new ItemMappingRegister(); var config = new TypeAdapterConfig(); config.Apply(register); |
MappingRegisters/ItemMappingRegister.cs |
public class ItemMappingRegister : IRegister { public void Register(TypeAdapterConfig config) => config.NewConfig<CreateUpdateItemRequest, Item>(); } |
Dependency Injection
![]() |
Useful if
|
dotnet add package Mapster.DependencyInjection |
Program.cs |
var config = new TypeAdapterConfig(); config.Scan(typeof(ItemMappingRegister).Assembly); config.Compile(); services.AddSingleton(config); services.AddScoped<IMapper, ServiceMapper>(); |
MappingRegisters/ItemMappingRegister.cs |
public class ItemMappingRegister : IRegister { public void Register(TypeAdapterConfig config) => config .NewConfig<ItemDto, Item>() .Map(dest => dest.Id, src => MapContext.Current.GetService<SomeService>().Do(src.Id)); } |
MyService.cs |
public class MyService(IMapper mapper) { public void Do() { var itemDto = mapper.Map<ItemDto>(item); } } |
Installation
dotnet add package Mapster |