Ejemplo n.º 1
0
        public void TestGenericRebinding()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().BindInstance <ContextEnvironment>(ContextEnvironment.Normal);
            context.m().BindGeneric(typeof(IDIFactory <>), typeof(ContextFactory <>));
            context.m().BindGeneric(typeof(IDIRFactory <,>), typeof(ReproduceContextFactory <,>));

            IDIContext childContext = ContextHelper.CreateContext(context);

            childContext.m().Bind <IMyClass>(() => new MyClass());
            childContext.s().Rebind <IDIFactory <IOtherClass> >();

            MyClass obj1 = childContext.Resolve <IMyClass>() as MyClass;

            Assert.That(obj1.factory is ContextFactory <IOtherClass>);
            Assert.That(obj1.chainFactory is ReproduceContextFactory <IOtherClass, IGlobalContextInitializer>);

            MyClass obj2 = childContext.Resolve <IMyClass>() as MyClass;

            Assert.That(obj2.factory is ContextFactory <IOtherClass>);
            Assert.That(obj2.chainFactory is ReproduceContextFactory <IOtherClass, IGlobalContextInitializer>);

            Assert.AreSame(obj1.factory, obj2.factory);
            Assert.AreNotSame(obj1.chainFactory, obj2.chainFactory);
        }
Ejemplo n.º 2
0
 public void Initialize(IDIContext context)
 {
     context.s().Rebind <ILog>(null, BindingName.For(LogType.Decorated));
     context.m().Bind <ILogOwner>(() => new MyLogOwner());
     context.s().Bind <IWorld>(() => new Earth());
     context.s().Bind <ISky>(() => new Sky());
     context.m().Bind <IHumanFactory>(() => new HumanFactory());
     context.m().Bind <IHuman>(() => new Human());
 }
Ejemplo n.º 3
0
        public void TestMixedInjection()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().Bind <IMyClass>(() => new MyClassMethodMixed());
            context.m().Bind <IOrange>(() => new Orange());
            context.m().Bind <IApple>(() => new Apple());

            IMyClass obj = context.Resolve <IMyClass>();

            Assert.That(obj.orange is Orange);
            Assert.That(obj.apple is Apple);
        }
Ejemplo n.º 4
0
        public void TestNullNameResolve()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IApple>(() => new Apple());
            context.m().Bind <IOrange> (() => new Orange1());

            IApple appleInstance = context.Resolve <IApple>(null, null);

            Assert.IsNotNull(appleInstance);

            appleInstance = context.Resolve <IApple>("");
            Assert.IsNotNull(appleInstance);
        }
Ejemplo n.º 5
0
        public void Initialize(IDIContext context)
        {
            context.s().Bind <IRemoteObjectsHash>(() => new RemoteObjectsHash());
            context.s().BindInstance <ContextEnvironment>(ContextEnvironment.RemoteObjects, "unity", true);
            context.s().BindInstance <MBLifeTime>(MBLifeTime.Permanent);
            context.s().Bind <IRemoteObjectsDestroyer>(() => new RemoteObjectsDestroyer());

            context.s().Bind <IRemoteObjectsRecord>(() => new RemoteObjectsRecordRoot());
            context.m().Bind <IRemoteObjectsRecord>(() => new RemoteObjectsRecord(), "factory");

            context.m().Bind <RootSceneFactory>(() => new RootSceneFactory());
            context.m().Bind <ISceneObject>(() => new SceneObject());
            context.m().Bind <IDISceneFactory>(() => new SceneFactory());
        }
Ejemplo n.º 6
0
        public void TestRequirementPriorityOverContext()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().Bind <IMyClass>(() => new MyClass());
            context.m().Bind <IOrange>(() => new Orange());
            context.m().Bind <IApple>(() => new Apple());

            IMyClass obj = context.Resolve <IMyClass>(() => Construction
                                                      .ForType <IApple>(new DefaultApple())
                                                      );

            Assert.That(obj.orange is Orange);
            Assert.That(obj.apple is DefaultApple);
        }
