/// <summary> /// Registers an <see cref="ResolveInterceptor"/> delegate that allows intercepting calls to /// <see cref="SimpleInjector.Container.GetInstance">GetInstance</see> and /// <see cref="InstanceProducer.GetInstance()"/>. /// </summary> /// <remarks> /// If multiple registered <see cref="ResolveInterceptor"/> instances must be applied, they will be /// applied/wrapped in the order of registration, i.e. the first registered interceptor will call the /// original instance producer delegate, the second interceptor will call the first interceptor, etc. /// The last registered interceptor will become the outermost method in the chain and will be called /// first. /// </remarks> /// <param name="interceptor">The <see cref="ResolveInterceptor"/> delegate to register.</param> /// <param name="predicate">The predicate that will be used to check whether the given delegate must /// be applied to a registration or not. The given predicate will be called once for each registration /// in the container.</param> /// <exception cref="ArgumentNullException"> /// Thrown when either the <paramref name="interceptor"/> or <paramref name="predicate"/> are /// null references. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered. /// </exception> /// <example> /// The following example shows the usage of the <see cref="RegisterResolveInterceptor" /> method: /// <code lang="cs"><![CDATA[ /// var container = new Container(); /// /// container.Options.RegisterResolveInterceptor((context, producer) => /// { /// object instance = producer.Invoke(); /// Console.WriteLine(instance.GetType().Name + " resolved for " + context.Producer.ServiceType.Name); /// return instance; /// }, /// context => context.Producer.ServiceType.Name.EndsWith("Controller")); /// /// container.Register<IHomeViewModel, HomeViewModel>(); /// container.Register<IUserRepository, SqlUserRepository>(); /// /// // This line will write "HomeViewModel resolved for IHomeViewModel" to the console. /// container.GetInstance<IHomeViewModel>(); /// ]]></code> /// </example> public void RegisterResolveInterceptor( ResolveInterceptor interceptor, Predicate <InitializationContext> predicate) { Requires.IsNotNull(interceptor, nameof(interceptor)); Requires.IsNotNull(predicate, nameof(predicate)); this.Container.ThrowWhenContainerIsLockedOrDisposed(); this.Container.RegisterResolveInterceptor(interceptor, predicate); }
public ContextualResolveInterceptor(ResolveInterceptor interceptor, Predicate <InitializationContext> predicate) { this.Interceptor = interceptor; this.Predicate = predicate; }
private static Func <object> ApplyResolveInterceptor(ResolveInterceptor interceptor, InitializationContext context, Func <object> wrappedProducer) { return(() => ThrowWhenResolveInterceptorReturnsNull(interceptor(context, wrappedProducer))); }
/// <summary> /// Registers an <see cref="ResolveInterceptor"/> delegate that allows intercepting calls to /// <see cref="SimpleInjector.Container.GetInstance">GetInstance</see> and /// <see cref="InstanceProducer.GetInstance()"/>. /// </summary> /// <remarks> /// If multiple registered <see cref="ResolveInterceptor"/> instances must be applied, they will be /// applied/wrapped in the order of registration, i.e. the first registered interceptor will call the /// original instance producer delegate, the second interceptor will call the first interceptor, etc. /// The last registered interceptor will become the outermost method in the chain and will be called /// first. /// </remarks> /// <param name="interceptor">The <see cref="ResolveInterceptor"/> delegate to register.</param> /// <param name="predicate">The predicate that will be used to check whether the given delegate must /// be applied to a registration or not. The given predicate will be called once for each registration /// in the container.</param> /// <exception cref="ArgumentNullException"> /// Thrown when either the <paramref name="interceptor"/> or <paramref name="predicate"/> are /// null references. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown when this container instance is locked and can not be altered. /// </exception> /// <example> /// The following example shows the usage of the <see cref="RegisterResolveInterceptor" /> method: /// <code lang="cs"><![CDATA[ /// var container = new Container(); /// /// container.Options.RegisterResolveInterceptor((context, producer) => /// { /// object instance = producer.Invoke(); /// Console.WriteLine(instance.GetType().Name + " resolved for " + context.Producer.ServiceType.Name); /// return instance; /// }, /// context => context.Producer.ServiceType.Name.EndsWith("Controller")); /// /// container.Register<IHomeViewModel, HomeViewModel>(); /// container.Register<IUserRepository, SqlUserRepository>(); /// /// // This line will write "HomeViewModel resolved for IHomeViewModel" to the console. /// container.GetInstance<IHomeViewModel>(); /// ]]></code> /// </example> public void RegisterResolveInterceptor(ResolveInterceptor interceptor, Predicate<InitializationContext> predicate) { Requires.IsNotNull(interceptor, nameof(interceptor)); Requires.IsNotNull(predicate, nameof(predicate)); this.Container.ThrowWhenContainerIsLocked(); this.Container.RegisterResolveInterceptor(interceptor, predicate); }