automapper

Mapping a model into a dto and 'include' only the key of child element

有些话、适合烂在心里 提交于 2020-01-26 03:14:48
问题 I have the following Project model: public class Project { [Key] public int ProjectID { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Screenshot> Screenshots { get; set; } } And I have the following Screenshot model: public class Screenshot { [Key] public int ScreenshotID { get; set; } public string ScreenshotName { get; set; } public byte[] ScreenshotContent { get; set; } public string ScreenshotContentType { get; set; } } As

AutoMapper(七)

倖福魔咒の 提交于 2020-01-26 02:35:01
返回总目录 Null值替换 如果源类型的成员链上的属性值为Null,Null值替换允许提供一个可替换的值。下面有两个类Person和PersonInfo类,都有一个属性Title(头衔),从Person映射到PersonInfo,如果Person的属性没有赋值,那么PersonInfo的对应属性值就用“屌丝”来替换。 namespace SeventhAutoMapper { class Person { public string Title { get; set; } } class PersonInfo { public string Title { get; set; } } class Program { static void Main(string[] args) { //映射 Mapper.CreateMap<Person, PersonInfo>() .ForMember(dest => dest.Title, opt => opt.NullSubstitute("屌丝"));//源属性如果为null,置为“屌丝” //执行映射 var personInfo = Mapper.Map<PersonInfo>(new Person());//源属性未赋值,故为null var personInfo2 = Mapper.Map<PersonInfo>(new

AutoMapper初步学习

落花浮王杯 提交于 2020-01-26 02:17:53
AutoMapper初步学习 AutoMapper 初学 入门 前言 在一个应用程序中,如果在两个不同的类型对象之间传输数据,通常我们会用DTOs(数据传输对象),View Models(视图模型),或者直接是一些从一个service或者Web API的一些请求或应答对象。一个常见的需要使用数据传输对象的情况是,我们想把属于一个对象的某些属性值赋值给另一个对象的某些属性值,但是问题是,这个两个对象可能并不是完全匹配的,比如,两者之间的属性类型,名称等等,是不一样的,或者我们只是想把一个对象的一部分属性值赋值给另一个对象。 手动映射 以往的方式,都是手动映射去一一对应需要的某些属性值,我们来看一下是如何处理的。 通过以下这个例子来直观感受这种方式,创建了以下三个类: public class Author {     public string Name { get ; set ; } } public class Book {     public string Title { get ; set ; }     public Author Author { get ; set ; } } public class BookViewModel {     public string Title { get ; set ; }     public string Author {

automapper true false to Y N and reverse

核能气质少年 提交于 2020-01-25 10:00:27
问题 We currently have a viewmodel (custom c# class ) which has two properties is_active and is_deleted which are bool. the entity class which is saved/retrieved by the db calls has those fields as string , as in the DB they are stored to 'Y' and 'N'. Currently we have this for the AutoMapper mapping: CreateMap<classOne, classTwo>() .ForPath(d => d.IS_ACTIVE, opt => opt.MapFrom(s => s.IS_ACTIVE == "Y" ? true : false)) .ForPath(d => d.IS_DELETED, opt => opt.MapFrom(s => s.IS_DELETED == "Y" ? true :

Mapping from Collection<T> to Collection<T> by convention

爷,独闯天下 提交于 2020-01-25 07:23:15
问题 Is it possible to map from a property of type Collection<T> to another property of type Collection<T> by convention without the need to define the mapping explicitly? class CollectionExample { public static void Example() { var config = new MapperConfiguration(cfg => { cfg.CreateMap<Foo, FooDto>() //.ForMember(dest => dest.Items, member => member.MapFrom(src => src.Items)) ; }); var mapper = config.CreateMapper(); var foo = new Foo() { Items = { new Foo(), new Foo(), new Foo() } }; var fooDto

ABP理论学习之数据传输对象(DTO)

我是研究僧i 提交于 2020-01-24 14:45:02
返回总目录 本篇目录 为何需要DTO 领域层抽象 数据隐藏 序列化和懒加载问题 DTO惯例和验证 DTO和实体的自动映射 使用特性和扩展方法进行映射 帮助接口 DTO用于 应用层 和 展现层 间的数据传输。 展现层调用具有DTO参数的 应用服务 方法,然后应用服务使用领域对象来执行一些特定的业务逻辑,最后返回给展现层一个DTO。因此,展现层完全独立于领域层。在一个理想的分层应用中,展现层不直接和领域对象打交道(仓储,实体...)。 为何需要DTO 为每个应用服务方法创建一个DTO起初可能被看作是一项乏味而又耗时的事情。但如果正确地使用它,那么DTOs可能会拯救你应用。为啥呢? 领域层抽象 DTO为展现层抽象领域对象提供了一种有效方式。这样,层与层之间就正确分离了。即使你想完全分离展现层,仍然可以使用已存在的应用层和领域层。相反,只要领域服务的契约(方法签名和DTOs)保持不变,即使重写领域层,完全改变数据库模式,实体和ORM框架,也不需要在展现层做任何改变。 数据隐藏 试想你有一个User实体,包含Id,Name,EmailAddress和Password字段。如果UserAppService的GetAllUsers()方法返回一个List ,即使你没有在屏幕上显示它,那么任何人也都能看到所有user的密码。它不是涉及安全的,而是与数据隐藏相关的。应用服务都应该返回给展现层需要的

AutoMapper Map to different type based on an enum?

心已入冬 提交于 2020-01-22 20:44:09
问题 I'm starting to implement AutoMapper, first I managed to integrate it with Castle.Windsor, which I'm already using. Now I have a Post entity which I want to map to either a LinkPostModel or an ImagePostModel . Both inherit from PostModel 1) This is what I have so far: public class PostModelFromPostEntityConverter : ITypeConverter<Post, PostModel> { private readonly IPostService postService; public PostModelFromPostEntityConverter(IPostService postService) { if (postService == null) { throw

AutoMapper Map to different type based on an enum?

穿精又带淫゛_ 提交于 2020-01-22 20:43:46
问题 I'm starting to implement AutoMapper, first I managed to integrate it with Castle.Windsor, which I'm already using. Now I have a Post entity which I want to map to either a LinkPostModel or an ImagePostModel . Both inherit from PostModel 1) This is what I have so far: public class PostModelFromPostEntityConverter : ITypeConverter<Post, PostModel> { private readonly IPostService postService; public PostModelFromPostEntityConverter(IPostService postService) { if (postService == null) { throw

How to scan and auto-configure profiles in AutoMapper?

给你一囗甜甜゛ 提交于 2020-01-20 14:21:40
问题 Is there any way to auto-configue Automapper to scan for all profiles in namespace/assembly? What I would like to do is to add mapping profiles to AutoMapper from given assembly filtered by given interface, something like Scan Conventions in StructureMap: public static void Configure() { ObjectFactory.Initialize(x => { // Scan Assembly x.Scan( scanner => { scanner.TheCallingAssembly(); scanner.Convention<MyCustomConvention>(); scanner.WithDefaultConventions(); }); // Add Registries x

.NetCore学习笔记:四、AutoMapper对象映射

拟墨画扇 提交于 2020-01-17 15:23:17
什么是AutoMapper? AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码。这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来为我们做? 我们来看看在.netcore3.1中怎样使用AutoMapper9.0。 1 public class BasicProfile : Profile, IProfile 2 { 3 public BasicProfile() 4 { 5 CreateMap<TestDto, Test>(); 6 CreateMap<Test, TestDto>(); 7 } 8 } Profile提供了一个命名的映射类,所有继承自Profile类的子类都是一个映射集合。这里我创建了一个BasicProfile继承Profile类。 CreateMap创建映射规则。 IProfile创建影射类的约束,表示继承自该接口的类为映射集合。 由于AutoMapper9.0中取消了自动创建影射规则的方法这里我们需要自己写一个: 1 public static class ServiceCollectionExtensions 2 { 3 /// <summary> 4 /// 自动创建映射 5 /// </summary> 6 /// <param name="services"></param>