public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
public class FooDto
{
public int ID { get; set; }
public string Name { get; set; }
}
public void Map()
{
var config = new MapperConfiguration(cfg => cfg.CreateMap());
var mapper = config.CreateMapper();
Foo foo = new Foo { ID = 1, Name = "Tom" };
FooDto dto = mapper.Map(foo);
}
2 注册
在使用 Map 方法之前,首先要告诉 AutoMapper 什么类可以映射到什么类。
var config = new MapperConfiguration(cfg => cfg.CreateMap());
每个 AppDomain 只能进行一次配置。这意味着放置配置代码的最佳位置是在应用程序启动中,例如 ASP.NET 应用程序的 Global.asax 文件。
从 9.0 开始 Mapper.Initialize 方法就不可用了。
2.1 Profile
Profile 是组织映射的另一种方式。新建一个类,继承 Profile,并在构造函数中配置映射。
public class EmployeeProfile : Profile
{
public EmployeeProfile()
{
CreateMap();
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile();
});
public class Foo
{
public int Id { get; set; }
public string MyName { get; set; }
}
public class FooDto
{
public int ID { get; set; }
public string My_Name { get; set; }
}
public void Map()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap();
cfg.SourceMemberNamingConvention = new PascalCaseNamingConvention();
cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
});
var mapper = config.CreateMapper();
Foo foo = new Foo { Id = 2, MyName = "Tom" };
FooDto dto = mapper.Map(foo);
}
3.2 配置可见性
默认情况下,AutoMapper 仅映射 public 成员,但其实它是可以映射到 private 属性的。
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.SetMethod.IsPrivate;
cfg.CreateMap();
});
需要注意的是,这里属性必须添加 private set,省略 set 是不行的。
3.3 全局属性/字段过滤
默认情况下,AutoMapper 尝试映射每个公共属性/字段。以下配置将忽略字段映射。
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapField = fi => false;
});
3.4 识别前缀和后缀
var config = new MapperConfiguration(cfg =>
{
cfg.RecognizePrefixes("My");
cfg.RecognizePostfixes("My");
}
3.5 替换字符
var config = new MapperConfiguration(cfg =>
{
cfg.ReplaceMemberName("Ä", "A");
});
这功能我们基本上用不上。
4 调用构造函数
有些类,属性的 set 方法是私有的。
public class Commodity
{
public string Name { get; set; }
public int Price { get; set; }
}
public class CommodityDto
{
public string Name { get; }
public int Price { get; }
public CommodityDto(string name, int price)
{
Name = name;
Price = price * 2;
}
}
public class Commodity
{
public string Name { get; set; }
public int Price { get; set; }
}
public class CommodityDto
{
public string Name { get; }
public int Price { get; }
public CommodityDto(string name, int price)
{
Name = name;
Price = price * 2;
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Employee2 : Employee
{
public string DeptName { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
}
public class EmployeeDto2 : EmployeeDto
{
public string DeptName { get; set; }
}
数组映射代码如下:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().Include();
cfg.CreateMap();
});
IMapper mapper = config.CreateMapper();
var employees = new[]
{
new Employee { ID = 1, Name = "Tom" },
new Employee2 { ID = 2, Name = "Jerry", DeptName = "R & D" }
};
var dto = mapper.Map(employees);
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return $"{FirstName} {LastName}";
}
}
public class EmployeeDto
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
}
7 自定义映射
当源类型与目标类型名称不一致时,或者需要对源数据做一些转换时,可以用自定义映射。
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoinTime { get; set; }
}
public class EmployeeDto
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int JoinYear { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
Department 类中的属性名,直接跟 EmployeeDto 类中的属性名一致,则可以使用 IncludeMembers 方法指定。
9 嵌套映射
有时,我们可能不需要展平。看如下例子:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public string Heads { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public DepartmentDto Department { get; set; }
}
public class DepartmentDto
{
public int ID { get; set; }
public string Name { get; set; }
}
我们要将 Employee 映射到 EmployeeDto,并且将 Department 映射到 DepartmentDto。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap();
cfg.CreateMap();
});
//关键字的使用探讨/*访问关键词private 只能在本类中访问public 只能在本工程中访问protected 只能在包中和子类中访问默认的 只能在包中访问*//*final 类 方法 变量 final 类 不能被继承 final 方法 不能被子类覆盖,但可以继承 final 变量 只能有一次赋值,赋值后不能改变 final 不能用来修饰构造方法*///this()
What’s new in Zabbix 2.0?
去年开始使用Zabbix的时候,是1.8.X的版本,今年Zabbix已经跨入了2.0的时代。看了2.0的release notes,和performance相关的有下面几个:
:: Performance improvements::Trigger related da
修改jboss端口
%JBOSS_HOME%\server\{服务实例名}\conf\bindingservice.beans\META-INF\bindings-jboss-beans.xml
中找到
<!-- The ports-default bindings are obtained by taking the base bindin
@echo off
::演示:删除指定路径下指定天数之前(以文件名中包含的日期字符串为准)的文件。
::如果演示结果无误,把del前面的echo去掉,即可实现真正删除。
::本例假设文件名中包含的日期字符串(比如:bak-2009-12-25.log)
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
rem 指定天数
set DaysAgo=1
HTML5的video和audio标签是用来在网页中加入视频和音频的标签,在支持html5的浏览器中不需要预先加载Adobe Flash浏览器插件就能轻松快速的播放视频和音频文件。而html5media.js可以在不支持html5的浏览器上使video和audio标签生效。 How to enable <video> and <audio> tags in