コード例 #1
0
ファイル: Program.cs プロジェクト: bnayae/AOPs
        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();
                }
            }
        }
コード例 #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);
            });
        }
コード例 #3
0
ファイル: Extensions.cs プロジェクト: bnayae/AOPs
        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));
        }