Ejemplo n.º 1
0
        private static void Decorated(ICalculator calc)
        {
            ICalculator decorated = DynamicProxyFactory <ICalculator> .Create(calc);

            var sw = Stopwatch.StartNew();
            int i  = 0;
            var q  = new Queue <int>();

            while (true)
            {
                int r = decorated.Add(1, i % 100);
                q.Enqueue(r);
                if (++i % LIMIT == 0)
                {
                    sw.Stop();
                    Console.WriteLine($"Decorated Duration = {sw.Elapsed:g} for {LIMIT:N0}");
                    i = 0;
                    if (q.First() < 0)
                    {
                        throw new Exception("won't happen");
                    }
                    sw.Restart();
                    q.Clear();
                }
            }
        }
Ejemplo n.º 2
0
        public static void Decorate <T>(
            this IServiceRegistry container)
        {
            container.Decorate <T>((ioc, instance) =>
            {
                var decorated = DynamicProxyFactory <T> .Create(instance,
                                                                info => Console.WriteLine($"    BEFORE: {instance} {info.ImplementationName} [{info.ContractName}: {info.MethodName}]"),
                                                                info => Console.WriteLine($@"   AFTER ({info.Duration:g}): {info.ImplementationName} [{info.ContractName}: {info.MethodName}]
------------------------------"),
                                                                info => Console.WriteLine($"    ERROR: {info.ImplementationName} [{info.ContractName}: {info.MethodName}]\r\n{info.Error}"));

                return(decorated);
            });
        }
Ejemplo n.º 3
0
        Decorate <TContract>(
            this IRegistrationBuilder <TContract, ConcreteReflectionActivatorData, SingleRegistrationStyle> builder,
            ContainerBuilder ioc,
            string name = null,
            Action <IRegistrationBuilder <TContract, LightweightAdapterActivatorData, DynamicRegistrationStyle> > decoratorConfig = null)
        {
            Func <IComponentContext, TContract, TContract> decoratorFactory = (context, instance) =>
            {
                var decorated = DynamicProxyFactory <TContract> .Create(instance,
                                                                        info => Console.WriteLine($"\tBEFORE: {info.ImplementationName} [{info.ContractName}: {info.MethodName}]"),
                                                                        info => Console.WriteLine($@"        AFTER ({info.Duration:g}): {info.ImplementationName} [{info.ContractName}: {info.MethodName}]
-----------------------------------"),
                                                                        info => Console.WriteLine($"\tERROR: {info.ImplementationName} [{info.ContractName}: {info.MethodName}]\r\n{info.Error}"));

                return(decorated);
            };

            return(Decorate <TContract>(builder, ioc, decoratorFactory, name, decoratorConfig));
        }