Newbie question

7 views
Skip to first unread message

Matthijs

unread,
Nov 24, 2009, 6:02:45 PM11/24/09
to ninject
Hello all,

I'm rather new in C# and IoC containers. I come from a java-oriented
world where I used the (IMO quite good) Spring framework which also
does dependency injection.
For a (hobby) project I'm doing in .Net I want to use Ninject as it
seems like a great framework with a lot of active users.
Unfortunately, the documentation in the wiki is for ninject1 and I
have just cloned and compiled ninject version 2 (seemed like the
better option). I'm having some trouble mapping the use case I have
with the ninject syntax:

I have 2 interfaces called IProductReader and IProductWriter. Then
there are 2 implementation classes: LuceneProductReaderWriter :
IProductReader, IProductWriter and MyProductReader : IProductReader.

Now, I want some classes to use the LuceneProductReaderWriter and
other classes to use MyProductReader. What is the best way to go about
this?
The XML extension test case (basic.xml) looks like it is using names
for specific bindings, but since I won't be using XML (for a change),
how do I do that with the C# syntax?

Also, I want to inject aspx pages with ninject. Is there any
documentation for the ninject.web extension?

Best regards,
Matthijs

Sean Chambers

unread,
Nov 24, 2009, 8:44:22 PM11/24/09
to nin...@googlegroups.com
Hello,

I've never used ninject in this manner, but I believe you can do this using the .Named() syntax:

Bind<IProductReader>().To<ProductReader>().Named("ProductReader");

then to retrieve:

Kernel.Get<IUserRepository>( "ProductReader", null );

this only works if you are using a service locator or the like to retrieve the instances.


Another option is to use the .WhenInjectedInto if you want to restrict it:

Bind<IProductReaderWriter>().To<ProductReaderWriter>().WhenInjectedInto<MyOtherService>();


Ian and others can correct me if i'm wrong, but at first glance something along those lines would probably work.

Sean


--

You received this message because you are subscribed to the Google Groups "ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ninject?hl=en.



Matthijs

unread,
Nov 27, 2009, 9:45:42 AM11/27/09
to ninject
Cool. The WhenInjectedInto syntax looks good for me. Thanks!
In spring dependency injection can work in a more push-like model, so
you can say "inject this (singleton) instance with this object, and
this other instance with another object". This way, specific strings
and lists and whatnot can be injected too. A pull-like model like
ninject is probably possible too. Philosophically, I can agree with
both models. In most cases you just need 'an' instance of the
interface. In specific cases however, you might want a specific
instance. Dependency injection is still the way to go, because the
class itself shouldn't need to care about what instance. Also,
ninject's module system gives a lot of flexibility too.

Thanks and best regards,
Matthijs

On 25 nov, 02:44, Sean Chambers <schamber...@gmail.com> wrote:
> Hello,
>
> I've never used ninject in this manner, but I believe you can do this using
> the .Named() syntax:
>
> Bind<IProductReader>().To<ProductReader>().Named("ProductReader");
>
> then to retrieve:
>
> Kernel.Get<IUserRepository>( "ProductReader", null );
>
> this only works if you are using a service locator or the like to retrieve
> the instances.
>
> Another option is to use the .WhenInjectedInto if you want to restrict it:
>
> Bind<IProductReaderWriter>().To<ProductReaderWriter>().WhenInjectedInto<MyOtherService>();
>
> Ian and others can correct me if i'm wrong, but at first glance something
> along those lines would probably work.
>
> Sean
>
> On Tue, Nov 24, 2009 at 6:02 PM, Matthijs <matthijs.wensv...@gmail.com>wrote:> Hello all,
> > ninject+u...@googlegroups.com<ninject%2Bunsu...@googlegroups.com>
> > .

Sean Chambers

unread,
Nov 27, 2009, 10:17:06 AM11/27/09
to nin...@googlegroups.com
In ninject you can add a ienumerable<imyservice> to where you are
injecting to and ninject will return all bindings that match that
interface.

I use this alot when using the strategy pattern with ninject and let
the consumer choose which implementation to use. This becomes very
flexible because to add another strategy I need only add another class
that implements that interface and then do the bindings automatically
with some linq and reflection.

Sean



On Nov 27, 2009, at 9:45 AM, Matthijs <matthijs...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages