public void TestRebindMultipleToSingleton() { IDIContext context = ContextHelper.CreateContext(); context.m().Bind <IMyClass>(() => new MyClass()); IDIContext childContext = ContextHelper.CreateContext(context); IBinding d2 = childContext.Introspect <IMyClass>(); Assert.AreEqual(InstantiationType.Abstract, d2.instantiationType); Assert.AreEqual(context, d2.context); childContext.s().Rebind <IMyClass>(); IMyClass a1 = context.Resolve <IMyClass>(); IMyClass a2 = context.Resolve <IMyClass>(); Assert.AreNotSame(a1, a2); IMyClass b1 = childContext.Resolve <IMyClass>(); IMyClass b2 = childContext.Resolve <IMyClass>(); Assert.AreSame(b1, b2); Assert.AreNotSame(b1, a1); IBinding d1 = context.Introspect <IMyClass>(); Assert.AreEqual(InstantiationType.Abstract, d1.instantiationType); Assert.AreEqual(context, d1.context); d2 = childContext.Introspect <IMyClass>(); Assert.AreEqual(InstantiationType.Concrete, d2.instantiationType); Assert.AreEqual(childContext, d2.context); }
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); }
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); }
protected void VerifyObjectCreation(string name, object instance, IDIContext resolutionContext) { var contextObject = instance as IDIClosedContext; if (contextObject == null || !contextObject.IsValid()) { IBinding desc = resolutionContext.Introspect <T>(name); VerifyInstantiationContext(desc, resolutionContext, instance); return; } VerifyInstantiationContext(contextObject.descriptor.bindingDescriptor, resolutionContext, instance); if (contextObject.descriptor.factory != null) { throw new MindiException("Attempting to create an already created object on factory: " + this + ". Object: " + instance + ". If the object is bound as singleton, you cannot create it on the factory again !"); } contextObject.descriptor.factory = this; }