views:

79

answers:

2

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.ToProvider(System.Type)' has some invalid arguments "

Where Am I going Wrong?

+1  A: 

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)

Ruben Bartelink
A: 

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();
Jokiliku
@Jokiliku: (In case its not obvious, editing your question is a better way to do this to-fro and/or comments, but you dont have the rep yet (+1'd the question - should have done that in the first place), but I digress...). (Answering via edit....)
Ruben Bartelink
So, summarising based on my edit: first snippet matches 0 or n params, second matches exactly 1, third is same as second. On the fourth, `(IContext arg)` can be omitted as using delegate syntax, not lambda. On the fifth, the lambda has zero params, but no overloads of ToMethod match that. Hope this is clear. I know I've answered another question on SO which has far better discussion and examples of the difference between lambda and delegate syntax (my answer pointed out that the main case where one would favor lambda syntax is where you want to match 0 or n delegate params and ignore them)
Ruben Bartelink
Thanks RubenI did try to edit my question/add a comment but for some reason there was no way.Thanks for the clarification I will definitely checkout Jon Skeet's book. Starting to get my hands into C#.I do appreciate your help.
Jokiliku