Ejemplo n.º 7
0
        public void TestNamedParentIntrospection()
        {
            IDIContext parentContext = ContextHelper.CreateContext();

            parentContext.m().Bind <IApple>(() => new Apple(), BindingName.ForType <Apple>());

            IDIContext context = parentContext.Reproduce();

            context.m().Bind <IApple>(() => new BigApple(), BindingName.ForType <BigApple>());

            IBinding desc = context.Introspect <IApple>(BindingName.ForType <BigApple>());

            Assert.IsNotNull(desc);
            Assert.AreEqual(InstantiationType.Abstract, desc.instantiationType);
            Assert.That(desc.context == context);
            object apple = desc.factory();

            Assert.That(apple is BigApple);

            desc = context.Introspect <IApple>();
            Assert.IsNotNull(desc);
            Assert.AreEqual(InstantiationType.Abstract, desc.instantiationType);
            Assert.That(desc.context == context);
            apple = desc.factory();
            Assert.That(apple is BigApple);

            desc = context.Introspect <IApple>(BindingName.ForType <Apple>());
            Assert.IsNotNull(desc);
            Assert.AreEqual(InstantiationType.Abstract, desc.instantiationType);
            Assert.That(desc.context == parentContext);
            apple = desc.factory();
            Assert.That(apple is Apple);
        }
Ejemplo n.º 8
0
        public void TestRebindSingletonToMultiple()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().Bind <IMyClass>(() => new MyClass());

            IDIContext childContext = ContextHelper.CreateContext(context);

            childContext.m().Rebind <IMyClass>();

            IMyClass a1 = context.Resolve <IMyClass>();
            IMyClass a2 = context.Resolve <IMyClass>();

            Assert.AreSame(a1, a2);

            IMyClass b1 = childContext.Resolve <IMyClass>();
            IMyClass b2 = childContext.Resolve <IMyClass>();

            Assert.AreNotSame(b1, b2);
            Assert.AreNotSame(b1, a1);

            IBinding d1 = context.Introspect <IMyClass>();

            Assert.AreEqual(InstantiationType.Concrete, d1.instantiationType);
            Assert.AreEqual(context, d1.context);

            IBinding d2 = childContext.Introspect <IMyClass>();

            Assert.AreEqual(InstantiationType.Abstract, d2.instantiationType);
            Assert.AreEqual(childContext, d2.context);
        }
Ejemplo n.º 9
0
        public void TestMPSubjective()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IOrange> (() => new Orange1());
            context.m().Bind <IApple> (() => new Apple());
            IDIContext childContext = context.Reproduce();

            childContext.m().Bind <IOrange> (() => new Orange2());

            IApple apple2 = childContext.Resolve <IApple> ();
            IApple apple1 = context.Resolve <IApple> ();

            Assert.AreNotSame(apple1, apple2);
            Assert.IsInstanceOf(typeof(Orange1), apple1.orange);
            Assert.IsInstanceOf(typeof(Orange2), apple2.orange);
        }
Ejemplo n.º 10
0
        public void TestCustomFactory()
        {
            IDIContext context = ContextHelper.CreateContext <IGlobalContextInitializer>();

            context.m().Bind(() => new MyClassFactory());
            context.m().Bind <IApple>(() => new Apple());
            context.m().Bind <IOrange>(() => new Orange());
            context.m().Bind <IMyClass>(() => new MyClass());

            var factory = context.Resolve <MyClassFactory>();

            IMyClass instance = factory.Create();

            Assert.IsNotNull(instance);

            instance = factory.Destroy(instance);
            Assert.IsNull(instance);
        }
Ejemplo n.º 11
0
        public void TestGenericMPBindingMany()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().BindGenericMany(new List <Type> {
                typeof(ICollection <>), typeof(IList <>)
            }, typeof(List <>));
            context.m().Bind(() => new TestClassMany());

            var obj1 = context.Resolve <TestClassMany>();

            Assert.That(obj1.list1 is List <int>);
            Assert.That(obj1.list2 is List <string>);
            Assert.That(obj1.collection1 is List <int>);

            Assert.AreNotSame(obj1.list1, obj1.collection1);
            Assert.AreNotSame(obj1.list2, obj1.collection1);
            Assert.AreNotSame(obj1.list2, obj1.list1);
        }
Ejemplo n.º 12
0
        public void TestConstructionFactory()
        {
            IDIContext context = ContextHelper.CreateContext <IGlobalContextInitializer>();

            context.m().Bind(() => new MyClassConstructorFactory());
            context.m().Bind <IMyClass>(() => new MyClass());

            var factory = context.Resolve <MyClassConstructorFactory>();

            IMyClass instance = factory.Create(new Apple(), new Orange());

            Assert.IsNotNull(instance);
            IDIClosedContext cto = instance as IDIClosedContext;

            Assert.AreSame(cto.descriptor.context, context);

            instance = factory.Destroy(instance);
            Assert.IsNull(instance);
        }
