NInject Extension Factory

我是研究僧i 提交于 2019-12-07 22:42:55

问题


After reading the new documentation on NInject v3 and how to use the Factory Extension, apparently I still don't get it fully since my code throws exceptions all over the place...

I get this Exception, i could paste the whole thing if people would like that but i'll try and keep it short for now.

Error activating IDeployEntityContainer No matching bindings are available, and the type is not self-bindable.

Here is my code... The Ninject Bind Module class

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

The class which uses the factory

class DeployController : IDeployController {

    private readonly IDeployEntityFactory _entityFactory;

    public DeployController(..., IDeployEntityFactory entityFactory) {
        ...
    }

    public void Execute() {
       ...
       //I get the Exception on this line...
       _entityFactory.GetDeployEntity<IDeployEntityContainer>();
       ...
    }
}

Factory Interface

public interface IDeployEntityFactory
{
    T GetDeployEntity<T>();
}

The Factory Implementation

public class DeployEntityFactory : IDeployEntityFactory
{
    private readonly IResolutionRoot _resolutionRoot;

    public DeployEntityFactory(IResolutionRoot resolutionRoot)
    {
        _resolutionRoot = resolutionRoot;
    }

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

Behind the scenes Ninject will create a proxy that implements the specified factory interface and intercept all methods so that the proxy behaves like...

I understand that I don't have to actually create the implementation my self if i don't need to do something special/custom in the creation of objects inside the factory.

Source: http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/

EDIT1:

Just to make sure i leave you with every bit of information you need to see the problem, i'm adding the DeployEntityContainer class/Interface

public abstract class DeployEntityBase : IDeployEntity
{
    ...
    protected readonly IDeployEntityFactory _entityFactory;

    protected DeployEntityBase(..., IDeployEntityFactory entityFactory)
    {
        ...
        _entityFactory = entityFactory;
        ...
    }
    ...
}

public class DeployEntityContainer : DeployEntityBase, IDeployEntityContainer
{
    ...
    public DeployEntityContainer(..., IDeployEntityFactory entityFactory)
        : base(..., entityFactory)
    {
    }
}

回答1:


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. :)



来源:https://stackoverflow.com/questions/11207681/ninject-extension-factory

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