Contextual Binding to class name

18 views
Skip to first unread message

webgio

unread,
Sep 4, 2008, 11:28:33 AM9/4/08
to ninject
Hi,
I would like to use Contextual Binding based on the type of the Target
instead of an attribute.
For Example I have something like:

public class Ninja {
[Inject] public IWeapon MeleeWeapon { get; set; }
}

public class SuperNinja {
[Inject] public IWeapon SuperWeapon { get; set; }
}

so that:

Bind<IWeapon>().To<Sword>();

would be used to bind to Sword by Ninja
and something like:

Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.Type.Name.EqualTo("SuperNinja")

would be used to bind to Shuriken by SuperNinja

...
Is there a way to do this?

thank you!

Giorgio

Nate Kohari

unread,
Sep 4, 2008, 11:41:52 AM9/4/08
to nin...@googlegroups.com
Giorgio:

This is one of those cases where there are several ways of doing the same thing. The easiest way to bind for members of a certain type is:

Bind<IWeapon>().To<Shuriken>().ForMembersOf<SuperNinja>();


-Nate

webgio

unread,
Sep 5, 2008, 3:19:58 AM9/5/08
to ninject
Hi Nate,
I get a NullReferenceException:

[NullReferenceException: Riferimento a un oggetto non impostato su
un'istanza di oggetto.]

Ninject.Core.Binding.StandardBinder.<Ninject.Core.Binding.Syntax.IBindingConditionSyntax.ForMembersOf>b__0(IContext
ctx) +27
Ninject.Core.Infrastructure.PredicateCondition`1.Matches(T value)
+12
Ninject.Core.Binding.StandardBinding.Matches(IContext context) +112

Ninject.Core.Binding.StandardBindingSelector.GetMatchingBindings(IContext
context, IEnumerable`1 candidates) +139
Ninject.Core.Binding.StandardBindingSelector.SelectBinding(Type
service, IContext context) +503
Ninject.Core.KernelBase.ResolveBinding(Type service, IContext
context) +381
Ninject.Core.KernelBase.ResolveInstance(Type service, IContext
context, Boolean isEagerActivation) +298
Ninject.Core.KernelBase.Get() +119

Am I missing something?

thanks,
Giorgio

On Sep 4, 5:41 pm, "Nate Kohari" <nkoh...@gmail.com> wrote:
> Giorgio:
> This is one of those cases where there are several ways of doing the same
> thing. The easiest way to bind for members of a certain type is:
>
> Bind<IWeapon>().To<Shuriken>().ForMembersOf<SuperNinja>();
>
> -Nate
>

Nate Kohari

unread,
Sep 5, 2008, 8:33:46 AM9/5/08
to nin...@googlegroups.com
Giorgio:

Interesting...are you using 1.0 or the latest Subversion build?


-Nate

Giorgio Bozio

unread,
Sep 5, 2008, 8:47:38 AM9/5/08
to nin...@googlegroups.com
1.0, should I go with the Subversion build?

2008/9/5 Nate Kohari <nko...@gmail.com>

Nate Kohari

unread,
Sep 5, 2008, 8:48:22 AM9/5/08
to nin...@googlegroups.com
If you could, try it with the latest build and let me know if you experience the same error.


Thanks,
Nate

Giorgio Bozio

unread,
Sep 5, 2008, 10:20:21 AM9/5/08
to nin...@googlegroups.com
Do I need visual studio 2008? I can't compile it on VS 2005... any way to download a setup of the trunk version?...
Thanks,
Giorgio

2008/9/5 Nate Kohari <nko...@gmail.com>

webgio

unread,
Sep 9, 2008, 6:01:10 AM9/9/08
to ninject
Sorry Nate, I'm still unable to solve the problem and I can't compile
the trunk version.
I'm getting similar exception if a conditionally bind to an attribute.
Any idea?
Thanks,
Giorgio

On Sep 5, 4:20 pm, "Giorgio Bozio" <giorgio.bo...@gmail.com> wrote:
> Do I need visual studio 2008? I can't compile it on VS 2005... any way to
> download a setup of the trunk version?...
> Thanks,
> Giorgio
>
> 2008/9/5 Nate Kohari <nkoh...@gmail.com>
>
> > If you could, try it with the latest build and let me know if you
> > experience the same error.
>
> > Thanks,
> > Nate
>
> > On Fri, Sep 5, 2008 at 8:47 AM, Giorgio Bozio <giorgio.bo...@gmail.com>wrote:
>
> >> 1.0, should I go with the Subversion build?
>
> >> 2008/9/5 Nate Kohari <nkoh...@gmail.com>
>
> >> Giorgio:
> >>> Interesting...are you using 1.0 or the latest Subversion build?
>
> >>> -Nate
>
Reply all
Reply to author
Forward
0 new messages