/// <summary> /// Creates a new object of type <typeparamref name="TObject"/> and /// adds interception as needed to match the policies specified in /// the default policy configuration. /// </summary> /// <typeparam name="TObject">Concrete object type to create.</typeparam> /// <typeparam name="TInterface">Type of reference to return. Must be an interface the object implements.</typeparam> /// <param name="args">Arguments to pass to the <typeparamref name="TObject"/> constructor.</param> /// <returns>The intercepted object (or possibly a raw instance if no policies apply).</returns> public static TInterface Create <TObject, TInterface>(params object[] args) where TObject : TInterface { using (var policyInjector = new PolicyInjector(EnterpriseLibraryContainer.Current)) { return(policyInjector.Create <TObject, TInterface>(args)); } }
/// <summary> /// Resets the policy injector for the static facade. /// </summary> /// <remarks> /// Used for tests. /// </remarks> public static void Reset() { var currentPolicyInjector = policyInjector; policyInjector = null; if (currentPolicyInjector != null) { currentPolicyInjector.Dispose(); } }
/// <summary> /// Sets the policy injector for the static facade. /// </summary> /// <param name="policyInjector">The policy injector.</param> /// <param name="throwIfSet"><see langword="true"/> to throw an exception if the policy injector is already set; otherwise, <see langword="false"/>. Defaults to <see langword="true"/>.</param> /// <exception cref="InvalidOperationException">The policy injector is already set and <paramref name="throwIfSet"/> is <see langword="true"/>.</exception> public static void SetPolicyInjector(PolicyInjector policyInjector, bool throwIfSet = true) { Guard.ArgumentNotNull(policyInjector, "policyInjector"); var currentPolicyInjector = PolicyInjection.policyInjector; if (currentPolicyInjector != null && throwIfSet) { throw new InvalidOperationException(Resources.ExceptionPolicyInjectorAlreadySet); } PolicyInjection.policyInjector = policyInjector; if (currentPolicyInjector != null) { currentPolicyInjector.Dispose(); } }
/// <summary> /// Creates a new object of type <typeparamref name="TObject"/> and /// adds interception as needed to match the policies specified in /// the policy configuration supplied in <paramref name="configurationSource"/>. /// </summary> /// <typeparam name="TObject">Concrete object type to create.</typeparam> /// <typeparam name="TInterface">Type of reference to return. Must be an interface the object implements.</typeparam> /// <param name="configurationSource"><see cref="IConfigurationSource"/> containing the policy configuration.</param> /// <param name="args">Arguments to pass to the <typeparamref name="TObject"/> constructor.</param> /// <returns>The intercepted object (or possibly a raw instance if no policies apply).</returns> public static TInterface Create <TObject, TInterface>(IConfigurationSource configurationSource, params object[] args) { PolicyInjector policyInjector = GetInjectorFromConfig(configurationSource); return(policyInjector.Create <TObject, TInterface>(args)); }
/// <summary> /// Creates a proxy for the given object that adds interception policies as /// defined in <paramref name="configurationSource"/>. /// </summary> /// <remarks> /// Despite the name of the <typeparamref name="TInterface"/> parameter, this /// may be any type that the instance is assignable to, including both interfaces /// that it implements and the concrete type of the object. /// </remarks> /// <typeparam name="TInterface">Type of the proxy to return.</typeparam> /// <param name="configurationSource"><see cref="IConfigurationSource"/> containing the policy configuration.</param> /// <param name="instance">Instance object to wrap.</param> /// <returns>The proxy for the instance, or the raw object if no policies apply.</returns> public static TInterface Wrap <TInterface>(IConfigurationSource configurationSource, object instance) { PolicyInjector policyInjector = GetInjectorFromConfig(configurationSource); return(policyInjector.Wrap <TInterface>(instance)); }