question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do all mapping in one place with the new API changes?

See original GitHub issue

Before the change not to use the static API, I had just one static class with one method where I did all the mappings I needed. I called this method in Application_Start.

I also had an ‘IgnoreUnmapped’ extension which used Automapper’s static FindTypeMapFor method.

So, for example, I had: public static void Map() { Mapper.CreateMap<CustomerModel, CustomerDto>().ReverseMap(); Mapper.CreateMap<CustomerAddressModel, CustomerAddressDto>().ReverseMap(); Mapper.CreateMap<CustomerBankModel, CustomerBankDto>().ReverseMap() .IgnoreUnmapped(); Mapper.CreateMap<CustomerCourseModel, CustomerCourseDto>().ReverseMap() .ForMember(model => model.Field1, option => option.Ignore()) .ForMember(model => model.Field2, option => option.Ignore()); }

So now where am I going to place all my mapping?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jslattscommented, Mar 8, 2016

Create a mapping profile class (derived from Profile) and override Configure() with your mappings:

public class CustomProfile : Profile
{
  public CustomProfile() : base(nameof(CustomProfile)) { }

  protected override void Configure()
  {
    CreateMap<CustomerModel, CustomerDto>().ReverseMap(); 
    CreateMap<CustomerAddressModel, CustomerAddressDto>().ReverseMap(); 
    CreateMap<CustomerBankModel, CustomerBankDto>().ReverseMap().IgnoreUnmapped(); 
    CreateMap<CustomerCourseModel, CustomerCourseDto>().ReverseMap().ForMember(model => model.Field1, option => option.Ignore()).ForMember(model => model.Field2, option => option.Ignore());
  }
}

Then, depending on how your app is set up, instantiate it and use it. In my case, I inject it with Ninject.

var mapperConfig = new MapperConfiguration(config => config.AddProfile<CustomProfile>());
mapperConfig.AssertConfigurationIsValid();

// Ninject
kernel.Bind<IMapper>().ToConstant(mapperConfig.CreateMapper());

// Without ninject
var mapper = mapperConfig.CreateMapper();
CustomerDto dto = mapper.Map<CustomerModel>(model);

Not sure what to do about the extension. Maybe someone else can chime in.

0reactions
lock[bot]commented, May 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Digital Mapping Changes are Coming to HERE APIs and ...
Digital mapping changes are coming to HERE APIs and SDKs. What are they, and how will they benefit you? We explain.
Read more >
Versioning | Maps JavaScript API
The Maps JavaScript API team regularly updates the API with new features, bug fixes, and performance improvements. This page describes the ...
Read more >
FAQ for the new Google Maps API changes
Please see below for all the frequently asked questions relating to the new Google Maps API changes that comes into affect from the...
Read more >
What is Data Mapping? Definition and Examples
Data mapping is an essential part of data management that ensures data quality in integrations, migrations, and other data management tasks.
Read more >
Update mapping API | Elasticsearch Guide [8.9]
Update mapping APIedit. Adds new fields to an existing data stream or index. You can also use this API to change the search...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found