Autofac and autofac Chinese documents

Source: Internet
Author: User

Autofac and autofac Chinese documents

In the previous article, there was a small problem. Under an interface, multiple classes are registered and can be obtained normally. the previous method is not feasible. In the service, there is a way to implement it.

I. Service

1. Type-Basic Methods for describing services

This method was used in the previous article, but it was not mentioned.

builder.RegisterType<Dog>().As<IAnimal>();//-------------------------------------------------var animal = container.Resolve<IAnimal>();animal.Say();

In this way, you can connect the IAnimal type service to the Dog component. Through this service, you can create a Dog instance.

Services of different types are also useful in automatic assembly.

 

2. Alias

builder.RegisterType<Dog>().Named<IAnimal>("dog");builder.RegisterType<Cat>().Named<IAnimal>("cat");//-------------------------------------------------var dog = container.ResolveNamed<IAnimal>("dog");dog.Say();Console.WriteLine();var cat = container.ResolveNamed<IAnimal>("cat");cat.Say();

In this way, the previous functions can be implemented. register multiple classes under the same interface and obtain their entities normally.

The Named method is essentially the As () method called.

The alias method is somewhat broad. I can write anything here, without any restrictions (specifications). I do not know what is in it. It is not very convenient to use it.

Autofac also considers this, So it provides another method.

 

3. Key

First, create an enumerative that is understandable.

public enum AnimalEnum{     Dog,    Cat}

With this entity, you can know that under the IAnimal service, you can register those classes and obtain the entities of those classes.

builder.RegisterType<Dog>().Keyed<IAnimal>(AnimalEnum.Dog); builder.RegisterType<Cat>().Keyed<IAnimal>(AnimalEnum.Cat);//-------------------------------------------------var dog = container.ResolveKeyed<IAnimal>(AnimalEnum.Dog);dog.Say();Console.WriteLine();var cat = container.ResolveKeyed<IAnimal>(AnimalEnum.Cat);cat.Say();

The Keyed <> () method, like the Named <> () method, calls the As () method, but its value restrictions are somewhat different.

The result shows that the alias method is the same. however, there are advantages and disadvantages. Although you can clearly see the available classes, the scalability is worse. If you want to add a new class, you have to modify the AnimalEnum enumeration value.

In addition to the above two methods, there is also an Index acquisition method.

var iindex = container.Resolve<IIndex<string, IAnimal>>();var cat = iindex["cat"];cat.Say();var index = container.Resolve<IIndex<AnimalEnum, IAnimal>>();var dog = index[AnimalEnum.Dog];dog.Say();

The registration part is the same as before, but it is different from the previous one. if you do not know the specific value of "cat" in iindex ["cat"], the error rate will increase a lot. therefore, we recommend that you use the following Enumerated

 

4. traversal Mode

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();//-------------------------------------------------var list = container.ComponentRegistry.Registrations.ToList();var s = container.ResolveComponent(list[3], new List<Parameter>()) as Dog;s.Say();

Here, I first register the currently running assembly, and then get it through Registrations. All registered types should be traversed to find what you want,

Then, call the ResolveComponent method to obtain the instance. I am lazy here (someone is lazy). Haha. This method is not recommended.

Refer:

Autofac components, services, and automatic assembly Article 2

Autofac documentation

 

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.