public void apply_policy_selectively_with_a_func()
        {
            var activator1 = new ActivatorInterceptor <ITarget>(x => x.Activate());
            var policy     = new InterceptorPolicy <ITarget>(activator1, i => i.Name.StartsWith("A"));

            var container = new Container(x =>
            {
                x.Policies.Interceptors(policy);
                x.For <ITarget>().AddInstances(targets =>
                {
                    targets.Type <ATarget>().Named("A");
                    targets.Type <ATarget>().Named("A1");
                    targets.Type <ATarget>().Named("A2");
                    targets.Type <ATarget>().Named("B");
                    targets.Type <ATarget>().Named("C");
                    targets.Type <ATarget>().Named("D");
                });
            });

            container.GetInstance <ITarget>("A").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("A1").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("A2").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("B").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance <ITarget>("C").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance <ITarget>("D").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
        }
        public void apply_policy_selectively_with_a_func()
        {
            var activator1 = new ActivatorInterceptor<ITarget>(x => x.Activate());
            var policy = new InterceptorPolicy<ITarget>(activator1, i => i.Name.StartsWith("A"));

            var container = new Container(x =>
            {
                x.Policies.Interceptors(policy);
                x.For<ITarget>().AddInstances(targets =>
                {
                    targets.Type<ATarget>().Named("A");
                    targets.Type<ATarget>().Named("A1");
                    targets.Type<ATarget>().Named("A2");
                    targets.Type<ATarget>().Named("B");
                    targets.Type<ATarget>().Named("C");
                    targets.Type<ATarget>().Named("D");
                });
            });

            container.GetInstance<ITarget>("A").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("A1").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("A2").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("B").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance<ITarget>("C").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance<ITarget>("D").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
        }
        public async Task TestThrottleAsync()
        {
            var taskTokens = new List <Task>();
            var result     = string.Empty;
            var task       = new Interceptor <string>(
                v => result = v,
                InterceptorPolicy.Throttle(TimeSpan.FromMilliseconds(100))
                );

            Assert.IsFalse(task.IsWorking);
            _ = task.InvokeAsync("opq");
            await Task.Delay(10);

            Assert.AreEqual("opq", result);
            _ = task.InvokeAsync("rst");
            await Task.Delay(10);

            Assert.AreEqual("opq", result);
            _ = task.InvokeAsync("uvw");
            await Task.Delay(110);

            Assert.AreEqual("opq", result);
            _ = task.InvokeAsync("xyz");
            Assert.AreEqual("xyz", result);
            await task.WaitAsync();

            Assert.IsFalse(task.IsWorking);
        }
        public void matches_exactly_on_plugin_type_if_the_interceptor_is_a_decorator()
        {
            var policy = new InterceptorPolicy<ITarget>(new FuncInterceptor<ITarget>(x => new DecoratedTarget(x)));
            policy.DetermineInterceptors(typeof(Target), new SmartInstance<Target>())
                .Any().ShouldBeFalse();

            policy.DetermineInterceptors(typeof(ITarget), new SmartInstance<Target>())
                .Any().ShouldBeTrue();
        }
        public void matches_exactly_on_plugin_type_if_the_interceptor_is_a_decorator()
        {
            var policy = new InterceptorPolicy <ITarget>(new FuncInterceptor <ITarget>(x => new DecoratedTarget(x)));

            policy.DetermineInterceptors(typeof(Target), new SmartInstance <Target>())
            .Any().ShouldBeFalse();

            policy.DetermineInterceptors(typeof(ITarget), new SmartInstance <Target>())
            .Any().ShouldBeTrue();
        }
