Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c# - Where should I put automapper code?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...