NInject扩建工厂 [英] NInject Extension Factory

查看:143
本文介绍了NInject扩建工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读上的新文件之后的 NInject V3 并如何使用的工厂扩展的,显然我还是不明白这一点完全因为我的代码引发异常遍地方...



我得到这个例外,我可以粘贴整个事情,如果人们会喜欢的,但我会尽量保持简短现在。




错误激活IDeployEntityContainer没有匹配的绑定是可用的,
和类型不自绑定


< /块引用>

下面是我的代码...
的Ninject绑定模块类

 类MyNinjectModule:NinjectModule {
公共覆盖无效负载(){
...
&绑定LT; IDeployEntityFactory>()ToFactory();
绑定&所述; IDeployEntityContainer方式>()到< DeployEntityContainer>();

}
}

的类,它使用工厂

 类DeployController:IDeployController {

私人只读IDeployEntityFactory _entityFactory;

公共DeployController(...,IDeployEntityFactory entityFactory){

}

公共无效执行(){
。 ..
//我得到这一行的异常...
_entityFactory.GetDeployEntity< IDeployEntityContainer>();

}
}



工厂接口

 公共接口IDeployEntityFactory 
{$ b $(b T)GetDeployEntity< T>();
}



工厂实现

 公共类DeployEntityFactory:IDeployEntityFactory 
{
私人只读IResolutionRoot _resolutionRoot;

公共DeployEntityFactory(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}

公共牛逼GetDeployEntity< T>()
{
返回_resolutionRoot.Get< T>();
}
}




幕后Ninject的背后将创建一个实现指定的工厂接口和拦截所有方法
的代理,以使
代理的行为像...




据我所知,我没有实际创建实现我自己,如果我不需要做在创建工厂中的对象的一些特殊/自定义的。



来源: HTTP://www.planetgeek ·CH / 2011/12/31 / ninject的扩展工厂介绍/



EDIT1:



只是为了确保我离开你的信息,每一点你需要看的问题,我加入DeployEntityContainer类/接口

 公共抽象类DeployEntityBase:IDeployEntity 
{
...
只读保护IDeployEntityFactory _entityFactory;

保护DeployEntityBase(...,IDeployEntityFactory entityFactory)
{
...
_entityFactory = entityFactory;

}

}

公共类DeployEntityContainer:DeployEntityBase,IDeployEntityContainer
{
..
公共DeployEntityContainer(...,IDeployEntityFactory entityFactory)
:基地(...,entityFactory)
{
}
}


解决方案

我最终只更改绑定到正常绑定,

 绑定< IMyFactory>()为<。MyFactory方式>()InSingletonScope(); 

和它的工作!我首先想到的是笑的,但它是有道理的为好。



随着 ToFactory()绑定它永远用我实施的工厂,它只是产生一个来自定义的接口。



现在它使用我的实现。工厂改了一下:从newing了在工厂内核或在构造函数中注入它,现在我注入 IResolutionRoot 其中获取LT; T> (); 我的对象



下面就是澄清新代码

 类MyNinjectModule:NinjectModule {
公共覆盖无效负载(){
...
绑定< IDeployEntityFactory>()为< DeployEntityfactory>( ).InSingletonScope();
绑定&所述; IDeployEntityContainer方式>()到< DeployEntityContainer>();

}
}

公共类DeployEntityFactory:IDeployEntityFactory
{
私人只读IResolutionRoot _resolutionRoot;
...
公共DeployEntityFactory(...,IResolutionRoot resolutionRoot)
{
...
_resolutionRoot = resolutionRoot;
}

公共牛逼GetDeployEntity< T>()
{
返回_resolutionRoot.Get< T>();
}
}

如果这不是做正确的方式,我希望有人可以在它揭示并用正确的方式通知我...我想象@remogloor会知道这样的事情。 :)


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)
    {
    }
}

解决方案

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

这篇关于NInject扩建工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