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); }
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); }
protected void ControlRemoteObject(object instance, object value, PropertyInfo propertyInfo) { // Not letting mono behaviours to be injected if they are not IDIClosedContext // (because we cannot control their lifetime) IDIClosedContext ctxval = value as IDIClosedContext; if (RemoteObjectsHelper.IsRemoteObject(value) && (ctxval == null || !ctxval.IsValid())) { throw new MiniocException("Injecting a remote object that doesn't implement IDIClosedContext is not allowed ! Tried to inject object " + value + " into property " + propertyInfo.Name + " of the object " + instance); } }
public object Resolve() { object obj = instantiationFactory(); // Saving the binding descriptor on resolved object IDIClosedContext cctx = obj as IDIClosedContext; if (cctx != null && cctx.IsValid()) { cctx.descriptor.bindingDescriptor = this; } return(obj); }
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); }
private void InjectDependenciesInternal(object instance, Func <IConstruction> construction) { if (instance == null) { return; } // We need to call getInjectionStrategies early here so we can through exception if e.g. there are Injection attributes, but the object is not IDIClosedContext var strategies = _injector.getInjectionStrategies(instance); // Not injecting any dependencies if the object is not context object IDIClosedContext stateInstance = instance as IDIClosedContext; if (stateInstance == null || !stateInstance.IsValid()) { RegisterRemoteObject(instance, true); return; } IBinding descriptor = stateInstance.descriptor.bindingDescriptor; if (descriptor == null) { throw new MindiException("Called inject dependencies on an instance that has no binding descriptor set: " + instance); } // If this instance is concrete on another layer, we inject dependencies on its own layer only, to avoid subjectivization if (descriptor.instantiationType == InstantiationType.Concrete && descriptor.context != this) { descriptor.context.InjectDependencies(instance, construction); return; } if (stateInstance.descriptor.diState == DIState.NotResolved) { stateInstance.descriptor.diState = DIState.Resolving; _injector.injectDependencies(instance, strategies, construction); RegisterRemoteObject(instance); stateInstance.AfterInjection(); stateInstance.descriptor.diState = DIState.Resolved; } }