You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a situation where my custom mapper needs logic that already exists in another class as it could be used outside of mapping. It would make sense if I could inject this dependency into the custom mapper class rather than new it up inside the mapper. An example might be if mapping a value in the destination depended on knowing information about the current user. I could new it up in the mapping class, but that would hinder testing. Has anyone come across that need and solved it in a currently available way?
Thanks!
The text was updated successfully, but these errors were encountered:
The main idea is to allow custom mapper classes to be instantiated by constructor dependency injection and then pass that instance to the mapping registration to use. Ideally this instance could have the context object set in it for use just as you would use the current custom mapping logic. This would allow for logic needed in the mapping to come from existing classes if that same logic is used elsewhere in the application. The DI container would most likely eschew the static mapper class in favor of creating a singleton dependency registration for the MappingServiceProvider to fully create the object map at application startup. This was a bit of a simplistic example, but I hope it helps to get my question/idea across to you.
Thanks for your time!
Class which takes a custom mapper instance as a dependency and registers a custom mapping instance with ExpressMapper.
public class AnimalToAnimalVmMapping
{
ICustomTypeMapper<Animal, AnimalVm> customMapper;
public AnimalToAnimalVmMapping(ICustomTypeMapper<Animal, AnimalVm> customMapper)
{
this.customMapper = customMapper;
}
/// <summary>
/// Registers the mapping.
/// </summary>
public void RegisterMapping()
{
Mapper.RegisterCustom<Animal, AnimalVm>(this.customMapper);
}
}
The custom mapper class
public class AnimalToAnimalVmCustomMapper : ICustomTypeMapper<Animal, AnimalVm>
{
ISoundLookupService lookupService;
public AnimalToAnimalVmCustomMapper (ISoundLookupService lookupService)
{
this.lookupService= lookupService;
}
/// <summary>
/// Custom mapping logic.
/// </summary>
public AnimalVm Map(IMappingContext<Animal, AnimalVm> context)
{
return new AnimalVm
{
// Use the injected mapping service to lookup the sound based on the source animal type.
Sound = this.lookupService.GetSound(context.Source.AnimalType),
// Other mapping here
};
}
}
I have a situation where my custom mapper needs logic that already exists in another class as it could be used outside of mapping. It would make sense if I could inject this dependency into the custom mapper class rather than new it up inside the mapper. An example might be if mapping a value in the destination depended on knowing information about the current user. I could new it up in the mapping class, but that would hinder testing. Has anyone come across that need and solved it in a currently available way?
Thanks!
The text was updated successfully, but these errors were encountered: