The Unity framework provides several life cycle administrators with similarities and differences. It's hard to get dizzy when you first touch.
Make this small copy, just to help yourself to understand and remember. If you also use untiy, or can refer to see.
Words say:
- Transientlifetimemanager: A new object is created each time it is parsed, and the Unity container does not save the object's reference (that is, the container is not responsible for releasing the object). This control mode is generally called Transient.
- ContainerControlledLifetimeManager: Each time the parse is fixed, it returns the same existing object and releases the object when the container itself is destroyed. The effect of this life cycle pattern is equivalent to Singleton mode. This is also the default life cycle administrator when calling the RegisterInstance method.
- Perresolvelifetimemanager: Similar to Transientlifetimemanager, the main difference is that it reuses the same object during deep parsing, rather than creating new objects each time.
- Perthreadlifetimemanager: Basically, this life cycle is equal to the thread-wide Singleton. Its behavior is similar to ContainerControlledLifetimeManager, except that it depends on the thread to decide whether to create a new object. This life cycle administrator is not responsible for releasing objects.
- Externallycontrolledlifetimemanager: As with ContainerControlledLifetimeManager, it has a Singleton effect, but the main difference is that Externallycontrolledlifetimemanager is a weak reference to a saved object (weak reference). In this way, you can take full control of the time the object is disposed of, such as immediately calling its Dispose method when the object is exhausted to dispose of the object. Obviously, this life cycle administrator is not responsible for releasing objects.
- Hierarchicallifetimemanager: Used in a class container, that is, the container itself contains a sub-container of the occasion. Basically, this life cycle behavior equates to ContainerControlledLifetimeManager plus additional control over the child container: the sub-container has its own Singleton scope and does not share the same object with the parent container.
- Perrequestlifetimemanager: This life cycle administrator applies only to Web applications. It ensures that the same object is always returned when a type is resolved within the same HTTP request. Basically, it is a Singleton within the scope of an HTTP request.
Small copy: Choose Unity's Object Lifecycle Administrator