Ejemplo n.º 1
0
        static void NonRegistered()
        {
            Console.WriteLine("Straight:\tNot supported");
            Console.Write("pooDI:\t\t");
            watch.Start();
            var pooDI = new DI.Container();
            for(int i = 0; i < n; ++i) {
                try {
                    var c = pooDI.Resolve<ITestClass1>();
                } catch(DI.InterfaceNotRegisteredException) { }
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Unity:\t\t");
            watch.Start();
            var unity = new UnityContainer();
            for(int i = 0; i < n; ++i) {
                try {
                    var c = unity.Resolve<ITestClass1>();
                } catch(ResolutionFailedException) { }
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Ninject:\t");
            watch.Start();
            var kernel = new StandardKernel();
            for(int i = 0; i < n; ++i) {
                try {
                    var c = kernel.Get<ITestClass1>();
                } catch(ActivationException) { }
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Autofac:\t");
            watch.Start();
            var builder = new ContainerBuilder();
            var container = builder.Build();
            for(int i = 0; i < n; ++i) {
                try {
                    var c = container.Resolve<ITestClass1>();
                } catch(ComponentNotRegisteredException) { }
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Castle Windsor:\t");
            watch.Start();
            var windsor = new WindsorContainer();
            for(int i = 0; i < n; ++i) {
                try {
                    var c = windsor.Resolve<ITestClass1>();
                } catch(ComponentNotFoundException) { }
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();
        }
Ejemplo n.º 2
0
        static void ChainingInjection()
        {
            Console.Write("Straight:\t");
            watch.Start();
            for(int i = 0; i < n; ++i) {
                var c = new TestClass1();
                var d = new TestClass2(c);
                var e = new TestClass3(d);
                var f = new TestClass4(e);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("pooDI:\t\t");
            watch.Start();
            var pooDI = new DI.Container();
            pooDI.RegisterType<ITestClass1, TestClass1>(false);
            pooDI.RegisterType<TestClass2>(false);
            pooDI.RegisterType<TestClass3>(false);
            pooDI.RegisterType<TestClass4>(false);
            for(int i = 0; i < n; ++i) {
                var c = pooDI.Resolve<TestClass4>();
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Unity:\t\t");
            watch.Start();
            var unity = new UnityContainer();
            unity.RegisterType<ITestClass1, TestClass1>();
            unity.RegisterType<TestClass2>();
            unity.RegisterType<TestClass3>();
            unity.RegisterType<TestClass4>();
            for(int i = 0; i < n; ++i) {
                var c = unity.Resolve<TestClass4>();
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Ninject:\t");
            watch.Start();
            var kernel = new StandardKernel();
            kernel.Bind<ITestClass1>().To<TestClass1>();
            kernel.Bind<TestClass2>().ToSelf();
            kernel.Bind<TestClass3>().ToSelf();
            kernel.Bind<TestClass4>().ToSelf();
            for(int i = 0; i < n; ++i) {
                var c = kernel.Get<TestClass4>();
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Autofac:\t");
            watch.Start();
            var builder = new ContainerBuilder();
            builder.RegisterType<TestClass1>().As<ITestClass1>();
            builder.RegisterType<TestClass2>().AsSelf();
            builder.RegisterType<TestClass3>().AsSelf();
            builder.RegisterType<TestClass4>().AsSelf();
            var container = builder.Build();
            for(int i = 0; i < n; ++i) {
                var c = container.Resolve<TestClass4>();
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Castle Windsor:\t");
            watch.Start();
            var windsor = new WindsorContainer();
            windsor.Register(
                Component.For<ITestClass1>()
                    .ImplementedBy<TestClass1>()
                    .LifestyleTransient(),
                Component.For<TestClass2>()
                    .LifestyleTransient(),
                Component.For<TestClass3>()
                    .LifestyleTransient(),
                Component.For<TestClass4>()
                    .LifestyleTransient()
            );
            for(int i = 0; i < n; ++i) {
                var c = windsor.Resolve<TestClass4>();
                windsor.Release(c);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();
        }
Ejemplo n.º 3
0
        static void BuildUp()
        {
            Console.Write("Straight:\t");
            watch.Start();
            for(int i = 0; i < n; ++i) {
                var c = new TestClass5();
                c.t = new TestClass1();
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("pooDI:\t\t");
            watch.Start();
            var pooDI = new DI.Container();
            pooDI.RegisterType<ITestClass1, TestClass1>(false);
            for(int i = 0; i < n; ++i) {
                var c = new TestClass5();
                pooDI.BuildUp<TestClass5>(c);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Unity:\t\t");
            watch.Start();
            var unity = new UnityContainer();
            unity.RegisterType<ITestClass1, TestClass1>();
            for(int i = 0; i < n; ++i) {
                var c = new TestClass5();
                unity.BuildUp<TestClass5>(c);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Ninject:\t");
            watch.Start();
            var kernel = new StandardKernel();
            kernel.Bind<ITestClass1>().To<TestClass1>();
            for(int i = 0; i < n; ++i) {
                var c = new TestClass5();
                kernel.Inject(c);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.Write("Autofac:\t");
            watch.Start();
            var builder = new ContainerBuilder();
            builder.RegisterType<TestClass1>().As<ITestClass1>();
            var container = builder.Build();
            for(int i = 0; i < n; ++i) {
                var c = new TestClass5();
                container.InjectProperties<TestClass5>(c);
            }
            watch.Stop();
            Console.WriteLine(watch.Elapsed);
            watch.Reset();

            Console.WriteLine("Castle Windsor:\tNot supported");
        }