From cdd87fdb71ade41457b5a102eab5cf8f369237d6 Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Fri, 5 Jul 2019 14:23:27 +0200 Subject: [PATCH] - update lifestyles with example usages and information --- Lifestyles.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Lifestyles.md b/Lifestyles.md index 60a4418..f74329b 100644 --- a/Lifestyles.md +++ b/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. +This is how you register a class as `Transient`: + +```c# +container.Register(RegistrationFactory.Register()); +``` + ## `Lifestyle.Singleton` 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(Lifestyle.Singleton)); +``` ## `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()`. +They will also be released once the `IocContainer` is disposed. + +This is how you register a class as `Multiton`: + +```c# +container.Register(RegistrationFactory.Register()); +```