-
Notifications
You must be signed in to change notification settings - Fork 331
Fluent API Code generation
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)
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);
});
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
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;
}
}
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; }
}
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 => ...
}
- 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