Exemple #6
0
        /// <summary>
        /// Decorates all instances of TPluginType with the concrete type TDecoratorType
        /// </summary>
        public SmartInstance <TDecoratorType, TPluginType> DecorateAllWith <TDecoratorType>(Func <Instance, bool> filter = null)
            where TDecoratorType : TPluginType
        {
            var instance    = new SmartInstance <TDecoratorType, TPluginType>();
            var interceptor = new DecoratorInterceptor(typeof(TPluginType), instance);
            var policy      = new InterceptorPolicy <TPluginType>(interceptor, filter);

            alter = graph => graph.Policies.Add(policy);

            return(instance);
        }
        public void do_not_duplicate_interceptor_policies()
        {
            var theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
            var policy1 = new InterceptorPolicy<ITarget>(theActivator);
            var policy2 = new InterceptorPolicy<ITarget>(theActivator);

            policy1.ShouldEqual(policy2);

            var policies = new InterceptorPolicies();
            policies.Add(policy1);
            policies.Add(policy2);
            policies.Add(policy1);
            policies.Add(policy2);

            policies.Policies.Single().ShouldBeTheSameAs(policy1);
        }
        public async Task TestMutlipleHitsAsync()
        {
            var taskTokens = new List <Task>();
            var result     = string.Empty;
            var task       = new Interceptor <string>(
                v => result = v,
                InterceptorPolicy.Mutliple(2, 4, TimeSpan.FromMilliseconds(100))
                );

            Assert.IsFalse(task.IsWorking);
            _ = task.InvokeAsync("abc");
            await Task.Delay(20);

            Assert.AreEqual(string.Empty, result);
            _ = task.InvokeAsync("defg");
            await Task.Delay(20);

            Assert.AreEqual("defg", result);
            _ = task.InvokeAsync("hijk");
            await Task.Delay(20);

            Assert.AreEqual("hijk", result);
            _ = task.InvokeAsync("lmn");
            await Task.Delay(20);

            Assert.AreEqual("lmn", result);
            _ = task.InvokeAsync("opq");
            await Task.Delay(110);

            Assert.AreEqual("lmn", result);
            _ = task.InvokeAsync("rst");
            await Task.Delay(20);

            Assert.AreEqual("lmn", result);
            _ = task.InvokeAsync("uvw");
            await Task.Delay(20);

            Assert.AreEqual("uvw", result);
            _ = task.InvokeAsync("xyz");
            await Task.Delay(20);

            Assert.AreEqual("xyz", result);
            await task.WaitAsync();

            Assert.IsFalse(task.IsWorking);
        }
        public void do_not_duplicate_interceptor_policies()
        {
            var theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
            var policy1      = new InterceptorPolicy <ITarget>(theActivator);
            var policy2      = new InterceptorPolicy <ITarget>(theActivator);

            policy1.ShouldBe(policy2);

            var policies = Policies.Default();

            policies.Add(policy1);
            policies.Add(policy2);
            policies.Add(policy1);
            policies.Add(policy2);

            policies.Interception().Single().ShouldBeTheSameAs(policy1);
        }
        public async Task TestDebounceAsync()
        {
            var result = string.Empty;
            var task   = new Interceptor <string>(
                v => result = v,
                InterceptorPolicy.Debounce(TimeSpan.FromMilliseconds(100))
                );

            Assert.IsFalse(task.IsWorking);
            _ = task.InvokeAsync("abc");
            Assert.IsTrue(task.IsWorking);
            await Task.Delay(20);

            Assert.AreEqual(string.Empty, result);
            _ = task.InvokeAsync("defg");
            await Task.Delay(20);

            Assert.AreEqual(string.Empty, result);
            _ = task.InvokeAsync("hijk");
            await Task.Delay(20);

            Assert.AreEqual(string.Empty, result);
            _ = task.InvokeAsync("lmn");
            await Task.Delay(20);

            Assert.AreEqual(string.Empty, result);
            await Task.Delay(90);

            if (string.IsNullOrEmpty(result))
            {
                await Task.Delay(20);
            }
            Assert.AreEqual("lmn", result);
            await task.WaitAsync();

            Assert.IsFalse(task.IsWorking);
        }
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 public Interceptor(Action <T> action, InterceptorPolicy policy)
     : base(action, policy)
 {
 }
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <param name="doNotWait">true if do not wait for the action result; otherwise, false.</param>
 public Interceptor(Func <T, Task> action, InterceptorPolicy policy, bool doNotWait = false)
     : base(action, policy, doNotWait)
 {
 }
Exemple #13
0
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <typeparam name="T1">The type of action handler argument 1.</typeparam>
 /// <typeparam name="T2">The type of action handler argument 2.</typeparam>
 /// <typeparam name="T3">The type of action handler argument 3.</typeparam>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Action <T1, T2, T3> Action <T1, T2, T3>(Action <T1, T2, T3> action, InterceptorPolicy policy)
 => new Interceptor <T1, T2, T3>(action, policy).InvokeBegin;
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="policy">The interceptor policy.</param>
 public Interceptor(InterceptorPolicy policy)
     : base(policy)
 {
 }
