Skip to content
Arad Alvand edited this page Aug 10, 2022 · 20 revisions

References

Basic

Method Description Link
src.Adapt<Dest>() Mapping to new type basic
src.Adapt(dest) Mapping to existing object basic
query.ProjectToType<Dest>() Mapping from queryable basic
Convention & Data type support data types

Mapper instance (for dependency injection)

Method Description Link
IMapper mapper = new Mapper() Create mapper instance mappers
mapper.Map<Dest>(src) Mapping to new type
mapper.Map(src, dest) Mapping to existing object

Builder (for complex mapping)

Method Description Link
src.BuildAdapter()
mapper.From(src)
Create builder mappers
.ForkConfig(config => ...) Inline configuration config location
.AddParameters(name, value) Passing runtime value setting values
.AdaptToType<Dest>() Mapping to new type
.AdaptTo(dest) Mapping to existing object
.CreateMapExpression<Dest>() Get mapping expression
.CreateMapToTargetExpression<Dest>() Get mapping to existing object expression
.CreateProjectionExpression<Dest>() Get mapping from queryable expression

Config

Method Description Link
TypeAdapterConfig.GlobalSettings Global config config
var config = new TypeAdapterConfig() Create new config instance config instance
src.Adapt<Dest>(config) Passing config to mapping
new Mapper(config) Passing config to mapper instance
src.BuildAdapter(config) Passing config to builder
config.RequireDestinationMemberSource Validate all properties are mapped config validation
config.RequireExplicitMapping Validate all type pairs are defined config validation
config.AllowImplicitDestinationInheritance Use config from destination based class inheritance
config.AllowImplicitSourceInheritance Use config from source based class inheritance
config.SelfContainedCodeGeneration Generate all nested mapping in 1 method TextTemplate
config.Compile() Validate mapping instruction & cache config validation
config.CompileProjection() Validate mapping instruction & cache for queryable
config.Clone() Copy config config instance
config.Fork(forked => ...) Inline configuration config location

Config scanning

Method Description Link
IRegister Interface for config scanning config location
config.Scan(...assemblies) Scan for config in assemblies config location
config.Apply(...registers) Apply registers directly config location

Declare settings

Method Description Link
config.Default Get setting applied to all type pairs config
TypeAdapterConfig<Src, Dest>.NewConfig()
config.NewConfig<Src, Dest>()
Create setting applied to specific type pairs config
TypeAdapterConfig<Src, Dest>.ForType()
config.ForType<Src, Dest>()
Get setting applied to specific type pairs config
config.ForType(typeof(GenericPoco<>),typeof(GenericDto<>)) Get setting applied to generic type pairs config
config.When((src, dest, mapType) => ...) Get setting that applied conditionally config
config.ForDestinationType<Dest>() Get setting that applied to specific destination type config
Configuration for nested mapping nested mapping

Settings

Method Description Apply to queryable Link
AddDestinationTransform Clean up data for a specific type x setting values
AfterMapping Add steps after mapping done before-after
AvoidInlineMapping Skip inline process for large type mapping object reference
BeforeMapping Add steps before mapping start before-after
ConstructUsing Define how to create object x constructor
EnableNonPublicMembers Mapping non-public properties non-public
EnumMappingStrategy Choose whether mapping enum by value or by name data types
Fork Add new settings without side effect on main config x nested mapping
GetMemberName Define how to resolve property name x custom naming
Ignore Ignore specific properties x ignore
IgnoreAttribute Ignore specific attributes annotated on properties x attribute
IgnoreIf Ignore conditionally x ignore
IgnoreMember Setup rules to ignore x rule based
IgnoreNonMapped Ignore all properties not defined in Map x ignore
IgnoreNullValues Not map if src property is null shallow & merge
Include Include derived types on mapping inheritance
IncludeAttribute Include specific attributes annotated on properties x attribute
IncludeMember Setup rules to include x rule based
Inherits Copy setting from based type x inheritance
Map Define property pairs x custom mapping
MapToConstructor Mapping to constructor x constructor
MapToTargetWith Define how to map to existing object between type pair custom conversion
MapWith Define how to map between type pair x custom conversion
MaxDepth Limit depth of nested mapping x object reference
NameMatchingStrategy Define how to resolve property's name x custom naming
PreserveReference Tracking reference when mapping object reference
ShallowCopyForSameType Direct assign rather than deep clone if type pairs are the same shallow & merge
TwoWays Define type mapping are 2 ways x 2-ways & unflattening
Unflattening Allow unflatten mapping x 2-ways & unflattening
UseDestinationValue Use existing property object to map data readonly-prop

Attributes

Annotation Description Link
[AdaptMember(name)] Mapping property to different name attribute
[AdaptIgnore(side)] Ignore property from mapping attribute
[UseDestinationValue] Use existing property object to map data attribute
[AdaptTo] [AdaptFrom] [AdaptTwoWays] Add setting on POCO class location
[Mapper] [GenerateMapper] [PropertyType] Define setting for code generation Mapster.Tool

Plugins

Plugin Method Description
Async setting.AfterMappingAsync
builder.AdaptToTypeAsync
perform async operation on mapping
Debugging config.Compiler = exp => exp.CompileWithDebugInfo() compile to allow step into debugging
Dependency Injection MapContext.Current.GetService<IService>() Inject service into mapping logic
EF 6 & EF Core builder.EntityFromContext Copy data to tracked EF entity
FEC config.Compiler = exp => exp.CompileFast() compile using FastExpressionCompiler
Immutable config.EnableImmutableMapping() mapping to immutable collection
Json.net config.EnableJsonMapping() map json from/to poco and string

Code Generation Tools

Plugin Tool Description
Mapster.Tool dotnet mapster generate DTOs and mapping codes on build
TextTemplate t4 generate mapping codes using t4
Clone this wiki locally