-
Notifications
You must be signed in to change notification settings - Fork 333
Data types
Converting between primitive types (ie. int, bool, double, decimal) is supported, including when those types are nullable. For all other types, if you can cast types in c#, you can also cast in Mapster.
decimal i = 123.Adapt<decimal>(); //equal to (decimal)123;
Mapster maps enums to numerics automatically, but it also maps strings to and from enums automatically in a fast manner.
The default Enum.ToString() in .NET is quite slow. The implementation in Mapster is double the speed. Likewise, a fast conversion from strings to enums is also included. If the string is null or empty, the enum will initialize to the first enum value.
In Mapster, flagged enums are also supported.
var e = "Read, Write, Delete".Adapt<FileShare>();
//FileShare.Read | FileShare.Write | FileShare.Delete
For enum to enum with different type, by default, Mapster will map enum by value. You can override to map enum by name by:
TypeAdapterConfig.GlobalSettings.Default
.EnumMappingStrategy(EnumMappingStrategy.ByName);
When Mapster maps other types to string, Mapster will use ToString
method. And whenever Mapster maps string to the other types, Mapster will use Parse
method.
var s = 123.Adapt<string>(); //equal to 123.ToString();
var i = "123".Adapt<int>(); //equal to int.Parse("123");
This includes mapping among lists, arrays, collections, dictionary including various interfaces: IList<T>
, ICollection<T>
, IEnumerable<T>
, ISet<T>
, IDictionary<TKey, TValue>
etc...
var list = db.Pocos.ToList();
var target = list.Adapt<IEnumerable<Dto>>();
Mapster can map two different objects using the following rules
- Source and destination property names are the same. Ex:
dest.Name = src.Name
- Source has get method. Ex:
dest.Name = src.GetName()
- Source property has child object which can flatten to destination. Ex:
dest.ContactName = src.Contact.Name
ordest.Contact_Name = src.Contact.Name
Example:
class Staff {
public string Name { get; set; }
public int GetAge() {
return (DateTime.Now - this.BirthDate).TotalDays / 365.25;
}
public Staff Supervisor { get; set; }
...
}
struct StaffDto {
public string Name { get; set; }
public int Age { get; set; }
public string SupervisorName { get; set; }
}
var dto = staff.Adapt<StaffDto>();
//dto.Name = staff.Name, dto.Age = staff.GetAge(), dto.SupervisorName = staff.Supervisor.Name
Mappable Object types are included:
- POCO classes
- POCO structs
- POCO interfaces
- Dictionary type implement
IDictionary<string, T>
- Record types (either class, struct, and interface)
Example for object to dictionary:
var point = new { X = 2, Y = 3 };
var dict = point.Adapt<Dictionary<string, int>>();
dict["Y"].ShouldBe(3);
Example for record types
class Person {
public string Name { get; }
public int Age { get; }
public Person(string name, int age) {
this.Name = name;
this.Age = age;
}
}
var src = new { Name = "Mapster", Age = 3 };
var target = src.Adapt<Person>();
There are limitations to map Record type automatically. Record type must not have a setter and have only one non-empty constructor, and all parameter names must match with properties. Otherwise you need to add MapToConstructor
config.
- 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