Exemple #15
0
            /// <summary>
            /// Intercept the object being created and potentially replace it with a wrapped
            /// version or another object. This will apply to every registration where the service
            /// type is T or the implementation type could be cast to T
            /// </summary>
            /// <param name="interceptor"></param>
            /// <returns></returns>
            public void InterceptAll(Func <IServiceContext, T, T> interceptor)
            {
                var policy = new InterceptorPolicy <T>(interceptor);

                _parent.Policies.Add(policy);
            }
 public void SetUp()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
     thePolicy = new InterceptorPolicy<ITarget>(theActivator);
 }
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <param name="doNotWait">true if do not wait for the action result; otherwise, false.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Func <T, Task> Action(Func <T, Task> action, InterceptorPolicy policy, bool doNotWait = false)
 => new Interceptor <T>(action, policy, doNotWait).InvokeAsync;
Exemple #18
0
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <typeparam name="T1">The type of action handler argument 1.</typeparam>
 /// <typeparam name="T2">The type of action handler argument 2.</typeparam>
 /// <typeparam name="T3">The type of action handler argument 3.</typeparam>
 /// <typeparam name="T4">The type of action handler argument 4.</typeparam>
 /// <typeparam name="T5">The type of action handler argument 5.</typeparam>
 /// <typeparam name="T6">The type of action handler argument 6.</typeparam>
 /// <typeparam name="T7">The type of action handler argument 7.</typeparam>
 /// <typeparam name="T8">The type of action handler argument 8.</typeparam>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <param name="doNotWait">true if do not wait for the action result; otherwise, false.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Func <T1, T2, T3, T4, T5, T6, T7, T8, Task> Action <T1, T2, T3, T4, T5, T6, T7, T8>(Func <T1, T2, T3, T4, T5, T6, T7, T8, Task> action, InterceptorPolicy policy, bool doNotWait = false)
 => new Interceptor <T1, T2, T3, T4, T5, T6, T7, T8>(action, policy, doNotWait).InvokeAsync;
 /// <summary>
 /// Creates a multi-hit interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="min">The minmum invoking count.</param>
 /// <param name="max">The maxmum invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remark>
 /// The handler to process for the specific times and it will be reset after a while.
 /// </remark>
 public static Action <T> Mutliple(Action <T> action, int min, int?max, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Mutliple(min, max, timeout));
 /// <summary>
 /// Creates an interceptor policy responded at a specific times.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="min">The minmum invoking count.</param>
 /// <param name="max">The maxmum invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// A handler to process at last only when request to call in the specific times range.
 /// A sample scenario is double click.
 /// </remarks>
 public static Action <T> Times(Action <T> action, int min, int?max, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Times(min, max, timeout));
 /// <summary>
 /// Creates a throttle interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="duration">The duration.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// You may want to request to call an action only once in a short time
 /// even if you request to call several times.
 /// The rest will be ignored.
 /// So the handler will be frozen for a while after it has processed.
 /// </remarks>
 public static Action <T> Throttle(Action <T> action, TimeSpan duration)
 => Action(action, InterceptorPolicy.Throttle(duration));
 /// <summary>
 /// Creates a debounce interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="delay">Delay time span.</param>
 /// <returns>The interceptor policy.</returns>
 /// <remarks>
 /// Maybe a handler will be asked to process several times in a short time
 /// but you just want to process once at the last time because the previous ones are obsolete.
 /// A sample scenario is real-time search.
 /// </remarks>
 public static Action <T> Debounce(Action <T> action, TimeSpan delay)
 => Action(action, InterceptorPolicy.Debounce(delay));
Exemple #23
0
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Action Action(Action action, InterceptorPolicy policy)
 => new Interceptor(action, policy).InvokeBegin;
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="policy">The interceptor policy.</param>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="doNotWait">true if do not wait for the action result; otherwise, false.</param>
 public Interceptor(InterceptorPolicy policy, Func <InterceptorEventArgs <T>, Task> action, bool doNotWait = false)
     : base(policy, action, doNotWait)
 {
 }
Exemple #25
0
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <param name="doNotWait">true if do not wait for the action result; otherwise, false.</param>
 public Interceptor(Func <Task> action, InterceptorPolicy policy, bool doNotWait = false)
     : base(action is null ? null : arg => action(), policy, doNotWait)
 {
 }
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Action <T> Action(Action <T> action, InterceptorPolicy policy)
 => new Interceptor <T>(action, policy).InvokeBegin;
