Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper in WebAPI Controller

I have a Car WebAPI controller method as below - note _carService.GetCarData returns a collection of CarDataDTO objects

[HttpGet]
[Route("api/Car/Retrieve/{carManufacturerID}/{year}")]
public IEnumerable<CarData> RetrieveTest(int carManufacturerID, int year)
{
    //Mapper.Map<>
    var cars = _carService.GetCarData(carManufacturerID, year);
    //var returnData = Mapper.Map<CarData, CarDataDTO>();
    return cars;
}

CarData is a WebAPI model I have created.

public class CarData
{
    public string Model { get; set; }
    public string Colour { get; set; }
    //other properties removed from brevity
}

CarDataDTO is a class I have created that models the DB Table - I retrieve the Data via a stored proc called with dapper.

public class CarDataDTO
{
    public int CarID { get; set; }
    public int CarManufacturerID { get; set; }
    public int Year { get; set; }
    public string Model { get; set; }
    public string Colour { get; set; }
    //other properties removed from brevity
}

If I have a breakpoint on the var cars line in my API controller I can see everything returned as expected and I have a collection of CarDTO objects. However, I don't require the WebAPI to return the CarDataID, CarID or Year which is why I created the CarData API model.

How can I easily use Automapper to only map the properties I am concerned with?

Do I need something set up in my WebApiConfig class?

like image 370
Ctrl_Alt_Defeat Avatar asked Mar 15 '17 14:03

Ctrl_Alt_Defeat


People also ask

When should I use AutoMapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is AutoMapper profile?

automapper Profiles Basic Profile Profiles permit the programmer to organize maps into classes, enhancing code readability and maintainability. Any number of profiles can be created, and added to one or more configurations as needed. Profiles can be used with both the static and instance-based APIs.

How do I use AutoMapper to list a map?

Once you have your types, and a reference to AutoMapper, you can create a map for the two types. Mapper. CreateMap<Order, OrderDto>(); The type on the left is the source type, and the type on the right is the destination type.


1 Answers

You could install the AutoMapper nuget package from: AutoMapper And then declare a class like:

public class AutoMapperConfig
{
    public static void Initialize()
    {
        Mapper.Initialize((config) =>
        {
            config.CreateMap<Source, Destination>().ReverseMap();
        });
    }
}

And then call this in your Global.asax like this:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AutoMapperConfig.Initialize();
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

And if you would like to ignore certain properties then you could do something like this:

Mapper.CreateMap<Source, Destination>()
  .ForMember(dest => dest.SomePropToIgnore, opt => opt.Ignore())

And the way you use this for mapping is:

DestinationType obj = Mapper.Map<SourceType, DestinationType>(sourceValueObject);
List<DestinationType> listObj = Mapper.Map<List<SourceType>, List<DestinationType>>(enumarableSourceValueObject);
like image 74
Jinish Avatar answered Sep 18 '22 05:09

Jinish