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.

IValueResolver with parameterized constructor

See original GitHub issue

I have written my own IValueResolver but I want it to have an interface injected at runtime from Ninject. How can I archive this?

Configure:

CreateMap<SourceEntity, DestModel>()
               .ForMember(dest => dest.IsOk, opt => opt.ResolveUsing<MyResolver>());

Resolver:

public class MyResolver : IValueResolver<SourceEntity, DestModel, bool>
        {
            private readonly IMyFacadeService _myFacadeService;

            public MyResolver (IMyFacadeService myFacadeService)
            {
                _myFacadeService= myFacadeService;
            }

            public bool Resolve(SourceEntity source, DestModel destination, bool destMember, ResolutionContext context)
            {
                var returnedBoolValue = Task.Run(async () => await _myFacadeService.CallSomeMethod(source)).Result;

                return returnedBoolValue;
            }
        }

I had a look at the documentation where it says “ConstructBy” should be possible, but I cannot get that to work. It says return of opt.ResolveUsing<CustomResolver>() is void so I cannot add .ConstructedBy

Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>()
        .ForMember(dest => dest.Total, 
            opt => opt.ResolveUsing<CustomResolver>().ConstructedBy(() => new CustomResolver())
        );

But even if this was possible. How to actually add the Ninject Kernal binding for IMyFacadeService ?

Regards,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
lisamarietcommented, Mar 28, 2017

I finally got it… here is the answer for anyone else looking, this is my ninject module for AutoMapper:

    public class AutoMapperModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IValueResolver<SourceEntity, DestModel, bool>>().To<MyResolver>();

            var mapperConfiguration = CreateConfiguration();
            Bind<MapperConfiguration>().ToConstant(mapperConfiguration).InSingletonScope();

            // This tells Ninject how to create automapper instances
            Bind<IMapper>().ToMethod(ctx => new Mapper(mapperConfiguration, type => ctx.Kernel.Get(type)));
        }

        private MapperConfiguration CreateConfiguration()
        {
            var config = new MapperConfiguration(cfg =>
            {
                // Add all profiles in current assembly
                cfg.AddProfiles(GetType().Assembly);
            });

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

Register ValueResolver with Parameterized Constructor ...
I have a custom IMemberValueResolver in my .NET Core 2.2 App that takes in a service and translates the source member to the...
Read more >
How to use a value resolver when resolving a ctor parameter
I have an entity which has no property setters but has a parameterized constructor: public class Unit { public int Id { get;...
Read more >
AutoMapper – passing parameter to custom resolver weird ...
There are ways to call Map that allow you to pass in values, so you don't need to try to finesse things with...
Read more >
Dependency Injection
InSingletonScope(); // This teaches Ninject how to create automapper instances say if for instance // MyResolver has a constructor with a parameter that ......
Read more >
Integrating AutoMapper with ASP.NET Core DI
When Injected into a constructor, I assign it to a private member by accessing mapperResolver.Mapper. Don't even have to call services.
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