I'm really happy Ninject2 was released! Great work. I love this
framework and it keeps getting better. I don't mean to rain on
anyone's parade however ... but I have devised a unit test to make
explicit a bug in Ninject2 that makes it impossible for me to
implement. It has to do with getting a copy of the kernel within the
When() method of a binding. I do something like this with Ninject1
Bind<IService>().To<FlatFileService>().OnlyIf(context =>
context.Kernel.Get<Settings>().UseFlatFiles);
I do this a lot to conditionally bind implementations to services
depending on what some user entered as a setting. When I try to do
this in Ninject I get NREs because ParentContext is null.
Bind<IService>().To<FlatFileService>().When(request =>
request.ParentContext.Kernel.Get<Settings>().UseFlatFiles);
The test class is attached below (ugly, but does the trick ... I
really had no idea how to test the Bindings otherwise!!). I don't
want to be one of those whiners who gripes at open source authors
rather than contributes so I would really like to take a stab at
repairing it but, not being terribly familiar with the code base, I
wonder if someone could point me in the general direction? Heck, am I
even getting the IKernel in the correct way from an IRequest object??
public class WhenResolvingObject
{
protected readonly IKernel kernel = new StandardKernel();
[Fact]
public void ParentContextIsNotNull()
{
kernel.Bind<IWarrior>().To<Ninja>().When(request =>
{
request.ParentContext.ShouldNotBeNull();
return true;
});
var warrior = kernel.Get<IWarrior>();
warrior.ShouldNotBeNull();
warrior.ShouldNotBeInstanceOf<Ninja>();
}
}
Thanks!
--
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.
In my case having the object fixed initially shouldn't be a problem
since the Settings are Singletons anyway and they're usually the only
thing I use when conditionally resolving things.
What's your opinion on adding a Kernel property to the IRequest? I
might take a stab at it if you thought it advisable. It would be nice
to have a way to get at the IKernel that generated the request from
within the IRequest itself. I could do the same logic inside of a
provider since you do get an IContext with a provider, but I find too
many providers make for a smell and the logic I need is very simple
and make providers seem like overkill.
Thanks!
> > ninject+u...@googlegroups.com<ninject%2Bunsu...@googlegroups.com >