/// <summary> /// Creates a custom lifestyle using the supplied <paramref name="lifestyleApplierFactory"/> delegate. /// </summary> /// <remarks> /// The supplied <paramref name="lifestyleApplierFactory" /> will be called just once per registered /// service. The supplied <paramref name="lifestyleApplierFactory" /> will be called by the framework /// when the type is resolved for the first time, and the framework will supply the factory with a /// <b>Func<object></b> for creating new (transient) instances of that type (that might /// have been <see cref="Container.ExpressionBuilding">intercepted</see> and /// <see cref="Container.RegisterInitializer{TService}">initializers</see> might have been applied). /// It is the job of the <paramref name="lifestyleApplierFactory" /> to return a <b>Func<object></b> /// that applies the proper caching. The <b>Func<object></b> that is returned by the /// <paramref name="lifestyleApplierFactory" /> will be stored for that registration (every /// registration will store its own <b>Func<object></b> delegate) and this delegate will be /// called every time the service is resolved (by calling /// <code>container.GetInstance<TService></code> or when that service is injected into another /// type). /// </remarks> /// <param name="name">The name of the lifestyle to create. The name is used to display the lifestyle /// in the debugger.</param> /// <param name="lifestyleApplierFactory">A factory delegate that takes a <b>Func<object></b> delegate /// that will produce a transient instance and returns a delegate that returns cached instances.</param> /// <returns>A new <see cref="Lifestyle"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null reference /// (Nothing in VB).</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is an empty string.</exception> /// <example> /// The following example shows the creation of a lifestyle that caches registered instances for 10 /// minutes: /// <code lang="cs"><![CDATA[ /// 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); /// ]]></code> /// </example> public static Lifestyle CreateCustom(string name, CreateLifestyleApplier lifestyleApplierFactory) { Requires.IsNotNullOrEmpty(name, nameof(name)); Requires.IsNotNull(lifestyleApplierFactory, nameof(lifestyleApplierFactory)); return(new CustomLifestyle(name, lifestyleApplierFactory)); }
public CustomRegistration(CreateLifestyleApplier lifestyleApplierFactory, Lifestyle lifestyle, Container container, Func <TImplementation> instanceCreator = null) : base(lifestyle, container) { this.lifestyleApplierFactory = lifestyleApplierFactory; this.instanceCreator = instanceCreator; }
public CustomRegistration( CreateLifestyleApplier lifestyleApplierFactory, Lifestyle lifestyle, Container container, Type implementationType, Func <object>?instanceCreator = null) : base(lifestyle, container, implementationType, instanceCreator) { this.lifestyleApplierFactory = lifestyleApplierFactory; }
public CustomLifestyle(string name, CreateLifestyleApplier lifestyleApplierFactory) : base(name) { this.lifestyleApplierFactory = lifestyleApplierFactory; }
public CustomRegistration(CreateLifestyleApplier lifestyleCreator, Lifestyle lifestyle, Container container) : base(lifestyleCreator, lifestyle, container) { }