-
Notifications
You must be signed in to change notification settings - Fork 331
TextTemplate
PM> Install-Package ExpressionTranslator
Although Mapster allow you to step-into debugging, but all mapping are generated at runtime. Therefore, error will be captured at runtime, and we also lose the power of static analysis to find the usage.
Here are steps to add code generation.
- Create text template
- In template, add references & mapping logic
<#@ template debug="true" language="C#" #>
<#@ output extension=".g.cs" #>
<#@ Assembly Name="netstandard" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Runtime" #>
<#@ Assembly Name="System.Linq.Expressions" #>
<#@ Assembly Name="$(TargetDir)/$(ProjectName).dll" #>
<#@ Assembly Name="$(TargetDir)/Mapster.dll" #>
<#@ Assembly Name="$(TargetDir)/ExpressionTranslator.dll" #>
<#@ import namespace="ExpressionDebugger" #>
<#@ import namespace="Mapster" #>
<#@ import namespace="YourNamespace" #>
<#
//this line is to generate all nested mapping in 1 file
TypeAdapterConfig.GlobalSettings.SelfContainedCodeGeneration = true;
var cust = default(Customer);
var def = new ExpressionDefinitions
{
IsStatic = true, //change to false if you want instance
MethodName = "Map",
Namespace = "YourNamespace",
TypeName = "CustomerMapper"
};
var code = cust.BuildAdapter()
.CreateMapExpression<CustomerDTO>()
.ToScript(def);
WriteLine(code);
#>
- Generate code by right click on template file, and select
Run Custom Tool
.
That's it. Done!
- Example: CustomerMapper
- T4 Documentation (Microsoft)
Q: How to pass lambda to Before/After mapping?
A: Please use BeforeMappingInline
and AfterMappingInline
instead. link
Q: Can I generate multiple outputs from single template?
A: You can. link
Q: After running template file, it said library XXX not found.
A: Some unused libraries will be excluded during build. You can direct reference to dll in template file. Or tell Visual Studio to copy all reference libraries to output. link
<!-- This setting will copy all references to output -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- This setting apply only Debug build -->
<CopyLocalLockFileAssemblies Condition=" '$(Configuration)'=='Debug' ">true</CopyLocalLockFileAssemblies>
Q: After running template file on Mac, it said netstandard
is not found.
A: You need direct reference.
<!-- Remove this line -->
<#@ Assembly Name="netstandard" #>
<!-- Change to this line (path might not be the same) -->
<#@ Assembly Name="/usr/local/share/dotnet/sdk/2.2.103/Microsoft/Microsoft.NET.Build.Extensions/net461/lib/netstandard.dll" #>
Q: After running template file in .NET Core project on Windows, it said, System.Runtime version 4.2.x.x not found.
A: You can build using .NET Framework version. Otherwise, you need to update assembly binding in Visual Studio config file. link
Q: After running template file, it said Compile items are duplicated.
A: You can set property to skip generated items.
<DefaultItemExcludes>**/*.g.cs</DefaultItemExcludes>
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance