帮助我了解AutoMapper

帮助我了解AutoMapper

本文介绍了帮助我了解AutoMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是AutoMapper的地步?可能有人给我一个非常简单的例子吗?

What is the point of AutoMapper? Could someone give me a really simple example?

我看过它的视频,但它只是不点击。

I have watched the video on it, but it is just not clicking.

山姆

推荐答案

通常AutoMapper是在数据实体和商业模式之间的映射很大。例如,让说,例如,我有一个数据域类型,称之为 PersonEntity ,并且它具有属性的数组:性别等。我不想暴露我的数据域通过键入消费应用程序,所以我可以定义业务域类型,例如,现在类型可以包含其他业务逻辑,如 GetRelatedPersons 等,这些可能无法功能与您的数据域。在这种情况下,我有两种类型, PersonEntity 这在某些时候我会写样板code键的属性从一个到另一个复制。我可以这样做多种方式,我可以:

Typically AutoMapper is great at mapping between data entities and business models. E.g., lets say for instance, I have a data domain type, call it PersonEntity, and it has an array of properties: FirstName, Surname, Gender etc. I don't want to expose my data domain type through to the consuming application, so I can define business domain types, e.g. Person, now the Person type can contain additional business logic, such as GetRelatedPersons, etc, which may not be functionality tied to your data domain. In this case, I have two types, PersonEntity and Person which at some point I will have to write boilerplate code to copy the properties from one to the other. I could do this a variety of ways, I could:

1.复印构造器:

public Person(PersonEntity entity) {

2.Generalised映射方式:

2.Generalised Mapping Method:

public Person CreatePerson(PersonEntity entity) {

3.Implicit /显式转换:

3.Implicit/Explicit Conversion:

public static implicit operator Person(PersonEntity entity) {

但AutoMapper允许你这样做,是很容易地创建这两种类型之间的映射过程。因此,在其simiplist形式,我可以:

But what AutoMapper allows you to do, is easily create a mapping process between those two types. So in its simiplist form, I could:

Person person = Mapper.Map<PersonEntity, Person>(entity);

通过使用惯例,第一种方式,AutoMapper框架将翻译从一个实例到下一个匹配的属性。它也可以让你自定义如何,如果你想更精细的控制类型的映射。

By using a convention-first approach, the AutoMapper framework will translate matching properties from one instance to the next. It also allows you to customise how the types are mapped if you want more fine grained control.

希望有所帮助。

这篇关于帮助我了解AutoMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:01