In Ninject 2.0, how do I have both a general binding and a binding for a specfic case?

300 Aufrufe
Direkt zur ersten ungelesenen Nachricht

walkerco...@gmail.com

ungelesen,
13.09.2010, 16:58:2413.09.10
an ninject
I posted this question to stack overflow (http://stackoverflow.com/
questions/3677755/in-ninject-2-0-how-do-i-have-both-a-general-binding-
and-a-binding-for-a-specfic) and haven't gotten any responses, so I
thought I would try here.

I have a situation where I want to dependency inject my user object,
but also place the current user in the IoC container. I want the
following lines to work:

kernel.Get<User>(); // Should return a new User()
kernel.Get<User>("Current"); // Should return the current user

One might think bindings like this would work:

Bind<User>().ToSelf();
Bind<User>().ToMethod(LoadCurrentUser).InRequestScope().Named("Current");

Of course, that gives:

Ninject.ActivationException: Error activating User
More than one matching bindings are available.
Activation path:
1) Request for User

Suggestions:
1) Ensure that you have defined a binding for User only once.

I understand the error since a Named binding does not restrict the
application of that binding, so both bindings apply. It seems clear
that I need to use the contextual bind with the .When*() methods but I
can't come up with any way to do that. I feel like there should be
when methods that detect whether a named instance is applied.
Something like:

// Not valid Ninject syntax
Bind<User>().ToSelf().WhenUnnamedRequested();
Bind<User>().ToMethod(LoadCurrentUser).WhenNamedRequested().InRequestScope().Named("Current");

I can't find any place on the IRequest interface or it's properties
that tells me the name requested. How do I do this?

Sean Chambers

ungelesen,
13.09.2010, 17:30:4213.09.10
an nin...@googlegroups.com
Perhaps you could use WhenInjectedInto to do selective injection here?

Sean

On Sep 13, 2010, at 4:58 PM, "Je...@WalkerCodeRanger.com" <walkerco...@gmail.com
> wrote:

> --
> 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
> .
>

Remo Gloor

ungelesen,
13.09.2010, 17:39:4213.09.10
an ninject
If you are accessing the user by calling Get on the kernel (which I
hope you do not) then give the first binding a name and access User
always by name. Of course there is a way to get the unnamed version if
you don't give a name in the request. But because I hardly recommend
not to do this, I do not list it here. If you sill want to do it this
way I'll tell you later.

If you are doing it the better and prefered way and inject the user to
the objects that require it as dependency there are two ways:
1. The easier one: Give the first binding a name and add a named
attribute to the parameters e.g.
ctor([Named("NewUser") IUser newUser, [Named("Current")] IUser
currentUser)
2. Or the prefered way to keep the implementation classes free of the
IoC framework: Specify custom attributes and add them to the
parameters e.g. ctor([NewUser] IUser newUser, [CurrentUser]IUser
currentUser). Change the Bindings to:
Bind<User>().ToSelf().WhenTargetHas<NewUserAttribute>();

Bind<User>().ToMethod(LoadCurrentUser).InRequestScope().WhenTargetHas<CurrentUserAttribute>();

On 13 Sep., 22:58, "J...@WalkerCodeRanger.com"
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten