Autofac and autofac Chinese documents

Source: Internet
Author: User

Autofac and autofac Chinese documents

From the available services in the container, select a constructor to create an object. This process is automatically assembled.

1. Select Constructor

By default, autofac will use a non-argument constructor to create an object. I slightly modified the Person class.

Public interface IPerson {void Self ();} public class Person: IPerson {IAnimal adopt; public string Name {get; set;} public int Age {get; set ;} public Person () {Console. writeLine ("No parameter constructor");} public Person (IAnimal MyPerson) {Console. writeLine ("a parameter constructor"); adopt = MyPerson;} public Person (string name, int age) {Console. writeLine ("Two Parameter constructors"); this. name = name; this. age = age;} public void Sel F () {Console. WriteLine ("My name is {0}. I'm {1} years old! ", This. Name, this. Age);} public void Say () {Console. WriteLine (" I adopted a small animal "); adopt. Say ();}}

However, you can also choose which constructor to use automatically by passing in parameters. You can also specify which constructor to use during registration.

builder.RegisterType<Person>();//-------------------------------------------------var person = container.Resolve<Person>();person.Self();var personA = container.Resolve(typeof(Person), new NamedParameter("name", "elvin"), new NamedParameter("age", 18)) as Person;personA.Self();

In this way, parameters are passed to control which constructor to call, which is implemented internally through reflection.

If UsingConstructor is used to specify the constructor to be used, the preceding non-argument method cannot be used. An error is returned.

Builder. registerType <Person> (). usingConstructor (typeof (string), typeof (int); // --------------------------------------------------- // var person = container. resolve <Person> (); in this way, an error is reported. // person. self (); var personA = container. resolve (typeof (Person), new NamedParameter ("name", "elvin"), new NamedParameter ("age", 18) as Person; personA. self ();

There is a picture and a truth.

 

2. Additional constructor Parameters

You can add constructor in two ways. One is to add the constructor at registration and the other is to add the constructor at Resolve, just like the personA in the above example.

Now let's talk about how to add it during registration.

builder.RegisterType<Person>().WithParameters(new NamedParameter[] { new NamedParameter("name", "sniper"), new NamedParameter("age", 20) });//-------------------------------------------------var person = container.Resolve<Person>(); person.Self();var personA = container.Resolve(typeof(Person), new NamedParameter("name", "elvin"), new NamedParameter("age", 18)) as Person;personA.Self();

At this time, I found that there is no argument writing and parameter writing, and no error will be reported. Moreover, when creating an instance, both constructors of the two parameters are called.

If at this time, I use a parameter to write

Var personB = container. resolve (typeof (Person), new NamedParameter ("MyPerson", new Dog () {Name = "Xiaohua"}) as Person; personB. self ();

At this time, we will find that autofac does not care about me at all, and we still use the previously agreed constructor. It's really naughty.

This parameter method is similar to the method used to register an instance. to answer my questions, I have to try it again.

Builder. registerInstance <Person> (new Person ("liang", 22); // --------------------------------------------------- var person = container. resolve <Person> (); person. self (); person. age + = 10; var personA = container. resolve (typeof (Person), new NamedParameter ("name", "elvin"), new NamedParameter ("age", 18) as Person; personA. self (); person. age + = 10; var personB = container. resolve (typeof (Person), new NamedParameter ("MyPerson", new Dog () {Name = "Xiaohua"}) as Person; personB. self ();

We can see from this that although the constructor conventions are given during registration, the method of registering an instance is similar to a singleton.

 

Iii. Automatic Assembly

Assembly scanning can automatically assemble, saving a lot of configuration work and repetitive work.

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();//-------------------------------------------------var person = container.Resolve<IPerson>();person.Self();var personA = container.Resolve<Person>();personA.Self();var animal = container.Resolve<IAnimal>();animal.Say();

Why is a parameter constructor called here? For this reason, I modified the Cat class and added two constructor functions, one without a parameter and the other with a string name, cat There is still a constructor without parameters.

Then I modified the constructor of the Person parameter and changed the parameter to string name. After running the command, I found that the constructor without parameters is called. From these phenomena, which affects the selection of constructors for autofac, which should be IAnimal. then I added a three-parameter constructor, Person (IAnimal myPerson, string name, int age), and found that no-parameter constructor was called.

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();//-------------------------------------------------var person = container.Resolve<IPerson>();person.Self();

I roughly looked at the source code.

In this method, the program will find whether there are container parameters based on the parameters. If so, it will choose to use the matching constructor. Here is a principle, the constructor that uses the most matched number, but the constructor must have as few parameters as possible. is it a bit dizzy? Let's give an example.

Public Person (string name) {Console. writeLine ("A name constructor"); this. name = name;} public Person (IAnimal MyPerson) {Console. writeLine ("two parameters: IAnimal, IGo constructor"); adopt = MyPerson;} public Person (IAnimal MyPerson, IGo go) {Console. writeLine ("two parameters: IAnimal, IGo constructor"); adopt = MyPerson; this. go = go;} public Person (IAnimal myPerson, string name, int age) {Console. writeLine ("three parameter constructors"); this. name = name; this. age = age; adopt = myPerson;} public Person (IAnimal myPerson, IGo go, string name, int age) {Console. writeLine ("four parameter constructors"); this. name = name; this. age = age; adopt = myPerson; this. go = go ;}

OK. The automatic matching rule is already in use.

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.