public void HaveBoxSingletonTest()
 {
     var container = new HaveBox.Container();
     container.Configure(x => x.For<MockObject>().Use<MockObject>().AsSingleton());
     var start = DateTime.Now;
     for (int i = 0; i < _iterations; i++)
     {
         var t = container.GetInstance<MockObject>();
     }
     var end = DateTime.Now;
     Console.WriteLine(string.Format("{0} Singleton, Total milliseconds elapsed: {1}", "HaveBox", (end - start).TotalMilliseconds));
 }
Exemplo n.º 2
0
        public void Resolve_ForSingleInstance()
        {
            var container = new Container();

            container.Register <SimpleService, ISimpleService>().AsSingleInstance();

            var conainerbuilder = new ContainerBuilder();

            conainerbuilder.RegisterType <SimpleService>().As <ISimpleService>().SingleInstance();
            var autofacContainer = conainerbuilder.Build();

            var haveBoxContainer = new HaveBox.Container();

            haveBoxContainer.Configure(config => config.For <ISimpleService>().Use <SimpleService>());

            int numberOfTimes = 100000;

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    container.Resolve <ISimpleService>();
                }
            });

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    haveBoxContainer.GetInstance <ISimpleService>();
                }
            });

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    autofacContainer.Resolve <ISimpleService>();
                }
            });
        }
        public void Resolve_ForSingleInstance()
        {
            var container = new Container();
            container.Register<SimpleService, ISimpleService>().AsSingleInstance();

            var conainerbuilder = new ContainerBuilder();
            conainerbuilder.RegisterType<SimpleService>().As<ISimpleService>().SingleInstance();
            var autofacContainer = conainerbuilder.Build();

            var haveBoxContainer = new HaveBox.Container();
            haveBoxContainer.Configure(config => config.For<ISimpleService>().Use<SimpleService>());

            int numberOfTimes = 100000;

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    container.Resolve<ISimpleService>();
                }
            });

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    haveBoxContainer.GetInstance<ISimpleService>();
                }
            });

            Measure(() =>
            {
                for (int i = 0; i < numberOfTimes; i++)
                {
                    autofacContainer.Resolve<ISimpleService>();
                }
            });
        }