private IHandlerBase ResolveCommand(Command Command, CommandDispatcher dispatcher, Type type)
        {
            var context = new ChildKernel(_kernel);

            // long-lived
            context.BindInstance(dispatcher);
            context.BindInstance(dispatcher.Environments);
            context.BindInstance(dispatcher.State);
            context.BindInstance(dispatcher.Output);
            context.BindInstance(dispatcher.Parser);
            context.BindInstance(dispatcher.Handlers);

            // transient
            context.BindInstance(Command);
            context.BindInstance(Command.Arguments);

            if (dispatcher.Environments.Current != null)
            {
                context.Bind(dispatcher.Environments.Current.GetType()).ToConstant(dispatcher.Environments.Current);
            }

            var instance = context.Get(type);

            context.Dispose();
            return(instance as IHandlerBase);
        }
示例#2
0
        internal static void InitializeChildKernel()
        {
//            if (null != ChildKernel) {
//                ChildKernel.Dispose();
//                ChildKernel = null;
//            }

            // 20140129
            if (null != ChildKernel && 5 == ChildKernelCreationCounter)
            {
                ChildKernel.Dispose();
                ChildKernel = null;
                ChildKernelCreationCounter = 0;
            } // else {
              //     ChildKernelCreationCounter++;
              // }

            // 20140125
            if (null == ChildKernel)
            {
                ChildKernel = new ChildKernel(Kernel, new ChildKernelModule());
                // 20140123
                // ChildKernel.Settings.ActivationCacheDisabled = true;
                ChildKernel.Settings.ActivationCacheDisabled = false;

                // 20140129
                ChildKernelCreationCounter++;
            }
        }
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello NInject!");

        // TODO: Implement Functionality Here

        Kernel = new StandardKernel(new NjModule());
        Kernel.Settings.ActivationCacheDisabled = true;

        var requester = Kernel.Get <Requester>();

        for (int i = 0; i < 100000000; i++)
        {
            var childKernel = new ChildKernel(Kernel, new NjChildKernelModule());
            childKernel.Settings.ActivationCacheDisabled = true;

            List <IMyObj> list =
                requester.RequestObjects(childKernel);

            foreach (MyObj listItem in list)
            {
                listItem.Dispose();
            }
            list.Clear();
            list = null;

            childKernel.Dispose();
        }

        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
        public static void CanSecondChildSupplyDifferentInstanceOfBusinessLogicWithCorrectRepository()
        {
            /*
             * Repository in the child kernel.
             * Business Logic in the parent kernel.
             * Dispose the child kernel and try it again!
             */

            //Arrange
            var kernel = new StandardKernel();

            kernel.Bind <IMyRepository>().To <MyRepository>();
            kernel.Bind <IMyBusinessLogic>().To <MyBusinessLogic>().InTransientScope();

            var myRepository1 = new Mock <IMyRepository>();

            myRepository1.Setup(x => x.Get("Test")).Returns("I came from a mock");

            var childKernel1 = new ChildKernel(kernel);

            childKernel1.Bind <IMyRepository>().ToConstant(myRepository1.Object).InSingletonScope();

            var businessLogic1 = childKernel1.Get <IMyBusinessLogic>();

            childKernel1.Dispose();

            var myRepository2 = new Mock <IMyRepository>();

            myRepository2.Setup(x => x.Get("Test")).Returns("I came from a mock2");

            var childKernel2 = new ChildKernel(kernel);

            childKernel1.Bind <IMyRepository>().ToConstant(myRepository2.Object).InSingletonScope();

            //Act
            var businessLogic2 = childKernel2.Get <IMyBusinessLogic>();

            //Assert
            Assert.NotEqual(businessLogic1, businessLogic2);

            Assert.Equal("I came from a mock and then the business layer returned it.",
                         businessLogic1.DoSomething("Test"));

            Assert.Equal("I came from a mock and then the business layer returned it.",
                         businessLogic2.DoSomething("Test"));
        }
示例#5
0
 internal static void DisposeChildKernel()
 {
     ChildKernel.Dispose();
     ChildKernel = null;
 }