I would like to use automapper in order to transform dto to entities. I followed this article (https://www.infoworld.com/article/3192900/how-to-work-with-automapper-in-c.html) but couldn't figure it out where to put MapperConfiguration. Any help would be
great.
Here is my Business Services:
public async Task<GameConfirmResponse> GamePurchase(RequestDto requestDto)
{
using (var scope = new TransactionScope())
{
//Unique reference ID
var game = new GameRequest {referenceId = Guid.NewGuid().ToString()};
//Transform DTO into GameRequest for calling Razer Initiate
var gameRequest = Mapper.Map<RequestDto, GameRequest>(requestDto);
gameRequest = Utilities.CreateSignature(gameRequest, RequestType.Initiate);
//Insert request into database
_unitOfWork.GameRepository.Insert(gameRequest);
#region Call Razer initiate
var response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");
//Read response
var htmlResponse = await response.Content.ReadAsStringAsync();
var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);
//Adding response into database
_unitOfWork.GameResponseRepository.Insert(gameResponse);
if (gameResponse.initiationResultCode == "00")
{
}
#endregion
await _unitOfWork.SaveAsync();
var gameConfirmResponse = Mapper.Map<RequestDto, GameConfirmResponse>(requestDto);
scope.Complete();
return gameConfirmResponse;
}
}
Keep your friends close and your enemies even closer
I followed this article (https://www.infoworld.com/article/3192900/how-to-work-with-automapper-in-c.html) but couldn't figure it out where to put MapperConfiguration. Any help would be great.
As far as I know, if you want to use automapper instance API, you could create a class named AutoMapperConfig.cs.
Then you could add the Create the AutoMapper Configuration Class and add your mapping profile class here.
At last, you could directly call the AutoMapper Configuration's method to create the config and then using config.CreateMapper metho to create the mapper.
More details, you could refer to below codes:
AutoMapperConfig.cs
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCNormallIssue.Models;
namespace MVCNormallIssue.App_Start
{
public class AutoMapperConfig
{
//public static void config()
//{
// Mapper.Initialize( cfg => cfg.CreateMap<Author,AuthorDTO>());
//}
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
//way one
cfg.CreateMap<Author, AuthorDTO>();
//way two
cfg.AddProfile<AuthorMappingProfile>();
}
);
return config;
}
}
//way two
public class AuthorMappingProfile : Profile
{
public AuthorMappingProfile()
{
CreateMap<Author, AuthorDTO>().ReverseMap();
}
}
}
Controller:
using AutoMapper;
using MVCNormallIssue.App_Start;
using MVCNormallIssue.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCNormallIssue.Controllers
{
public class AutoMapperTestController : Controller
{
// GET: AutoMapperTest
public ActionResult Index()
{
//Obsolete
//Mapper.Initialize(x => x.CreateMap<Author, AuthorDTO>());
//For member used to customsize the mapper
// var config = new MapperConfiguration(cfg => { cfg.CreateMap<Author, AuthorDTO>().ForMember(destination => destination.NameAndAge, opts => opts.MapFrom(x => x.Name + "is" + x.Age)); });
var config = new AutoMapperConfig().Configure();
IMapper imapper = config.CreateMapper();
var source = new Author() { Id=1, Age="20", Name="Test1" };
//way one if the destination object not exists
var re = imapper.Map<Author, AuthorDTO>(source);
//way two if the destination object is existed
var des = new AuthorDTO();
imapper.Map(source, des);
return View();
}
}
}
Best Regards,
Brando
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Hi your answer save my life , I have searching for configuring the Autommaper in my Api for hours after finding your solution , I have a little Note to say , if I have more than one ActionResult I should in every Action use
var config =newAutoMapperConfig().Configure();IMapper imapper = config.CreateMapper();
That will be a repetition and a memory consumption so you can define the MapperConfiguration and the IMapper interface globaly , and use them in eache Action.
I have a question do you have a solution to inject the IMapper in the constructor of the controller (DI) I try a lot of example but I find this problem :
<div> <div>"An error occurred when trying to create a controller of type 'CustomersController'. Make sure that the controller has a parameterless public constructor.",</div> <div></div> <div>Thank you in advance
.</div> </div>
Member
527 Points
2728 Posts
Automapper 8.1.1 how to configure
Jun 30, 2019 09:02 PM|cenk1536|LINK
Hi,
I would like to use automapper in order to transform dto to entities. I followed this article (https://www.infoworld.com/article/3192900/how-to-work-with-automapper-in-c.html) but couldn't figure it out where to put MapperConfiguration. Any help would be great.
Here is my Business Services:
Star
9831 Points
3120 Posts
Re: Automapper 8.1.1 how to configure
Jul 01, 2019 06:01 AM|Brando ZWZ|LINK
Hi cenk1536,
As far as I know, if you want to use automapper instance API, you could create a class named AutoMapperConfig.cs.
Then you could add the Create the AutoMapper Configuration Class and add your mapping profile class here.
At last, you could directly call the AutoMapper Configuration's method to create the config and then using config.CreateMapper metho to create the mapper.
More details, you could refer to below codes:
AutoMapperConfig.cs
Controller:
Best Regards,
Brando
Member
527 Points
2728 Posts
Re: Automapper 8.1.1 how to configure
Jul 02, 2019 06:48 AM|cenk1536|LINK
Hi Brando,
Can I add multiple maps inside this?
I use mapping in business serive layer as follows. Should I create AutoMapperConfig class in this layer?
Member
527 Points
2728 Posts
Re: Automapper 8.1.1 how to configure
Jul 03, 2019 07:35 PM|cenk1536|LINK
How can I implement DI with Unity? Where to put configuration ?
None
0 Points
1 Post
Re: Automapper 8.1.1 how to configure
May 02, 2020 02:47 AM|Salimi.NET|LINK
Hi your answer save my life , I have searching for configuring the Autommaper in my Api for hours after finding your solution , I have a little Note to say , if I have more than one ActionResult I should in every Action use
var config = new AutoMapperConfig().Configure(); IMapper imapper = config.CreateMapper();
That will be a repetition and a memory consumption so you can define the MapperConfiguration and the IMapper interface globaly , and use them in eache Action.
I have a question do you have a solution to inject the IMapper in the constructor of the controller (DI) I try a lot of example but I find this problem :
<div> <div>"An error occurred when trying to create a controller of type 'CustomersController'. Make sure that the controller has a parameterless public constructor.",</div> <div></div> <div>Thank you in advance .</div> </div>