Where should I put automapper code?
automapper, c#
Solution
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
Problem
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?