Ejemplo n.º 13
0
        public void TestwWithoutCustomFactoryWrapper()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IMyClass>(() => new MyClass());

            IMyClass obj = context.Resolve <IMyClass> ();

            Assert.IsInstanceOf <MyClass>(obj);
        }
Ejemplo n.º 14
0
        public void TestSimpleResolutionException()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().Bind <IMyClass>(() => new MyClass());
            context.m().Bind <IApple>(() => new Apple());

            Assert.Catch <MiniocException>(() => {
                context.Resolve <IMyClass>();
            });
        }
Ejemplo n.º 15
0
        public void TestResolveObjectWithInvalidatedContext()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IOrange> (() => new Orange1());

            IOrange          orange = context.Resolve <IOrange> ();
            IDIClosedContext ctx    = orange as IDIClosedContext;

            Assert.IsNotNull(ctx.descriptor.bindingDescriptor);
        }
Ejemplo n.º 16
0
        public void TestResolvedInstanceHasBindingDescriptor()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IOrange> (() => new Orange1());

            IOrange          orange = context.Resolve <IOrange> ();
            IDIClosedContext ctx    = orange as IDIClosedContext;

            Assert.IsNotNull(ctx.bindingDescriptor);
        }
Ejemplo n.º 17
0
        public void TestSeveralMethodsInjection()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IMyClass>(() => new MyClassMethodSeveral());
            context.m().Bind <IOrange>(() => new Orange());

            IMyClass obj = context.Resolve <IMyClass>();

            Assert.That(obj.orange is Orange);
            Assert.That(obj.apple == null);

            obj = context.Resolve <IMyClass>(() => Construction.ForType <IApple>(new Apple()));
            Assert.That(obj.orange is Orange);
            Assert.That(obj.apple is Apple);

            context.m().Bind <IApple>(() => new Apple());
            obj = context.Resolve <IMyClass>();
            Assert.That(obj.orange is Orange);
            Assert.That(obj.apple is Apple);
        }
Ejemplo n.º 18
0
        public void TestSoftResolution()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().Bind <IMyClass>(() => new MyClass());
            context.m().Bind <IOrange>(() => new Orange());

            IMyClass obj = context.Resolve <IMyClass>();

            Assert.That(obj.orange is Orange);
            Assert.IsNull(obj.apple);
        }
Ejemplo n.º 19
0
        protected MonoBehaviourBinder(IDIContext context, InstantiationMode mode)
        {
            this.contextInjection = context;

            if (mode == InstantiationMode.SINGLETON)
            {
                baseBinder = context.s();
            }
            else if (mode == InstantiationMode.MULTIPLE)
            {
                baseBinder = context.m();
            }
        }
Ejemplo n.º 20
0
        public void TestResolvedInstanceHasBindingDescriptor()
        {
            IDIContext context = ContextHelper.CreateContext();
            IOrange    orange  = new Orange1();

            (orange as IDIClosedContext).Invalidate();
            context.s().BindInstance(orange);
            context.m().Bind <IPineapple>(() => new Pineapple());

            IPineapple pineApple = context.Resolve <IPineapple>();

            Assert.IsNotNull(pineApple);
            Assert.IsNotNull(pineApple.orange);
        }
Ejemplo n.º 21
0
        public void TestGenericIntrospect()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.s().BindInstance <ContextEnvironment>(ContextEnvironment.Normal);
            context.m().BindGeneric(typeof(IDIFactory <>), typeof(ContextFactory <>));
            context.m().BindGeneric(typeof(IDIRFactory <,>), typeof(ReproduceContextFactory <,>));

            IDIContext childContext = ContextHelper.CreateContext(context);

            childContext.m().Bind <IMyClass>(() => new MyClass());
            childContext.s().Rebind <IDIFactory <IOtherClass> >();

            IBinding desc1 = childContext.Introspect <IDIFactory <IOtherClass> >();

            Assert.AreEqual(InstantiationType.Concrete, desc1.instantiationType);
            Assert.AreEqual(childContext, desc1.context);

            IBinding desc2 = childContext.Introspect <IDIRFactory <IOtherClass, IGlobalContextInitializer> >();

            Assert.AreEqual(InstantiationType.Abstract, desc2.instantiationType);
            Assert.AreEqual(context, desc2.context);
        }
