Ok, so here is what is going on.
I can get your code to run fine if I remove the .ctor :public Container(Dependency dep)
What is happening is that the heuristic is counting the parameters for which it has bindings, but ignores the .ctor arguments in its counting.
The .ctor argument is only used during activation instead of activation & selection.
Given this Container(Dependency dep) and Container(Dependency dep, string value) have the same score.
BUT!!
Ninject defaults to the first member with the highest score. If you reverse the declaration of the .ctors, your test will work fine:
Success:
public Container(Dependency dep, string value)
{
this.dep = dep;
this.value = value;
}
public Container(Dependency dep)
{
this.dep = dep;
}
Failure:
public Container(Dependency dep)
{
this.dep = dep;
}
public Container(Dependency dep, string value)
{
this.dep = dep;
this.value = value;
}
Does that make sense?
-Ian
On Apr 19, 2009 7:49am, Padcom <
pad...@gmail.com> wrote:
>
>
> Ok, now I've been mistaken. The fixture you provided works.
>
>
>
> What I've been trying to accomplish next (and that's the reason why I
>
> got a bit confused) is as follows:
>
>
>
> [TestFixture]
>
> public class CtorFixture {
>
> private class Dependency {
>
> }
>
> private class Container {
>
> private readonly Dependency dep;
>
> private readonly String value;
>
>
>
> public Container() {
>
> }
>
>
>
> public Container(Dependency dep) {
>
> this.dep = dep;
>
> }
>
>
>
> public Container(Dependency dep, String value) {
>
> this.dep = dep;
>
> this.value = value;
>
> }
>
> public Dependency Dep {
>
> get { return dep; }
>
> }
>
>
>
> public String Value {
>
> get { return value; }
>
> }
>
> }
>
>
>
> [Test]
>
> public void CtorWithResolvableDependenciesAndStringParamShouldBeUsed()
>
> {
>
> using (var kernel = new StandardKernel(new InlineModule(x => {
>
> x.Bind().ToSelf();
>
> x.Bind().ToSelf().WithConstructorArgument(
>
> "value", "Hello, world!");
>
> }), new AutoWiringModule())) {
>
> var container = kernel.Get();
>
> Assert.That(container.Dep, Is.Not.Null);
>
> Assert.That(container.Value, Is.EqualTo("Hello, world!"));
>
> }
>
> }
>
>
>
> [Test]
>
> public void CtorWithResolvableDependenciesShouldBeUsed() {
>
> using (var kernel = new StandardKernel(new InlineModule(x => {
>
> x.Bind().ToSelf();
>
> x.Bind().ToSelf();
>
> }), new AutoWiringModule())) {
>
> var container = kernel.Get();
>
> Assert.That(container.Dep, Is.Not.Null);
>
> }
>
> }
>
>
>
> [Test]
>
> public void DefaultCtorShouldBeUsed() {
>
> using (var kernel = new StandardKernel(new InlineModule(x => {
>
> x.Bind().ToSelf();
>
> x.Bind().ToSelf();
>
> }))) {
>
> var container = kernel.Get();