/// <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)); }
/// <summary>Initializes a new instance of the <see cref="Lifestyle"/> class.</summary> /// <param name="name">The user friendly name of this lifestyle.</param> /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null (Nothing in VB) /// or an empty string.</exception> protected Lifestyle(string name) { Requires.IsNotNullOrEmpty(name, nameof(name)); this.Name = name; this.IdentificationKey = new { Type = this.GetType(), Name = name }; }
/// <summary> /// Suppressing the supplied <see cref="DiagnosticType"/> for the given registration. /// </summary> /// <param name="type">The <see cref="DiagnosticType"/>.</param> /// <param name="justification">The justification of why the warning must be suppressed.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="justification"/> is a null /// reference.</exception> /// <exception cref="ArgumentException">Thrown when either <paramref name="justification"/> is an /// empty string or when <paramref name="type"/> is not a valid value of <see cref="DiagnosticType"/>. /// </exception> public void SuppressDiagnosticWarning(DiagnosticType type, string justification) { Requires.IsValidEnum(type, nameof(type)); Requires.IsNotNullOrEmpty(justification, nameof(justification)); if (this.suppressions is null) { this.suppressions = new HashSet <DiagnosticType>(); } this.suppressions.Add(type); }
/// <summary>Initializes a new instance of the <see cref="Lifestyle"/> class.</summary> /// <param name="name">The user friendly name of this lifestyle.</param> /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null (Nothing in VB) /// or an empty string.</exception> protected Lifestyle(string name) { Requires.IsNotNullOrEmpty(name, nameof(name)); this.Name = name; }
/// <summary>Initializes a new instance of the <see cref="Lifestyle"/> class.</summary> /// <param name="name">The user friendly name of this lifestyle.</param> /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null (Nothing in VB) /// or an empty string.</exception> protected Lifestyle(string name) { Requires.IsNotNullOrEmpty(name, "name"); this.name = name; }