Ninject和绑定泛型 [英] Ninject and binding generics

查看:268
本文介绍了Ninject和绑定泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ninject



我有一个通用的DomainObject类定义如下所示:

  public abstract class DomainObject< T> :IDomainObject其中T:IDomainObject 
{
protected DomainObject(IDataProvider< T> dataProvider)
{
DataProvider = dataProvider;
}

//等等和blih

受保护的IDataProvider< T> DataProvider {get;私人设置; }



$ b

正如你在上面的代码中看到的那样, DomainObject 有一个表达对 IDataProvider< T> 的依赖关系的构造函数。



在我的Ninject模块中,这里是我如何执行绑定。
metaData是从配置存储检索的,并允许我指定要绑定的具体类型。

  var medaData = DataContextDictionary .Items [类型]; 
var genericDomainObjectType = typeof(DomainObject<>);
键入[] genericDomainObjectTypeArgs = {medaData.ObjectType};
var domainObjectType = genericDomainObjectType.MakeGenericType(genericDomainObjectTypeArgs);
绑定(domainObjectType).To(medaData.ObjectType);

var genericIDataProviderType = typeof(IDataProvider<>);
var iDataProviderType = genericIDataProviderType.MakeGenericType(genericDomainObjectTypeArgs);
绑定(iDataProviderType).To(medaData.DataProviderType);

这个效果很好,但是我觉得这个代码很有意思,可以用更好的方式写出来。



是否有更好的方式来表达Ninject的这种依赖关系?



感谢您的帮助。

解决方案

你想绑定开放的泛型版本还是仅仅基于'medaData '键入你的代码?



如果绑定开放类型是意图(或可接受),那么至少在Ninject 3中,你应该能够正常绑定它们',如下所示:

  Bind(typeof(IDataProvider<>))to(typeof(DataProvider<>)) ; 

如果你想坚持绑定特定的封闭类型,我不知道比你已经有了什么(除非你可以使用约定扩展名)。

Warning: I'm just starting to explore Ninject.

I have a generic DomainObject class defined as this:

public abstract class DomainObject<T> : IDomainObject where T : IDomainObject
{
    protected DomainObject(IDataProvider<T> dataProvider)
    {
        DataProvider = dataProvider;
    }

    // blah and blih

    protected IDataProvider<T> DataProvider { get; private set; }
}

As you can see in the code above, that DomainObject has a constructor expressing the dependency on a IDataProvider<T>.

In my Ninject module, here is how I do the bindings. metaData is retrieved from a configuration store and allows me to specify the concrete types to bind.

var medaData = DataContextDictionary.Items[type];
var genericDomainObjectType = typeof (DomainObject<>);
Type[] genericDomainObjectTypeArgs = { medaData.ObjectType };
var domainObjectType = genericDomainObjectType.MakeGenericType(genericDomainObjectTypeArgs);
Bind(domainObjectType).To(medaData.ObjectType);

var genericIDataProviderType = typeof (IDataProvider<>);
var iDataProviderType = genericIDataProviderType.MakeGenericType(genericDomainObjectTypeArgs);
Bind(iDataProviderType).To(medaData.DataProviderType);

This works well but I have the feeling this code is contrived and could be written in a better way.

Is there a better way to express such a dependency with Ninject?

Thanks for your help.

解决方案

Do you want to bind the open generic version or just the specific closed types based on the 'medaData' type in your code?

If binding the open types is the intent (or acceptable), then at least with Ninject 3, you should be able to bind them 'normally', like so:

Bind(typeof(IDataProvider<>)).To(typeof(DataProvider<>));

If you want to stick with binding specific closed types, I don't know of a better mechanism than what you have already (unless you can use the conventions extension).

这篇关于Ninject和绑定泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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