标签:end youdao entity 商品 gis nbsp initial 添加 tran
数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据(访问和存取器)。
public class GoodsEntity//这是我们的实体类 { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public DateTime? CreateTime { get; set; } public bool IsDeleted { get; set; } public List<Items> Items { get; set; } public BrandsEntity Brands { get; set; } }
public class GoodsDto { public string GoodsName { get; set; } public decimal Price { get; set; } public string CreateTime { get; set; } public string Content { get; set; } public bool IsDeleted { get; set; } public List<Items> Items { get; set; } public string BrandName { get; set; } }
services.AddAutoMapper(typeof(Startup));
public class AutoMapperConfigs { public static MapperConfiguration RegisterMappings() { return new MapperConfiguration(cfg => { cfg.AddProfile(new GoodsProfile()); }); } }
public class GoodsProfile : Profile { // 添加你的实体映射关系. public GoodsProfile() { // GoodsEntity转GoodsDto. CreateMap<GoodsEntity, GoodsDto>() // 映射发生之前 // 映射之前统一处理 .BeforeMap((src, dest) => src.Price = src.Price + 10) // 默认赋值 .BeforeMap((src, dest) => src.CreateTime = src.CreateTime == null ? (new DateTime(2012, 12, 12)) : src.CreateTime) // 映射匹配 .ForMember(dest => dest.GoodsName, opt => opt.MapFrom(src => src.Name)) .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => ((DateTime)src.CreateTime).ToString("yyyy-MM-dd"))) // 匹配的过程中赋值 .ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price + 10)) // 忽略某个属性的映射 .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()) // 合并 .ForMember(dest => dest.GoodsName, opt => opt.MapFrom(src => src.Brands.Name +" "+ src.Name)) // 映射发生之后 .AfterMap((src, dest) => dest.GoodsName = dest.Price < 40 ? "N/A." : dest.GoodsName) .AfterMap((src, dest) => dest.Content = "数据传输对象(DTO)(DataTransfer Object)"); // 最简单的匹配,属性字段/类型等完全一致 // GoodsDto转GoodsEntity. CreateMap<GoodsDto, GoodsEntity>(); } }
public class Configuration { public static void Configure() { Mapper.Initialize(cfg => { cfg.AddProfile<Profiles.SourceProfile>(); cfg.AddProfile<Profiles.OrderProfile>(); cfg.AddProfile<Profiles.CalendarEventProfile>(); }); } }
标签:end youdao entity 商品 gis nbsp initial 添加 tran
原文地址:https://www.cnblogs.com/cg-ww/p/12736586.html