简体   繁体   English

我应该在哪里放置automapper代码?

[英]Where should I put automapper code?

I'm using Automapper in Asp.net mvc application. 我在Asp.net mvc应用程序中使用Automapper。 I have a question regard to the usage of automapper 我对automapper的用法有疑问

from lots of sample code, I saw people use mapper Mapper.Map<Target>(source) directly in action, I'm not sure if this is good prctice, in my point of view, I would like to wrap the Mapper code in the proxy object instead of let it talk with controller directly 从大量的示例代码中,我看到人们直接使用映射器Mapper.Map<Target>(source) ,我不确定这是不是很好,在我的观点中,我想将Mapper代码包装好代理对象而不是让它直接与controller

      public BankflowData CreateBankflowAdjustments(BankflowData addedBankFlow)
      {
         var bankflow = Mapper.Map<Bankflow>(addedBankFlow);
         var newBankflow = Underlying.CreateBankFlowAdjustments(bankflow);
         return Mapper.Map<BankflowData>(newBankflow);
      }

in this example, controller know nothing about Class Bankflow , all it know is the dto BankflowData . 在这个例子中,控制器对Class Bankflow ,它只知道dto BankflowData

I would like to know if this is a good practice for an application using AutoMapper? 我想知道这对于使用AutoMapper的应用程序是否是一个好习惯?

For a previous question, I answered ASP.NET MVC with service layer and repository layer, where should the interfaces be defined? 对于上一个问题,我回答了ASP.NET MVC的服务层和存储库层,应该在哪里定义接口?

In my answer, I explained: 在我的回答中,我解释说:

[...] I have a typical structure like this: [...]我有一个这样的典型结构:

  • MyProject.Core MyProject.Core
  • MyProject.Domain MyProject.Domain
  • MyProject.DependencyInjection MyProject.DependencyInjection
  • MyProject.Infrastructure MyProject.Infrastructure
  • MyProject.Web MyProject.Web
  • MyProject.Tests MyProject.Tests

The Infrastructure layer contains information about logging, emailing and data access. Infrastructure层包含有关日志记录,电子邮件和数据访问的信息。 It will contain my ORM of choice. 它将包含我选择的ORM It's not business-logic stuff and it's not UI stuff. 这不是商业逻辑的东西,也不是UI的东西。 It's the railroad of my solution to get things done. 这是我完成任务的解决方案的铁路。 It's on the outer layer but it only references the Core. 它位于外层,但仅引用Core。

In my case the infrastructure layer also houses Automapper. 就我而言,基础设施层也包含Automapper。 The core defines a simple interface (let's say IAutoMapper ) and a simple object that exists in the infrastructure implements it and the object can be passed to the UI layer through dependency injection. 核心定义了一个简单的接口(比如说IAutoMapper ),基础设施中存在的一个简单对象实现了它,并且可以通过依赖注入将对象传递给UI层。

However Jimmy Bogard (the creator of Automapper) said in AutoMapper 3.0, Portable Class Libraries and PlatformNotSupportedException 然而 Jimmy Bogard(Automapper的创建者)在AutoMapper 3.0,可移植类库和PlatformNotSupportedException中表示

[...] if you whine about UI projects shouldn't reference this library directly because of some silly faux architect-y reason (even referencing a certain smelly round vegetable), I will drive to your house and slap you silly. [...] 如果你抱怨UI项目不应该直接引用这个库,因为一些愚蠢的虚假建筑师的原因(甚至引用某种臭的圆形蔬菜),我会开车到你的房子,打你傻。 Get off your high horse and start being productive. 摆脱你的高马,开始富有成效。

From what I understand he means that it's ok to reference Automapper from the UI layer. 根据我的理解,他意味着可以从UI层引用Automapper。 When he says "a certain smelly round vegetable" he is, of course, referring to Onion Architecture which Jimmy is not a big fan of. 当他说“某种臭的圆形蔬菜”时,他当然指的是洋米建筑 ,吉米并不是它的忠实粉丝。

if you have service layer in your application, it's better to place the automapper in service layer. 如果您的应用程序中有服务层,最好将automapper放在服务层中。 in any case try to use extension method for mapping your object by automapper like this: 在任何情况下尝试使用扩展方法通过automapper映射您的对象,如下所示:

public static class Mapping
{
public static BankflowData CreateBankflowAdjustments(this BankflowData addedBankFlow)
      {
         var bankflow = Mapper.Map<Bankflow>(addedBankFlow);
         var newBankflow = Underlying.CreateBankFlowAdjustments(bankflow);
         return Mapper.Map<BankflowData>(newBankflow);
      }
}

it will make your code more readable and will separate your concerns. 它将使您的代码更具可读性,并将您的问题分开。 take this for more info 这个来获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM