NInject Extension Factory

我的梦境 提交于 2019-12-06 05:16:46

I ended up just changing the bindings to normal bindings,

Bind<IMyFactory>().To<MyFactory>().InSingletonScope();

and it worked! My first thought was lol, but it makes sense as well.

With the ToFactory() binding it never ever used my implementation of the factory, it just generated one from the defined interface.

Now it uses my implementation. The factory is changed a bit: From newing up the kernel in the factory or injecting it in the constructor, now I inject IResolutionRoot which Get<T>(); my objects.

Here is the new code just for clarification.

class MyNinjectModule : NinjectModule {
    public override void Load() {
        ...
        Bind<IDeployEntityFactory>().To<DeployEntityfactory>().InSingletonScope();
        Bind<IDeployEntityContainer>().To<DeployEntityContainer>();
        ...
    }
}

public class DeployEntityFactory : IDeployEntityFactory
{
    private readonly IResolutionRoot _resolutionRoot;
    ...
    public DeployEntityFactory(..., IResolutionRoot resolutionRoot)
    {
        ...
        _resolutionRoot = resolutionRoot;
    }

    public T GetDeployEntity<T>()
    {
        return _resolutionRoot.Get<T>();
    }
}

If this is not the right way to do it, I hope somebody can shed light on it and notify me with the right way... I imagine @remogloor would know such a thing. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!