Ninject +绑定通用库 [英] Ninject + Bind generic repository

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

问题描述

我试图绑定一个通用IRepository<>接口,通用的我和库LT;> - 但是它总是返回null

我曾尝试喜欢各种各样的事情:

<$p$p><$c$c>Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>));
绑定(typeof运算(IRepository&LT;&GT;))。为了(typeof运算(库&LT;&GT;));

但是,如果我通过在非泛型接口和类,那么它就像一个梦吗?


解决方案

 绑定(typeof运算(IRepository&LT;&GT;))至(typeof运算(库&LT;&GT;))。


这是结合开放式泛型正确的语法。

如果您要求IRepository&LT时收到空回;任何的>,则有可能是在C你没有共享$ C $的一个区域其他一些问题。

I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null?

I have tried various things like:

Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>)); 
Bind(typeof(IRepository<>)).To(typeof(Repository<>)); 

However if I pass in a non-generic interface and class then it works like a dream?

解决方案

Bind(typeof(IRepository<>)).To(typeof(Repository<>));

This is the correct syntax for binding an open generic.

If you are receiving null back when requesting IRepository< of whatever >, then there may be some other problem in an area of code you haven't shared.

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

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