Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 408
Fluent API Code generation
Create a configuration class implement ICodeGenerationRegister.
publicclassMyRegister:ICodeGenerationRegister{publicvoidRegister(CodeGenerationConfigconfig){config.AdaptTo("[name]Dto").ForAllTypesInNamespace(Assembly.GetExecutingAssembly(),"Sample.CodeGen.Domains");config.GenerateMapper("[name]Mapper").ForType<Course>().ForType<Student>();}}Declare AdaptFrom, AdaptTo, or AdaptTwoWays.
Example:
config.AdaptTo("[name]Dto").ForType<Student>();Then Mapster will generate:
publicclassStudentDto{
...}You can add types by ForTypes, ForAllTypesInNamespace, ForType<>, and you can remove added types using ExcludeTypes.
config.AdaptTo("[name]Dto").ForAllTypesInNamespace(Assembly.GetExecutingAssembly(),"Sample.CodeGen.Domains").ExcludeTypes(typeof(SchoolContext)).ExcludeTypes(type =>type.IsEnum)By default, code generation will ignore properties that annotated [AdaptIgnore] attribute. But you can add more settings which include IgnoreAttributes, IgnoreNoAttributes, IgnoreNamespaces.
Example:
config.AdaptTo("[name]Dto").ForType<Student>().IgnoreNoAttributes(typeof(DataMemberAttribute));publicclassStudent{[DataMember]publicstringName{get;set;}//this property will be generatedpublicstringLastName{get;set;}//this will not be generated}config.AdaptTo("[name]Dto").ForType<Student>(cfg =>{cfg.Ignore(poco =>poco.LastName);});config.AdaptTo("[name]Dto").ForType<Student>(cfg =>{cfg.Map(poco =>poco.LastName,"Surname");//change property namecfg.Map(poco =>poco.Grade,typeof(string));//change property type});By default, code generation will forward type on the same declaration. (For example, Student has ICollection<Enrollment>, after code generation StudentDto will has ICollection<EnrollmentDto>).
You can override this by AlterType.
config.AdaptTo("[name]Dto").ForAllTypesInNamespace(Assembly.GetExecutingAssembly(),"Sample.CodeGen.Domains").AlterType<Student,Person>();//forward all Student to PersonFor AdaptTo and AdaptTwoWays, you can generate readonly properties with MapToConstructor setting.
For example:
config.AdaptTo("[name]Dto").ForType<Student>().MapToConstructor(true);This will generate:
publicclassStudentDto{publicstringName{get;}publicStudentDto(stringname){this.Name=name;}}For AdaptFrom, you can generate nullable properties with IgnoreNullValues setting.
For example:
config.AdaptFrom("[name]Merge").ForType<Student>().IgnoreNullValues(true);This will generate:
publicclassStudentMerge{publicint?Age{get;set;}}For any POCOs declared with AdaptFrom, AdaptTo, or AdaptTwoWays, you can declare GenerateMapper in order to generate extension methods.
Example:
config.AdaptTo("[name]Dto").ForType<Student>();config.GenerateMapper("[name]Mapper").ForType<Student>();Then Mapster will generate:
publicclassStudentDto{
...}publicstaticclassStudentMapper{publicstaticStudentDtoAdaptToDto(thisStudentpoco){ ...}publicstaticStudentDtoAdaptTo(thisStudentpoco,StudentDtodto){ ...}publicstaticExpression<Func<Student,StudentDto>>ProjectToDto=> ...}