使用 Ninject 绑定 Nhibernate.Burrow 的 ISession

发布于 2024-09-13 05:24:56 字数 480 浏览 3 评论 0原文

我正在尝试使用 NHibenate、Burrow 和 Ninject。

我似乎无法使用 ninject 绑定 Burrow ISession。

我目前

Bind<ISession>().ToProvider( new BurrowFramework().GetSession()).InRequestScope();  

遇到错误

cannot convert from 'NHibernate.ISession' to 'System.Type'  

The best overloaded method match for 'Ninject.Syntax.IBindingToSyntax<NHibernate.ISession>.ToProvider(System.Type)' has some invalid arguments

我哪里错了?

I am trying use NHibenate, Burrow and Ninject.

I cannot seem to be able to bind the Burrow ISession using ninject.

I currently have

Bind<ISession>().ToProvider( new BurrowFramework().GetSession()).InRequestScope();  

I get the errors

cannot convert from 'NHibernate.ISession' to 'System.Type'  

The best overloaded method match for 'Ninject.Syntax.IBindingToSyntax<NHibernate.ISession>.ToProvider(System.Type)' has some invalid arguments

Where Am I going Wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2024-09-20 05:25:24

谢谢鲁本

你能帮我澄清一下吗
之间有什么区别

Bind<ISession>().ToMethod(delegate { return new BurrowFramework().GetSession(); }).InRequestScope();

Bind<ISession>().ToMethod(arg => new BurrowFramework().GetSession()).InRequestScope();

Bind<ISession>().ToMethod((IContext arg) => new BurrowFramework().GetSession()).InRequestScope();

我本来期望第一个看起来像

(delegate(IContext arg){ return new BurrowFramework().GetSession();}

,但它似乎有效,而这不起作用

Bind<ISession>().ToMethod( () => new BurrowFramework().GetSession()).InRequestScope();

Thanks Ruben

Would you clarify something for me
what is the difference between

Bind<ISession>().ToMethod(delegate { return new BurrowFramework().GetSession(); }).InRequestScope();

and

Bind<ISession>().ToMethod(arg => new BurrowFramework().GetSession()).InRequestScope();

or

Bind<ISession>().ToMethod((IContext arg) => new BurrowFramework().GetSession()).InRequestScope();

I would have expected the first one to look like

(delegate(IContext arg){ return new BurrowFramework().GetSession();}

but it appears to work while this does not

Bind<ISession>().ToMethod( () => new BurrowFramework().GetSession()).InRequestScope();
假装不在乎 2024-09-20 05:25:21

您可能想做:

Bind<ISession>().ToMethod( () => new BurrowFramework().GetSession()).InRequestScope();  

查看 Ninject 文档 - ToProvider 指的是 Ninject 提供程序要求的特定接口,它允许您干净地管理更复杂的工厂(而不是像上面那样几乎像内联工厂方法一样工作的东西) )。

编辑:我将您的评论解释为暗示您尝试过,但发现我在假设 ToMethod 重载的情况下搞砸了,其中委托没有参数,委托语法中的细微差别让您感到困惑。如果不是这样,我应该写:

Bind<ISession>().ToMethod( ctx => new BurrowFramework().GetSession()).InRequestScope();  

现在,C# 语法的快速总结:

在 C# 2 中,我们有如下匿名委托:

0) ToMethod( delegate {})...
1) ToMethod( delegate() {})...
2) ToMethod( delegate(x) {})...
3) ToMethod( delegate(X x) {})...
4) ToMethod( delegate(x, y) {})...
5) ToMethod( delegate(X x, Y y) {})...

在 C# 3 中,我们可以按如下方式创建 lambda:

1) ToMethod( () => {})...
2) Method( name => {})...
3) ToMethod( (X x) => {})...
4) ToMethod( (x, y) => {})...
5) ToMethod( (X x, Y y) => {})...

匹配任何类型的 0、1, 1 个 X 类型,2 个任何类型,一个 X 后跟一个 Y 分别

它们都是等价的 - 编译器为每个类型生成相同的输出。

不同之处在于 lambda 语法中没有与语法 0 等效的语法。

强烈推荐 Jon Skeet 的《C# in Depth》,它让所有这些内容变得清晰(但请等待几个月后的第二版)

(如果有时间给出更深入的答案,请查看 Ninject 源代码/API,看看是否它们在始终或从不传递上下文方面是一致的)

You probably want to do:

Bind<ISession>().ToMethod( () => new BurrowFramework().GetSession()).InRequestScope();  

Have a look at the Ninject docs - ToProvider refers to a specific interface that a Ninject provider mandates, which allows you to manage more complex factories cleanly (as opposed to stuff that works nearly as an inline factory method as above).

EDIT: I interpret your comment as implying that you tried it but discovered I'd messed up in assuming that there was an overload of ToMethod where the delegate has no parameters, which subtle differences in delegate syntax are confusing you with. If that's not the case, I should have written:

Bind<ISession>().ToMethod( ctx => new BurrowFramework().GetSession()).InRequestScope();  

Now, quick summary of C# syntax:

In C# 2, we had anonymous delegates as follows:

0) ToMethod( delegate {})...
1) ToMethod( delegate() {})...
2) ToMethod( delegate(x) {})...
3) ToMethod( delegate(X x) {})...
4) ToMethod( delegate(x, y) {})...
5) ToMethod( delegate(X x, Y y) {})...

In C# 3, we can create lambdas as follows:

1) ToMethod( () => {})...
2) Method( name => {})...
3) ToMethod( (X x) => {})...
4) ToMethod( (x, y) => {})...
5) ToMethod( (X x, Y y) => {})...

Which match zero, 1 of any type, 1 of type X, 2 of any type, an X followed by a Y respectively

They are all equivalent - the compiler generates the same output for each.

The difference is that there is no equivalent of syntax 0 in lambda syntax.

Highly recommend Jon Skeet's C# in Depth for making all this stuff clear (but wait for the second edition which is a short number of months away)

(If had time to give more in depth answer would look at the Ninject sources/APIs and see if they are consistent in always or never passing a context)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文