Multiple constructors issue

24 views
Skip to first unread message

Germán Schuager

unread,
Apr 2, 2010, 8:32:34 PM4/2/10
to castle-project-users
Hi, I have the following class:

        public class Service
        {
            public ComponentX X { get; private set; }
            public ComponentY Y { get; private set; }
            public ComponentA A { get; private set; }

            public Service(ComponentA a)
            {
                A = a;
            }

            public Service(ComponentX x, ComponentY y)
            {
                X = x;
                Y = y;
            }
        }

and the following test:

        [Test]
        public void Kernel_should_select_ctor_with_available_dependencies()
        {
            var kernel = new DefaultKernel();
            kernel.AddComponent<ComponentX>();
            kernel.AddComponent<ComponentY>();
            kernel.AddComponent<Service>();

            var service = kernel.Resolve<Service>();

            Assert.That(service.A, Is.Null);
            Assert.That(service.X, Is.Not.Null);
            Assert.That(service.Y, Is.Not.Null);
        }

The test fails with this message:

TestCase 'Castle.MicroKernel.Tests.xxx.Kernel_should_select_ctor_with_available_dependencies'
failed: Castle.MicroKernel.Handlers.HandlerException : Can't create component 'Castle.MicroKernel.Tests.xxx+Service' as it has dependencies to be satisfied.
Castle.MicroKernel.Tests.xxx+Service is waiting for the following dependencies:

Services:
- Castle.MicroKernel.Tests.xxx+ComponentA which was not registered.
    Handlers\DefaultHandler.cs(114,0): at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
    Handlers\DefaultHandler.cs(53,0): at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean track)
    Handlers\AbstractHandler.cs(189,0): at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
    DefaultKernel.cs(936,0): at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments)
    DefaultKernel.cs(918,0): at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service)
    DefaultKernel_Resolve.cs(68,0): at Castle.MicroKernel.DefaultKernel.get_Item(Type service)
    DefaultKernel_Resolve.cs(199,0): at Castle.MicroKernel.DefaultKernel.Resolve(Type service)
    DefaultKernel_Resolve.cs(157,0): at Castle.MicroKernel.DefaultKernel.Resolve[T]()
    BestConstructorTestCase.cs(213,0): at Castle.MicroKernel.Tests.xxx.Kernel_should_select_ctor_with_available_dependencies()


Is this behavior by design?
I don't want to (cannot) register ComponentA in the kernel. How can I tell MK to use the second ctor?

Mauricio Scheffer

unread,
Apr 2, 2010, 9:43:13 PM4/2/10
to Castle Project Users

Germán Schuager

unread,
Apr 2, 2010, 10:56:03 PM4/2/10
to castle-pro...@googlegroups.com
Thanks Mauricio.
Now that test pass, but if you register Service before its dependencies it still fails.

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


Mauricio Scheffer

unread,
Apr 2, 2010, 11:37:39 PM4/2/10
to Castle Project Users
Now that's not so trivial to fix :)
Can you create an issue in the bug tracker? http://support.castleproject.org/
I created a branch on my fork with the failing test:
http://github.com/mausch/Castle.InversionOfControl/tree/multiple-constructor-bug

On Apr 2, 11:56 pm, Germán Schuager <gschua...@gmail.com> wrote:
> Thanks Mauricio.
> Now that test pass, but if you register Service before its dependencies it
> still fails.
>
> On Fri, Apr 2, 2010 at 10:43 PM, Mauricio Scheffer <
>
>
>

> mauricioschef...@gmail.com> wrote:
> > Fixed in
> >http://github.com/castleproject/Castle.InversionOfControl/commit/80bb...

> > castle-project-u...@googlegroups.com<castle-project-users%2Bun subs...@googlegroups.com>

Germán Schuager

unread,
Apr 2, 2010, 11:55:12 PM4/2/10
to castle-pro...@googlegroups.com

xtoff

unread,
Apr 3, 2010, 7:46:01 AM4/3/10
to Castle Project Users
Generally our handling of .ctors is broken. I started working on fix,
hopefully I'll have some free time after easter to finish it, and
commit the changes.

On Apr 3, 5:55 am, Germán Schuager <gschua...@gmail.com> wrote:
> Done:http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-198
>
> On Sat, Apr 3, 2010 at 12:37 AM, Mauricio Scheffer <
>
>
>
> mauricioschef...@gmail.com> wrote:
> > Now that's not so trivial to fix :)
> > Can you create an issue in the bug tracker?
> >http://support.castleproject.org/
> > I created a branch on my fork with the failing test:
>

> >http://github.com/mausch/Castle.InversionOfControl/tree/multiple-cons...

> > > > castle-project-u...@googlegroups.com<castle-project-users%2Bun subs...@googlegroups.com><castle-project-users%2Bun

Germán Schuager

unread,
Apr 3, 2010, 10:32:07 AM4/3/10
to castle-pro...@googlegroups.com
I have a relatively easy fix here: http://github.com/gschuager/Castle.InversionOfControl/commit/6b4e6d9220b6e84342b4f05d5c1f5619ac829e1f
Please review it.
Thanks

To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages