Пример #1
0
        public IActionResult Index()
        {
            ViewBag.mess1 = "First Instance " + _transientService1.GetID().ToString();
            ViewBag.mess2 = "Second Instance " + _transientService2.GetID().ToString();

            ViewBag.mess3 = "Thirt Instance " + _scopeService1.GetID().ToString();
            ViewBag.mess4 = "Fount Instance " + _scopeService2.GetID().ToString();

            ViewBag.mess5 = "Five Instance " + _instanceService1.GetID().ToString();
            ViewBag.mess6 = "Six Instance " + _instanceService2.GetID().ToString();
            return(View());
        }
Пример #2
0
        public IActionResult Index()
        {
            var singleton1            = singletonService1.GetID();
            var singleton2            = singletonService2.GetID();
            var singletonValueIsEqual = singleton1 == singleton2;

            //Injecting service with different lifetimes into another

            /* When the request comes for the first time a new instance of the singleton is created.
             * It also creates a new instance of the transient object and injects into the Singleton service.
             *
             * When the second request arrives the instance of the singleton is reused.
             * The singleton already contains the instance of the transient service Hence it is not created again.
             * This effectively converts the transient service into the singleton */
            var innerServices = singletonService1.TransientService.GetID() == singletonService2.TransientService.GetID();

            var scoped1            = scopedService1.GetID();
            var scoped2            = scopedService2.GetID();
            var scopedValueIsEqual = scoped1 == scoped2;

            var transient1            = transientService1.GetID();
            var transient2            = transientService2.GetID();
            var transientValueIsEqual = transient1 == transient2;


            //set viewdatat values
            SetViewData("singletonService1", singleton1);
            SetViewData("singletonService2", singleton2);
            SetViewData("singleton_Inner_Transiet_Service1", singletonService1.TransientService.GetID());
            SetViewData("singleton_Inner_Transiet_Service2", singletonService2.TransientService.GetID());

            SetViewData("scopedService1", scoped1);
            SetViewData("scopedService2", scoped2);

            SetViewData("TransientService1", transient1);
            SetViewData("TransientService2", transient2);

            return(View());
        }