Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looking for a way to inject dependencies into custom type mappers #133

Open
soundoff opened this issue Mar 10, 2017 · 2 comments
Open

Looking for a way to inject dependencies into custom type mappers #133

soundoff opened this issue Mar 10, 2017 · 2 comments
Assignees
Labels
Milestone

Comments

@soundoff
Copy link

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!

@anisimovyuriy
Copy link
Member

Dear @soundoff ,

Could you please provide an example?

Thanks

@anisimovyuriy anisimovyuriy self-assigned this Mar 14, 2017
@anisimovyuriy anisimovyuriy added this to the 1.9.2 milestone Mar 14, 2017
@soundoff
Copy link
Author

Sure,

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
            };
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants