Where should I put automapper code?

2019-01-25 20:49发布

问题:

I'm using Automapper in Asp.net mvc application. I have a question regard to the usage of 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

      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.

I would like to know if this is a good practice for an application using AutoMapper?

回答1:

if you have service layer in your application, it's better to place the automapper in service layer. in any case try to use extension method for mapping your object by automapper like this:

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



回答2:

For a previous question, I answered ASP.NET MVC with service layer and repository layer, where should the interfaces be defined?

In my answer, I explained:

[...] I have a typical structure like this:

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

The Infrastructure layer contains information about logging, emailing and data access. It will contain my ORM of choice. It's not business-logic stuff and it's not UI stuff. It's the railroad of my solution to get things done. It's on the outer layer but it only references the Core.

In my case the infrastructure layer also houses 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.

However Jimmy Bogard (the creator of Automapper) said in AutoMapper 3.0, Portable Class Libraries and 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. 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. When he says "a certain smelly round vegetable" he is, of course, referring to Onion Architecture which Jimmy is not a big fan of.



标签: c# AutoMapper