Autofac is dependent Trapping

Dependent captivity (capture)

Conditions occur:

Another component is a short period of time occurs components held imprisoned dependent .

Autofac does not prevent you create imprisoned dependent, because it is the responsibility of the developer, sometimes because of the occurrence of captivity will get a parse exception.

1. How to avoid dependence captivity

General guidelines to avoid captivity relies on:

Use of the service life cycle of components is less than equal to the life cycle of services being used.

E.g:

There is a Web application, we decided to connect to a database where some of the information passed by request.

You may need the following components:

  • And accept the current request repository database connection factory
  • Similarly HttpContextthe Current Request , may be used to determine the business logic.
  • A database connection factory ( Database Connection Factory ), receiving a set of parameters and returns the correct database connection

In this example, we consider the following should be set for each component of how the life cycle, and the context of the current request is an obvious context, we can be the life cycle of the current component is set to each request for the same instance (Instance of Per Request) .

If we repositoryset "singleton" mode, that SingleInstancemode, then this example will be cached in the application's entire life cycle, the request context (request context)is passed repositoryin, and has been in the application lifecycle repositoryheld there are even times when the request is over, this time the repository is to use the components of the service, and HttpContextthat service is being used, repositoryyes, but it has held less life-cycle component of long-standing, which occurred in captivity rely.

If you also set the repository Instance Per Requestmodel, it exists now and as long as the current request lifecycle, which will be released at the same time, there is no captivity.

Further, we will set repository Instance Per Dependencythat is, every time you create a new instance of dependence, this is also possible, even shorter its life cycle than the current request, imprisoned phenomenon does not occur.

Database connection factory ( Database Connection Factory's ) thought process is similar, but also need to choose a different life cycle analysis modes according to the situation, such as a database connection factory instances of consuming resources, or you need to maintain some internal state it to work correctly, this time may need to use a singleton.

2. It should be decided by the developer is acceptable rely captivity

We in the actual development, should be decided according to whether it is acceptable rely imprisoned occur.

For example, you have a cache class (CacheClass), it is intentionally set to cache content only during the lifetime consumption component, if the component is a singleton, then the components can be used throughout the life of the program stored things, if is a Instance Per Requestmodel, then it can only store a single Web request data, select singleton better in this case, although the dependence imprisoned phenomenon, but as long as the developers to understand the consequences of such a set of survival, so it is acceptable to of.

In summary, we should deliberately let captivity phenomenon.

Personal blog Original Address: https://www.wukailiving.cn/autofac-zhong-de-yi-lai-qiu-jin-xian-xiang.html
please indicate the source Thank you!

Guess you like

Origin www.cnblogs.com/zaijianba/p/11802588.html