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.

public class MyRegister : ICodeGenerationRegister
{
    public void Register(CodeGenerationConfig config)
    {
        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:

public class StudentDto {
    ...
}

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

public class Student {
    [DataMember]
    public string Name { get; set; }     //this property will be generated
    public string LastName { 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 name
        cfg.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:

public class StudentDto {
    public string Name { get; }

    public StudentDto(string name) {
        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:

public class StudentMerge {
    public int? 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:

public class StudentDto {
    ...
}
public static class StudentMapper {
    public static StudentDto AdaptToDto(this Student poco) { ... }
    public static StudentDto AdaptTo(this Student poco, StudentDto dto) { ... }
    public static Expression<Func<Student, StudentDto>> ProjectToDto => ...
}
Clone this wiki locally