- update lifestyles with example usages and information

master
Simon Gockner 7 years ago
parent 9629770ded
commit cdd87fdb71
  1. 26
      Lifestyles.md

@ -10,9 +10,33 @@ Using the `Transient`-Lifestyle, a new instance gets created every time an insta
:information_source: This is the default `Lifestyle` that the [`RegistrationFactory`](RegistrationFactory) uses. :information_source: This is the default `Lifestyle` that the [`RegistrationFactory`](RegistrationFactory) uses.
This is how you register a class as `Transient`:
```c#
container.Register(RegistrationFactory.Register<IFoo, Foo>());
```
## `Lifestyle.Singleton` ## `Lifestyle.Singleton`
The `Singleton`-Lifestyle is used to always get the same instance every time an instance is resolved. The `Singleton`-Lifestyle is used to always get the same instance every time an instance is resolved.
This is specifically useful if you have a service that needs to be accesses by multiple components of your application. `Singleton` instances will be created the first time they are requested and then reused whenever they are needed. They are bound to the `IocContainer` and will only be released once the `IocContainer` is disposed.
This is specifically useful if you have a service that needs to be accessed by multiple components of your application.
This is how you register a class as `Singleton`:
```c#
container.Register(RegistrationFactory.Register<IFoo, Foo>(Lifestyle.Singleton));
```
## `Lifestyle.Multiton` ## `Lifestyle.Multiton`
When using the `Multiton`-Lifestyle, a new instance gets created if the given scope has no created instance yet. Otherwise the already created instance is used.
`Multiton` instances will be created the first time they are requested for a given scope and then reused whenever requested for their scope.
They are also bound to the `IocContainer` but different to singletons, they can be cleared when you need to do so by calling `IocContainer.ClearMultitonInstances<T>()`.
They will also be released once the `IocContainer` is disposed.
This is how you register a class as `Multiton`:
```c#
container.Register(RegistrationFactory.Register<IFoo, Foo, TScope>());
```

Loading…
Cancel
Save