简体   繁体   中英

Ninject WithConstructorArgument not overriding global binding for type?

I have my general bindings, and one of which is this:

Bind<IHasher>().To<SHA256Hasher>();

This works great, as there are a few classes throughout which use this hasher. However there are one or two controllers which require another type of hasher for using the hash with 3rd parties, such as gravatar which expect md5 hashes. So then I do the below:

Bind<ISomeController>().To<SomeController>().Named("SomeController").WithConstructorArgument("emailHasher", new Md5Hasher());

Then my controller looks like:

    public class SomeController : Controller
    {
        private IHasher emailHasher;

        public CampaignController(IHasher emailHasher)
        {
            this.emailHasher = emailHasher;
        }
    }

When debugging I would expect the IHasher to be Md5Hasher, however it is an Sha256Hasher. So is this expected behaviour and any way to make it use the overridden type?

I suppose this is an MVC controller. The MVC Framework will resolve a SomeController not an ISomeController . Hence your binding does not apply but Ninject will use the implicit binding SomeController ToSelf instead.

Create a binding for SomeController ToSelf instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM