Where should I put the automapper code?

I am using Automapper in an asp.net mvc application. I have a question regarding the use of automapper

from a lot of code examples, I saw that people use the mapper Mapper.Map<Target>(source) directly in action, I'm not sure if this is a good prctice, from my point of view, I would like to wrap the Mapper code in a proxy object instead of letting him talk directly to the 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, the controller does not know anything about the Bankflow class, all it knows is dto BankflowData .

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

+2
source share
2 answers

If you have a service level in your application, it is better to place the auto-starter in the service level. anyway, try using the extension method to map your object using 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 separate your problems. this for more information

+3
source

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

In my answer, I explained:

[...] I have a typical structure:

  • MyProject.Core
  • MyProject.Domain
  • MyProject.DependencyInjection
  • MyProject.Infrastructure
  • Myproject.web
  • MyProject.Tests

The Infrastructure level contains information about registration, emailing, and data access. It will contain my ORM . This is not a business logic, not a UI. This is the railway of my decision so that everything is done. It is on the outer layer, but it only refers to Core.

In my case, the infrastructure layer also contains Automapper. The kernel defines a simple interface (say, IAutoMapper ), and a simple object that exists in the infrastructure implements it, and the object can be passed to the user interface level through dependency injection.

However, Jimmy Bogard (creator of Automapper) said in AutoMapper 3.0, Portable Class Libraries and PlatformNotSupportedException

[...] if you skim on user interface projects, you should not refer to this library directly because of some stupid idea of ​​an artificial architect (even referring to some smelly round vegetable), I will go to your house and spanking you is stupid. Get out of your tall horse and start to be productive.

From what I understand, it means it's okay to reference Automapper from the user interface layer. When he says β€œsome smelly round vegetable,” he certainly refers to β€œOnion Architecture,” which Jimmy is not a big fan of.

+3
source

Source: https://habr.com/ru/post/1212756/


All Articles