Skip to content

Fluent API Code generation

Max Wagner edited this page Oct 6, 2022 · 2 revisions

Configuration class

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>();}}

Generate models

Declare AdaptFrom, AdaptTo, or AdaptTwoWays.

Example:

config.AdaptTo("[name]Dto").ForType<Student>();

Then Mapster will generate:

publicclassStudentDto{
...}

Add types to generate

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)

Ignore some properties on generation

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}

Ignore a property

config.AdaptTo("[name]Dto").ForType<Student>(cfg =>{cfg.Ignore(poco =>poco.LastName);});

Change a property name, type

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});

Forward property types

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 Person

Generate readonly properties

For 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;}}

Generate nullable properties

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;}}

Generate extension methods

Generate using GenerateMapper.

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=> ...}

Clone this wiki locally