Exemple #27
0
 /// <summary>
 /// Creates an action with interceptor policy.
 /// </summary>
 /// <typeparam name="T1">The type of action handler argument 1.</typeparam>
 /// <typeparam name="T2">The type of action handler argument 2.</typeparam>
 /// <typeparam name="T3">The type of action handler argument 3.</typeparam>
 /// <typeparam name="T4">The type of action handler argument 4.</typeparam>
 /// <typeparam name="T5">The type of action handler argument 5.</typeparam>
 /// <typeparam name="T6">The type of action handler argument 6.</typeparam>
 /// <typeparam name="T7">The type of action handler argument 7.</typeparam>
 /// <typeparam name="T8">The type of action handler argument 8.</typeparam>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 /// <returns>The action with interceptor policy integration.</returns>
 public static Action <T1, T2, T3, T4, T5, T6, T7, T8> Action <T1, T2, T3, T4, T5, T6, T7, T8>(Action <T1, T2, T3, T4, T5, T6, T7, T8> action, InterceptorPolicy policy)
 => new Interceptor <T1, T2, T3, T4, T5, T6, T7, T8>(action, policy).InvokeBegin;
 public InterceptorPolicyTester()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
     thePolicy = new InterceptorPolicy<ITarget>(theActivator);
 }
 /// <summary>
 /// Creates a debounce interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="delay">Delay time span.</param>
 /// <returns>The interceptor policy.</returns>
 /// <remarks>
 /// Maybe a handler will be asked to process several times in a short time
 /// but you just want to process once at the last time because the previous ones are obsolete.
 /// A sample scenario is real-time search.
 /// </remarks>
 public static Func <T, Task> Debounce(Func <T, Task> action, TimeSpan delay)
 => Action(action, InterceptorPolicy.Debounce(delay));
Exemple #30
0
 /// <summary>
 /// Initializes a new instance of the Interceptor class.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="policy">The interceptor policy.</param>
 public Interceptor(Action action, InterceptorPolicy policy)
     : base(action is null ? null : arg => action(), policy)
 {
 }
 /// <summary>
 /// Creates a throttle interceptor policy.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="duration">The duration.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// You may want to request to call an action only once in a short time
 /// even if you request to call several times.
 /// The rest will be ignored.
 /// So the handler will be frozen for a while after it has processed.
 /// </remarks>
 public static Func <T, Task> Throttle(Func <T, Task> action, TimeSpan duration)
 => Action(action, InterceptorPolicy.Throttle(duration));
 /// <summary>
 /// Creates an interceptor policy responded at a specific times.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="min">The minmum invoking count.</param>
 /// <param name="max">The maxmum invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// A handler to process at last only when request to call in the specific times range.
 /// A sample scenario is double click.
 /// </remarks>
 public static Func <T, Task> Times(Func <T, Task> action, int min, int?max, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Times(min, max, timeout));
 /// <summary>
 /// Creates an interceptor policy responded at a specific times.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="count">The invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// A handler to process at last only when request to call in the specific times range.
 /// A sample scenario is double click.
 /// </remarks>
 public static Func <T, Task> Times(Func <T, Task> action, int count, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Times(count, timeout));
 /// <summary>
 /// Creates an interceptor policy responded at a specific times.
 /// </summary>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="count">The invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remarks>
 /// A handler to process at last only when request to call in the specific times range.
 /// A sample scenario is double click.
 /// </remarks>
 public static Action <T> Times(Action <T> action, int count, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Times(count, timeout));
Exemple #35
0
 /// <summary>
 /// Creates a multi-hit interceptor policy.
 /// </summary>
 /// <typeparam name="T1">The type of action handler argument 1.</typeparam>
 /// <typeparam name="T2">The type of action handler argument 2.</typeparam>
 /// <param name="action">The action to register to execute.</param>
 /// <param name="min">The minmum invoking count.</param>
 /// <param name="max">The maxmum invoking count.</param>
 /// <param name="timeout">The time span between each invoking.</param>
 /// <returns>The action with the specific interceptor policy integration.</returns>
 /// <remark>
 /// The handler to process for the specific times and it will be reset after a while.
 /// </remark>
 public static Func <T1, T2, Task> Mutliple <T1, T2>(Func <T1, T2, Task> action, int min, int?max, TimeSpan timeout)
 => Action(action, InterceptorPolicy.Mutliple(min, max, timeout));