1 Introduction
A powerful customizable tool for mapping entities to each other. Entities can be pure objects, DataReaders, SQL commands, and whatever you need. The tool generates runtime code through the Emit library. It is useful to deal with DTO objects, data access layers, etc.
Advantages : fast, easy to use, flexible;
disadvantages : no maintenance for 11 years...
Emit Mapper can automatically convert the following types:
- Any use of ToString () method for string processing.
- Use the basic types of the System.Convert class.
- Nullable types are value types and vice versa.
- Enumeration is its underlying type, and vice versa.
- Enumerate to string and vice versa.
- Different types of collections (Array, ArrayList, List<>, IEnumerable)
- Class to structure, and vice versa.
- Complex types with complex nested members use recursive mappings with shallow or deep correspondences.
2. Website
3. Use
Through the console
PM> Install-Package EmitMapper
or:
4. Use steps
The default mapping configurator can automatically convert the following types:
- Use ToString() method for any type to string type;
- You can use the primitive type converted by the System.Convert class;
- Nullable types, enumeration types, various collection types, structures and classes;
- Complex nested types are converted recursively;
- If the default conversion cannot meet the requirements, the default mapping configurator also allows you to specify naming conventions, custom constructors, custom converters, ignoring members, etc.
4.1 Ordinary mapping
The field names have the same
public class UserInfo
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
}
public class UserInfoDTO
{
public string name {
get; set; }
public string address {
get; set; }
}
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>();
UserInfoDTO userdto = mapper.Map(user);
4.2 Ignore the mapping of a field
public class UserInfo
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
}
public class UserInfoDTO
{
public string id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = "北京"
};
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.IgnoreMembers<UserInfo, UserInfoDTO>(new string[] {
"name" })
);
UserInfoDTO userdto = mapper.Map(user);
4.3 Assign default values to empty elements
public class UserInfo
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
public DateTime? godate {
get; set; }
}
public class UserInfoDTO
{
public string id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
public DateTime godate {
get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = null,
godate = null
};
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
//如果日期为空设置为当前时间
.NullSubstitution<DateTime?, DateTime>((value) => DateTime.Now)
//如果string类型为null赋值为“”
.NullSubstitution<string, string>((value) => "")
);
UserInfoDTO userdto = mapper.Map(user);
4.4 The names of the two entities are inconsistent and need to be mapped
public class UserInfo
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
}
public class UserInfoDTO
{
public int id {
get; set; }
public string name {
get; set; }
public string userAddress {
get; set; }
}
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.MatchMembers((x, y) =>
{
if (x == "address" && y == "userAddress")
{
return true;
}
return x == y;
})
);
UserInfoDTO userdto = mapper.Map(user);
4.4 There is a foreign key association, and the name of the foreign key needs to be mapped
public class UserInfo
{
public int id {
get; set; }
public string name {
get; set; }
public string address {
get; set; }
public Teacher teacher {
get; set; }
}
public class Teacher
{
public int id {
get; set; }
public string name {
get; set; }
}
public class UserInfoDTO
{
public int id {
get; set; }
public string name {
get; set; }
public string teacher {
get; set; }
}
var user = new UserInfo {
id = 12,
name = "张三",
address = "北京",
teacher = new Teacher {
id = 11,
name = "王五"
}
};
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>(
new DefaultMapConfig()
.ConvertUsing<Teacher, string>(t => t.name)
);
UserInfoDTO userdto = mapper.Map(user);
4.3 Multiple special situations coexist
public class Sourse
{
public int A;
public decimal? B;
public string C;
public Inner D;
public string E;
}
public class Dest
{
public int? A;
public decimal B;
public DateTime C;
public Inner2 D;
public string F;
}
public class Inner
{
public long D1;
public Guid D2;
}
public class Inner2
{
public long D12;
public Guid D22;
}
ObjectsMapper<Sourse, Dest> mapper1 =
new ObjectMapperManager().GetMapper<Sourse, Dest>(
new DefaultMapConfig()
.IgnoreMembers<Sourse, Dest>(new string[] {
"A" })
.NullSubstitution<decimal?, decimal>((value) => -1M)
.ConvertUsing<Inner, Inner2>(value => new Inner2 {
D12 = value.D1, D22 = value.D2 })
.PostProcess<Dest>((value, state) => {
value.F = "nothing"; return value; })
);
Dest dst = mapper1.Map(src);
5. Simple comparison between EmitMapper and TinyMapper
Original article: Simple comparison between EmitMapper and TinyMapper