tags:

views:

56

answers:

4

Hi,

I noticed that the Ninject API has calls such as

Bind<ISomething>().DoSomethingElse();

How is this achieved?

+1  A: 

It's called Generics.

In your specific example, it is a Generic Method.

The idea is that you only have to write one generic method signature which you can use for any type (as long as you don't restrict it).

Take the generic List<T> for example. You can create it to hold any type: string, int, classes, structs, whichever you like. If it wasn't generic, you'd have to write specific code for all of the types it can contain, or everything would have to be boxed to an object, like with the ArrayList class, which comes at a performance penalty.

Razzie
+1  A: 

This is called method chaining. This achieved by building methods that are returing the same instances.

simple example

interface test
{
     test bind();
     test  DoSomethingElse();
}

class tester:test
{
    test bind()
    { 
         // blah 
    }

    test DoSomethingElse()
    { 
         // blah 
    }
}

tester obj = new tester()
obj.bind().DoSomethingElse()
Ramesh Vel
why this is downvoted???
Ramesh Vel
@Srinivas Reddy Thatiparthy, did you check the OP's exact question. he asked Bind().DoSomethingElse(); pls check out the initial version.......
Ramesh Vel
The question subject line asked about the `<>`, not what happens after it. (And for the record, wasn't my downvote.)
Jeffrey Hantin
+1  A: 

What's happening here is that Bind is a generic method which takes a single type parameter. It's signature would look like the following

public T Bind<T>() {
  ...
}

So calling Bind<ISomething>() calls the function with a bound type parameter of ISomething and returns an ISomething instance.

Note: I don't know if this is the actual signature of the function. Not familiar with Ninject in particular but speculating based on other APIs

JaredPar
A: 

It's a generic method defined on the BindingRoot class.

public IBindingToSyntax<T> Bind<T>() 
{
    ...
}
Darin Dimitrov