Explore new objects when parsing the Resolve Method of Castle Windsor, windsorresolve

Source: Internet
Author: User

Explore new objects when parsing the Resolve Method of Castle Windsor, windsorresolve

When the dependency injection framework Castle Windsor parses an instance from the container (that is, the Resolve method is called), the constructor of the object to be parsed is called to create an object and return it. The problem is: which constructor does it call?

  • Constructors without Parameters
  • Constructor with parameters but not dependent on Injection
  • Constructor with parameters and dependent Injection
  • There are multiple constructors with parameters dependent on injection.

With this problem, I wrote a test code.

 

Test 1:

There is only one no-argument constructor:

CtorTest class (use Windsor to parse this class in the console Program)

   public class CtorTest    {       public string Message { get; set; }        public CtorTest()        {            Message = $"The message is from {nameof(CtorTest)}";        }        public void ShowMessage()        {            Console.WriteLine(Message);        }    }

The Main code of the console is as follows:

class Program    {        static void Main(string[] args)        {            IWindsorContainer iocContainer = new WindsorContainer();            iocContainer.Register(Component.For<CtorTest>().ImplementedBy<CtorTest>().LifestyleSingleton());                        var instance = iocContainer.Resolve<CtorTest>();            instance.ShowMessage();        }    }

Test results (the default constructor and the non-argument constructor are of the same nature ):

 

Test 2

Only one constructor with parameters but not dependent on injection (No parameter constructor)

The CtorTest code is as follows:

 public string Message { get; set; }        public CtorTest(string message)        {            Message = $"The message is from {nameof(CtorTest)}";        }        public void ShowMessage()        {            Console.WriteLine(Message);        }    }

Test results, of course, throw an exception:

If the default value (for example, string message = "") is provided for this parameter, Resolve will call this constructor. If a non-argument constructor is added, Resolve will call the constructor with parameters, if you add a constructor with two default values, two parameters will be called, so the conclusion here is:FirstParameters with default values (multiple parameters first), AgainNo Parameter.

 

Test 3:

There is a constructor with parameters dependent on injection, and a non-parameter constructor, a constructor with two default parameters.

Add a Sub class:

    public class Sub    {        public string Message { get; set; }        public Sub()        {            Message = $"The message is from {nameof(Sub)}";        }    }

The Ctor class code is as follows:

  public class CtorTest    {       public string Message { get; set; }        public CtorTest()        {            Message = $"The message is from {nameof(CtorTest)}";        }

Public CtorTest (string message = "message1", string message2 = "message2 ")
{
Message = $ "The message is from {nameof (CtorTest)} and {message} and {message2 }";
}

public CtorTest(Sub sub)        {            Message = sub.Message;        }        public CtorTest(string message = "")        {            Message = $"The message is from {nameof(CtorTest)}";        }        public void ShowMessage()        {            Console.WriteLine(Message);        }    } 

Main:

Class Program {static void Main (string [] args) {IWindsorContainer iocContainer = new WindsorContainer (); iocContainer. register (Component. for <CtorTest> (). implementedBy <CtorTest> (). lifestyleSingleton ());
// Inject sub into the container iocContainer. Register (Component. For <Sub> (). ImplementedBy <Sub> (). LifestyleSingleton ());Var instance = iocContainer. Resolve <CtorTest> (); instance. ShowMessage ();}}

Test results:

It can be seen from the results that it is throughCreate an instance for a constructor with parameters (the parameter is dependency injection), even if there is a constructor with two parameters with default values.

 

Test 4

Two constructors with parameters dependent on Injection

Add a Sub2 class:

    public class Sub2    {        public string Message { get; set; }        public Sub2()        {            Message = $"The message is from {nameof(Sub2)}";        }    }

The Ctor class code is as follows:

Public class CtorTest {public string Message {get; set;} public CtorTest () {Message = $ "The message is from {nameof (CtorTest)}" ;}// note: I intentionally put this before the sub parameter constructor.Public CtorTest (Sub2 sub2) {Message = sub2.Message ;}Public CtorTest (Sub sub) {Message = sub. message;} public CtorTest (string message = "") {Message = $ "The message is from {nameof (CtorTest)}" ;}public void ShowMessage () {Console. writeLine (Message );}}

The Main class code is as follows:

Class Program {static void Main (string [] args) {IWindsorContainer iocContainer = new WindsorContainer (); iocContainer. register (Component. for <CtorTest> (). implementedBy <CtorTest> (). lifestyleSingleton ());
// Inject sub2 into the container. Note that I intentionally put sub2 in front of sub iocContainer. register (Component. for <Sub2> (). implementedBy <Sub2> (). lifestyleSingleton (); // inject sub into the container iocContainer. register (Component. for <Sub> (). implementedBy <Sub> (). lifestyleSingleton (); var instance = iocContainer. resolve <CtorTest> (); instance. showMessage ();}}

Test results:

Although I put both the constructor and registration of Sub2 in front of Sub, I finally called the constructor WITH Sub parameters. What is the order of Sub2?Modify the class name (for example, change Sub2 to the name before Sub sorting, for example, S, then the constructor of the S parameter will be called)

 

Test 5

There are two constructors with parameters dependent on injection.

In the CtorTest class

        public CtorTest(Sub2 sub2)        {            Message = sub2.Message;        }

Modify

        public CtorTest(Sub2 sub2,Sub sub)        {            Message = sub2.Message +Environment.NewLine + sub.Message;         }

Test results:

It calls the modified constructor, that is:It first calls the one with many parameters.

 

Final Summary:

Resolve first calls the constructor with a large number of parameters and the parameters are injected through dependencies. IfConstructor with the same number of parametersIf there are multiple constructors, the first one is called in the order of parameter type names (this name should be fully qualified and not tested). If such constructors do not exist, constructors with multiple parameters and default values are called first.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.