This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21356|Reply: 1

[Source] IoC efficient Autofac

[Copy link]
Posted on 9/13/2016 1:14:12 PM | | |
IoC ~ Efficient Autofac

There is no doubt that Microsoft's favorite IoC container is not spring.net, unity but Autofac, because of its efficiency, because of its simplicity, so it is also used for Microsoft-led orchard projects

illustrates the use of an Autofac.


Let's call it now:

In the next lecture, I will talk about the use of Autofac in specific projects for orchard projects.

Now look at its life cycle

1、InstancePerDependency

Create a new unique instance for each dependency or call. This is also the default way to create instances.

Official documentation explains: Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)

2、InstancePerLifetimeScope

In a lifecycle domain, each dependency or call creates a single shared instance, and for each different lifecycle domain, the instance is unique and not shared.

Official documentation explains: Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.

3、InstancePerMatchingLifetimeScope

In an identified lifecycle domain, each dependency or call creates a single shared instance. Instances in the parent domain can be shared in the child identity domain of the identified lifecycle domain. If no identified lifecycle domain is found in the entire inheritance hierarchy, an exception is thrown: DependencyResolutionException.

Official documentation explains: Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent's instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.

4、InstancePerOwned

In the lifecycle of an instance created by an instance owned in a lifecycle domain, each dependent component or call to the Resolve() method creates a single shared instance, and the child lifecycle domain shares the instances in the parent lifecycle domain. If no suitable lifecycle domain with child instances is found in the inheritance hierarchy, an exception is thrown: DependencyResolutionException.

The official documentation explains:

Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent's instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.

5、SingleInstance

Every time you depend on a component or call the Resolve() method, you get the same shared instance. In fact, it is a singleton model.

Official documentation explains: Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.

6、InstancePerHttpRequest

In the context of an Http request, share an instance of a component. Available for asp.net MVC development only.





Previous:MVC's ActionResult
Next:ConcurrentDictionary vs. Dictionary+Locking - Dennis Gao
 Landlord| Posted on 9/13/2016 1:15:50 PM |
Nuget installation method:

Install Autofac

Install-Package Autofac
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com