Creates a custom lifestyle using the supplied lifestyleApplierFactory delegate.
Namespace: SimpleInjector
Assembly: SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax
public static Lifestyle CreateCustom( string name, CreateLifestyleApplier lifestyleApplierFactory )
Parameters
- name
- Type: SystemString
The name of the lifestyle to create. The name is used to display the lifestyle in the debugger. - lifestyleApplierFactory
- Type: SimpleInjectorCreateLifestyleApplier
A factory delegate that takes a Func<object> delegate that will produce a transient instance and returns a delegate that returns cached instances.
Return Value
Type: LifestyleA new Lifestyle.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | Thrown when one of the arguments is a null reference. |
ArgumentException | Thrown when name is an empty string. |
Remarks
container.GetInstance<TService>
Examples
C#
var customLifestyle = Lifestyle.CreateCustom("Absolute 10 Minute Expiration", instanceCreator => { TimeSpan timeout = TimeSpan.FromMinutes(10); var syncRoot = new object(); var expirationTime = DateTime.MinValue; object instance = null; // If the application has multiple registrations using this lifestyle, each registration // will get its own Func<object> delegate (created here) and therefore get its own set // of variables as defined above. return () => { lock (syncRoot) { if (expirationTime < DateTime.UtcNow) { instance = instanceCreator(); expirationTime = DateTime.UtcNow.Add(timeout); } return instance; } }; }); var container = new Container(); // We can reuse the created lifestyle for multiple registrations. container.Register<IService, MyService>(customLifestyle); container.Register<AnotherService, MeTwoService>(customLifestyle);
See Also