Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class with multiple constructors cannot be selected if parameter is not a primitive type #322

Closed
jonswaino opened this issue Jan 26, 2015 · 3 comments
Milestone

Comments

@jonswaino
Copy link

    public class IoC
        {
            public static IContainer Init()
            {
                var container = new Container(x =>
                {
                    x.Scan(s => {
                        s.TheCallingAssembly();
                        s.AssembliesFromApplicationBaseDirectory();
                        s.WithDefaultConventions();                   
                    });
                    x.For<IActivityService>().Singleton();

                    x.ForConcreteType<ClassWithTwoConstructors>()
                        .Configure.SelectConstructor(() => new ClassWithTwoConstructors(new AnotherDependency()))
                        .Ctor<AnotherDependency>("anotherDependency").Is(new AnotherDependency());

                });

                container.WhatDoIHave();
                return container;
            }
        }


 public class AnotherDependency
    {
        public AnotherDependency()
        {
        }
    }

    public class ClassWithTwoConstructors
    {


        public ClassWithTwoConstructors(int age, string name)
        {

        }

        public ClassWithTwoConstructors(AnotherDependency anotherDependency)        
        {

        }
    }

    public class MainService
    {
        public MainService(IStoryService storyService,
                           ClassWithTwoConstructors anotherDependency)
        {

        }
    }

The MainService constructor cannot be instantiated with the AnotherDependency parameter.

@jonswaino jonswaino changed the title Class with multiple param constructor cannot be selected if parameter is not a primitive type Class with multiple constructors cannot be selected if parameter is not a primitive type Jan 26, 2015
@jeremydmiller
Copy link
Contributor

@jonswaino I'm looking at this now.

@jeremydmiller jeremydmiller added this to the 3.1.5 milestone Jan 27, 2015
@jeremydmiller
Copy link
Contributor

@jonswaino,

It was a bit of an edge case with the constructor selection. My expression visitor was finding the inner constructor in this case. Without waiting for 3.1.5, you can work around it by:

            var container = new Container(x =>
            {
                x.Scan(s =>
                {
                    s.TheCallingAssembly();
                    s.AssembliesFromApplicationBaseDirectory();
                    s.WithDefaultConventions();
                });
                x.For<IActivityService>().Singleton();


                x.ForConcreteType<ClassWithTwoConstructors>()
                    .Configure.SelectConstructor(() => new ClassWithTwoConstructors(new AnotherDependency()))
                    .Ctor<AnotherDependency>().Is(anotherDependency);

                    // You don't need to specify the argument name if there is only 1 for that type
                    //.Ctor<AnotherDependency>("anotherDependency").Is(anotherDependency);

            });

@jonswaino
Copy link
Author

Ok great thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants