Aller au contenu

« Mapster » : différence entre les versions

De Banane Atomic
Ligne 27 : Ligne 27 :


= [https://github.com/MapsterMapper/Mapster/wiki/Dependency-Injection Dependency Injection] =
= [https://github.com/MapsterMapper/Mapster/wiki/Dependency-Injection Dependency Injection] =
<kode lang='ps'>
<kode lang='shell'>
dotnet install Mapster.DependencyInjection
dotnet add package Mapster.DependencyInjection
</kode>
</kode>



Version du 18 janvier 2025 à 22:01

Links

Mapping

// create a new itemDto from item
var itemDto = item.Adapt<ItemDto>();

// update itemDto with data from item
item.Adapt(itemDto);

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

Dependency Injection

dotnet add package Mapster.DependencyInjection

Installation

dotnet add package Mapster