Ejemplo n.º 22
0
        public void TestParentContainerChildAdapter()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IContainer>(() => new Container());
            context.m().Bind <IAdapter>(() => new DefaultAdapter());

            IDIContext childContext = ContextHelper.CreateContext(context);

            childContext.m().Bind <IAdapter>(() => new CustomAdapter());

            // Resolving container from the parent context
            // Expecting DefaultAdapter
            IContainer container = context.Resolve <IContainer> ();

            Assert.IsAssignableFrom <Container> (container);
            Assert.IsAssignableFrom <DefaultAdapter> (container.adapter);

            // Resolving container from the child context
            // Expecting CustomAdapter
            container = childContext.Resolve <IContainer> ();
            Assert.IsAssignableFrom <Container> (container);
            Assert.IsAssignableFrom <CustomAdapter> (container.adapter);
        }
Ejemplo n.º 23
0
        public void TestNamedParentResolution()
        {
            IDIContext parentContext = ContextHelper.CreateContext();

            parentContext.m().Bind <IApple>(() => new Apple(), BindingName.ForType <Apple>());

            IDIContext context = parentContext.Reproduce();

            context.m().Bind <IApple>(() => new BigApple(), BindingName.ForType <BigApple>());

            IApple apple = context.Resolve <IApple>(BindingName.ForType <BigApple>());

            Assert.That(apple is BigApple);

            apple = context.Resolve <IApple>();
            Assert.That(apple is BigApple);

            apple = context.Resolve <IApple>(BindingName.ForType <Apple>());
            Assert.That(apple is Apple);
        }
Ejemplo n.º 24
0
        public void TestSeveralRequirementsSameType()
        {
            IDIContext context = ContextHelper.CreateContext();

            context.m().Bind <IMyOtherClass>(() => new MyClassSameTypeRequirements());

            IMyOtherClass obj = context.Resolve <IMyOtherClass>(() => Construction
                                                                .ForType <IOrange>(new Orange()).AndType <IApple>(new Apple())
                                                                );

            Assert.That(obj.orange is Orange);
            Assert.That(obj.anotherOrange is Orange);
            Assert.AreSame(obj.orange, obj.anotherOrange);
            Assert.That(obj.apple is Apple);

            obj = context.Resolve <IMyOtherClass>(() => Construction
                                                  .ForType <IOrange>("orange", new Orange()).AndType <IApple>(new Apple())
                                                  .AndType <IOrange>("anotherOrange", new DefaultOrange())
                                                  );

            Assert.That(obj.orange is Orange);
            Assert.That(obj.anotherOrange is DefaultOrange);
            Assert.That(obj.apple is Apple);
        }
 public void Initialize(IDIContext context)
 {
     context.m().Bind <ILog>(() => new SimpleLog(), BindingName.For(LogType.Simple));
     context.m().Bind <ILog>(() => new DecoratedLog(context.Resolve <ILog>(BindingName.For(LogType.Simple))), BindingName.For(LogType.Decorated));
     context.m().Bind <ILogOwner>(() => new CoreLogOwner());
 }
Ejemplo n.º 26
0
        public void Initialize(IDIContext context)
        {
            // Generic collections
            context.m().BindGeneric(typeof(IList <>), typeof(List <>));
            context.m().BindGeneric(typeof(List <>), typeof(List <>));
            context.m().BindGeneric(typeof(IEnumerable <>), typeof(List <>));
            context.m().BindGeneric(typeof(HashSet <>), typeof(HashSet <>));
            context.m().BindGeneric(typeof(IDictionary <,>), typeof(Dictionary <,>));
            context.m().BindGeneric(typeof(Dictionary <,>), typeof(Dictionary <,>));

            context.s().BindInstance <ContextEnvironment>(ContextEnvironment.Normal);
            context.s().Bind <IActionQueue>(() => new ActionQueue());

            context.m().BindGeneric(typeof(IDIFactory <>), typeof(ContextFactory <>));
            context.m().BindGeneric(typeof(IDIRFactory <,>), typeof(ReproduceContextFactory <,>));
            context.m().BindGeneric(typeof(IDIRFactory <, ,>), typeof(ReproduceContextFactory <, ,>));
            context.m().BindGeneric(typeof(IDIRFactory <, , ,>), typeof(ReproduceContextFactory <, , ,>));

            context.m().BindGeneric(typeof(IDynamicInjection <>), typeof(DynamicResolver <>));
            context.m().BindGeneric(typeof(ISoftDynamicInjection <>), typeof(SoftDynamicResolver <>));
        }