Back to Mapster

References

docs/api/Reference.md

10.0.711.3 KB
Original Source

References

Basic

MethodDescriptionLink
src.Adapt<Dest>()Mapping to new typebasic
src.Adapt(dest)Mapping to existing objectbasic
query.ProjectToType<Dest>()Mapping from queryablebasic
Convention & Data type supportdata types

Mapper instance (for dependency injection)

MethodDescriptionLink
IMapper mapper = new Mapper()Create mapper instancemappers
mapper.Map<Dest>(src)Mapping to new type
mapper.Map(src, dest)Mapping to existing object

Builder (for complex mapping)

MethodDescriptionLink
src.BuildAdapter()
mapper.From(src)Create buildermappers
.ForkConfig(config => ...)Inline configurationconfig location
.AddParameters(name, value)Passing runtime valuesetting 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

MethodDescriptionLink
TypeAdapterConfig.GlobalSettingsGlobal configconfig
var config = new TypeAdapterConfig()Create new config instanceconfig 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.RequireDestinationMemberSourceValidate all properties are mappedconfig validation
config.RequireExplicitMappingValidate all type pairs are definedconfig validation
config.AllowImplicitDestinationInheritanceUse config from destination based classinheritance
config.AllowImplicitSourceInheritanceUse config from source based classinheritance
config.SelfContainedCodeGenerationGenerate all nested mapping in 1 methodTextTemplate
config.Compile()Validate mapping instruction & cacheconfig validation
config.CompileProjection()Validate mapping instruction & cache for queryable
config.Clone()Copy configconfig instance
config.Fork(forked => ...)Inline configurationconfig location

Config scanning

MethodDescriptionLink
IRegisterInterface for config scanningconfig location
config.Scan(...assemblies)Scan for config in assembliesconfig location
config.Apply(...registers)Apply registers directlyconfig location
<!-- TODO: Check if this might be Settings? If so, the used variable might not be the best choice 'config' -->

Declare settings

MethodDescriptionLink
config.DefaultGet setting applied to all type pairsconfig
TypeAdapterConfig<Src, Dest>.NewConfig()
config.NewConfig<Src, Dest>()Create setting applied to specific type pairsconfig
TypeAdapterConfig<Src, Dest>.ForType()
config.ForType<Src, Dest>()Get setting applied to specific type pairsconfig
config.ForType(typeof(GenericPoco<>),typeof(GenericDto<>))Get setting applied to generic type pairsconfig
config.When((src, dest, mapType) => ...)Get setting that applied conditionallyconfig
config.ForDestinationType<Dest>()Get setting that applied to specific destination typeconfig
Configuration for nested mappingnested mapping

Settings

MethodDescriptionApply to queryableLink
AddDestinationTransformClean up data for a specific typexsetting values
AfterMappingAdd steps after mapping donebefore-after
AvoidInlineMappingSkip inline process for large type mappingobject reference
BeforeMappingAdd steps before mapping startbefore-after
ConstructUsingDefine how to create objectxconstructor
EnableNonPublicMembersMapping non-public propertiesnon-public
EnumMappingStrategyChoose whether mapping enum by value or by namedata types
ForkAdd new settings without side effect on main configxnested mapping
GetMemberNameDefine how to resolve property namexcustom naming
IgnoreIgnore specific propertiesxignore
IgnoreAttributeIgnore specific attributes annotated on propertiesxattribute
IgnoreIfIgnore conditionallyxignore
IgnoreMemberSetup rules to ignorexrule based
IgnoreNonMappedIgnore all properties not defined in Mapxignore
IgnoreNullValuesNot map if src property is nullshallow & merge
IncludeInclude derived types on mappinginheritance
IncludeAttributeInclude specific attributes annotated on propertiesxattribute
IncludeMemberSetup rules to includexrule based
InheritsCopy setting from based typexinheritance
MapDefine property pairsxcustom mapping
MapToConstructorMapping to constructorxconstructor
MapToTargetWithDefine how to map to existing object between type paircustom conversion
MapWithDefine how to map between type pairxcustom conversion
MaxDepthLimit depth of nested mappingxobject reference
NameMatchingStrategyDefine how to resolve property's namexcustom naming
PreserveReferenceTracking reference when mappingobject reference
ShallowCopyForSameTypeDirect assign rather than deep clone if type pairs are the sameshallow & merge
TwoWaysDefine type mapping are 2 waysx2-ways & unflattening
UnflatteningAllow unflatten mappingx2-ways & unflattening
UseDestinationValueUse existing property object to map datareadonly-prop

Attributes

AnnotationDescriptionLink
[AdaptMember(name)]Mapping property to different nameattribute
[AdaptIgnore(side)]Ignore property from mappingattribute
[UseDestinationValue]Use existing property object to map dataattribute
[AdaptTo] [AdaptFrom] [AdaptTwoWays]Add setting on POCO classlocation
[Mapper] [GenerateMapper] [PropertyType]Define setting for code generationMapster.Tool

Packages

PackagesMethodDescription
Asyncsetting.AfterMappingAsync
builder.AdaptToTypeAsyncperform async operation on mapping
Debuggingconfig.Compiler = exp => exp.CompileWithDebugInfo()compile to allow step into debugging
Dependency InjectionMapContext.Current.GetService<IService>()Inject service into mapping logic
EF 6 & EF Corebuilder.EntityFromContextCopy data to tracked EF entity
FECconfig.Compiler = exp => exp.CompileFast()compile using FastExpressionCompiler
Immutableconfig.EnableImmutableMapping()mapping to immutable collection
Json.netconfig.EnableJsonMapping()map json from/to poco and string

Code Generation Tools

PluginToolDescription
Mapster.Tooldotnet mapstergenerate DTOs and mapping codes on build
TextTemplatet4generate mapping codes using t4