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()); }
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()); }
public void TestValueInstanceBinding() { IDIContext context = ContextHelper.CreateContext(); context.s().BindInstance <int>(5); int i = context.Resolve <int>(); Assert.AreEqual(5, i); context.s().BindInstance <MyEnum>(MyEnum.Viktoria); MyEnum name = context.Resolve <MyEnum>(); Assert.AreEqual(MyEnum.Viktoria, name); }
public virtual T Create <T>(string sceneName, bool destroyableObjects, string bindingName = null, Action <IDIContext> customContextInitializer = null, Func <IConstruction> construction = null) where T : class, ISceneObject { if (environment != ContextEnvironment.RemoteObjects) { throw new MindiException("SceneFactory can only work in the Remote objects environment"); } IDIContext newContext = ContextHelper.CreateContext(this.context); if (destroyableObjects) { newContext.s().BindInstance <MBLifeTime>(MBLifeTime.DestroyWithScene); } IList <ISceneContextInitializer> initializers = ContextBuilder.Initialize <ISceneContextInitializer>(newContext, new SceneContextAttribute(sceneName)); BindObjectsRecord(newContext); if (customContextInitializer != null) { customContextInitializer(newContext); } return(CreateScene <T>(newContext, initializers, sceneName, bindingName, construction)); }
public void TestRebindSingletonToSingleton() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClass()); IDIContext childContext = ContextHelper.CreateContext(context); childContext.s().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.AreSame(b1, b2); Assert.AreNotSame(b1, a1); Assert.AreNotSame(b2, a2); IBinding d1 = context.Introspect <IMyClass>(); Assert.AreEqual(InstantiationType.Concrete, d1.instantiationType); Assert.AreEqual(context, d1.context); IBinding d2 = childContext.Introspect <IMyClass>(); Assert.AreEqual(InstantiationType.Concrete, d2.instantiationType); Assert.AreEqual(childContext, d2.context); }
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); }
public void TestRequirementError() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClassRequirement()); Assert.Throws <MiniocException>(() => { context.Resolve <IMyClass>(); }); }
public void TestNullBinding() { IDIContext context = ContextHelper.CreateContext(); context.s().BindInstance <IApple>(null); IApple apple = context.TryResolve <IApple>(); Assert.IsNull(apple); }
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>(); }); }
public void TestSoftResolutionWithDefault() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClassWithDefault()); IMyClass obj = context.Resolve <IMyClass>(); Assert.That(obj.orange is DefaultOrange); Assert.That(obj.apple is DefaultApple); }
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 <>)); }
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); }
public void TestConstructorResolution() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClass()); IMyClass obj = context.Resolve <IMyClass>(() => Construction .ForType <IOrange>(new Orange()).AndType <IApple>(new Apple())); Assert.That(obj.orange is Orange); Assert.That(obj.apple is Apple); }
protected MonoBehaviourBinder(IDIContext context, InstantiationMode mode) { this.contextInjection = context; if (mode == InstantiationMode.SINGLETON) { baseBinder = context.s(); } else if (mode == InstantiationMode.MULTIPLE) { baseBinder = context.m(); } }
public void TestCreationByNewFactorySingleton() { IDIContext context = ContextHelper.CreateContext(); // Let's bind a test type using singleton binder context.s().Bind <IDependencyTest>(() => new DependencyTest()); // Let's see that the dependencies are now injected - this class has injected context IDependencyTest test1 = context.Resolve <IDependencyTest>(); IDependencyTest test2 = context.Resolve <IDependencyTest>(); Assert.AreSame(test1, test2); }
public void TestSoftRequirement() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClassSoftRequirement()); IMyClass obj = context.Resolve <IMyClass>(() => Construction .ForType <IApple>(new Apple()) ); Assert.That(obj.apple is Apple); Assert.IsNull(obj.orange); }
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); }
public void TestTypelessRequirement() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClassRequirement()); IMyClass obj = context.Resolve <IMyClass>(() => Construction .For("orange", new Orange()) .And("apple", new Apple()) ); Assert.That(obj.orange is Orange); Assert.That(obj.apple is Apple); }
public void TestConstructorContextRestricted() { IDIContext context = ContextHelper.CreateContext(); context.s().Bind <IMyClass>(() => new MyClass()); Assert.Catch <MiniocException>(() => { context.Resolve <IMyClass>(() => Construction .ForType <IOrange>(new Orange()) .AndType <IApple>(new Apple()) .AndType <IDIContext>(new MiniocContext()) ); }); }
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); }
public void TestCreationByNew_IDICOntainerBranch() { IDIContext context = ContextHelper.CreateContext(); // Let's bind a test type using a usual new context.s().Bind <IDependencyTest>(() => new DependencyTest()); // Let's see that the dependencies are now injected - we have changed MinIOC. In original version this would fail IDependencyTest test = context.Resolve <IDependencyTest>(); Assert.IsNotNull((test as IDIClosedContext).descriptor); Assert.IsNotNull((test as IDIClosedContext).descriptor.context); Assert.IsNotNull(test.contextAccess); }
public void Test2InterfacesContainerBinding() { IDIContext context = ContextHelper.CreateContext(); // Then creating bindings for 2 interfaces through the container context.s().BindMany <IAdvancedRead, IAdvancedWrite>(() => new AdvancedModel()); IAdvancedRead read = context.Resolve <IAdvancedRead>(); IAdvancedWrite write = context.Resolve <IAdvancedWrite>(); Assert.That(read is AdvancedModel); Assert.That(write is AdvancedModel); Assert.AreSame(read, write); }
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); }
public void TestInstanceBindingST() { MyClass obj = new MyClass(); IDIContext context = ContextHelper.CreateContext(); context.s().BindInstance <IMyClass>(obj); IMyClass anotherObj = context.Resolve <IMyClass>(); Assert.AreSame(obj, anotherObj); IMyClass oneMorerObj = context.Resolve <IMyClass>(); Assert.AreSame(obj, oneMorerObj); }
public void TestSTNotSubjective() { IDIContext context = ContextHelper.CreateContext(); context.m().Bind <IOrange> (() => new Orange1()); context.s().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.AreSame(apple1, apple2); Assert.IsInstanceOf(typeof(Orange1), apple1.orange); Assert.IsInstanceOf(typeof(Orange1), apple2.orange); }
public void TestGenericSBindingMany() { IDIContext context = ContextHelper.CreateContext(); context.s().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.That(obj1.collection2 is List <string>); Assert.AreSame(obj1.list1, obj1.collection1); Assert.AreSame(obj1.list2, obj1.collection2); Assert.AreNotSame(obj1.list2, obj1.collection1); Assert.AreNotSame(obj1.list2, obj1.list1); Assert.AreNotSame(obj1.collection2, obj1.collection1); }
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); }
public void Initialize(IDIContext context) { context.s().Bind <IEditorPrefabFilter>(() => new EditorPrefabFilter()); }
public void Initialize(IDIContext context) { context.s().Bind <ISky>(() => new Sky()); context.Resolve <ISky>().SetColor(ConsoleColor.Magenta); }
protected void BindObjectsRecord(IDIContext ctx) { ctx.s().Rebind <IRemoteObjectsRecord>(null, "factory"); }