A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/MapsterMapper/Mapster/wiki/Fluent-API-Code-generation below:

Fluent API Code generation · MapsterMapper/Mapster Wiki · GitHub

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

Declare AdaptFrom, AdaptTo, or AdaptTwoWays.

Example:

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

Then Mapster will generate:

public class StudentDto {
    ...
}

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

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4