Beispiel #1
0
        public object Get(IInjectionBinding binding, object[] args)
        {
            if (binding == null)
            {
                throw new InjectionException("InjectorFactory cannot act on null binding", InjectionExceptionType.NULL_BINDING);
            }
            InjectionBindingType type = binding.type;

            if (objectMap.ContainsKey(binding) == false)
            {
                objectMap [binding] = new Dictionary <object, object> ();
            }

            switch (type)
            {
            case InjectionBindingType.SINGLETON:
                return(singletonOf(binding, args));

            case InjectionBindingType.VALUE:
                return(valueOf(binding));

            default:
                break;
            }
            return(instanceOf(binding, args));
        }
        public void TestSingletonChainBinding()
        {
            int a = 0;

            Binder.BindingResolver resolver = delegate(IBinding binding)
            {
                Assert.That(binding.value == typeof(InjectableDerivedClass));
                InjectionBindingType correctType = (a == 0) ? InjectionBindingType.DEFAULT : InjectionBindingType.SINGLETON;
                Assert.That((binding as InjectionBinding).type == correctType);
                a++;
            };
            new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ().ToSingleton();
        }
        public void TestValueChainBinding()
        {
            int a = 0;
            InjectableDerivedClass testValue = new InjectableDerivedClass();

            Binder.BindingResolver resolver = delegate(IBinding binding)
            {
                if (a == 2)
                {
                    Assert.That(binding.value == testValue);
                    InjectionBindingType correctType = (a == 0) ? InjectionBindingType.DEFAULT : InjectionBindingType.VALUE;
                    Assert.That((binding as InjectionBinding).type == correctType);
                }
                a++;
            };
            new InjectionBinding(resolver).Bind <InjectableSuperClass>().To <InjectableDerivedClass>().ToValue(testValue);
        }
Beispiel #4
0
        public object Get(IInjectionBinding binding, object[] args)
        {
            if (binding == null)
            {
                throw new InjectionException("InjectorFactory cannot act on null binding", InjectionExceptionType.NULL_BINDING);
            }
            InjectionBindingType type = binding.type;

            switch (type)
            {
            case InjectionBindingType.SINGLETON:
                return(singletonOf(binding, args));

            case InjectionBindingType.VALUE:
                return(valueOf(binding));

            default:
                break;
            }
            return(instanceOf(binding, args));
        }
Beispiel #5
0
 private void AddBinding <TInterface, TImplementation>(InjectionBindingType ibt) where TImplementation : TInterface
 {
     _bindingTypeForTInterface.Add(typeof(TInterface), ibt);
     _bindings.Add(typeof(TInterface), typeof(TImplementation));
 }
Beispiel #6
0
        private object GetExistingInstanceOfTypeForScope(Type type, Dictionary <Type, object> scopedInstances, InjectionBindingType injectionBindingType)
        {
            switch (injectionBindingType)
            {
            case InjectionBindingType.Singleton:
                if (_singletonInstances.ContainsKey(type))
                {
                    return(_singletonInstances[type]);
                }
                return(null);

            case InjectionBindingType.Scoped:
                if (scopedInstances.ContainsKey(type))
                {
                    return(scopedInstances[type]);
                }
                return(null);

            case InjectionBindingType.Transient:
                return(null);

            default:
                throw new Exception($"Unknown injection binding type for {type.FullName}");
            }
        }
Beispiel #7
0
        private void StoreInstance(Type type, object o, Dictionary <Type, object> scopedInstances, InjectionBindingType injectionBindingType)
        {
            switch (injectionBindingType)
            {
            case InjectionBindingType.Singleton:
                _singletonInstances[type] = o;
                break;

            case InjectionBindingType.Scoped:
                scopedInstances[type] = o;
                break;

            case InjectionBindingType.Transient:
                break;

            default:
                throw new Exception($"Unknown injection binding type for {type.FullName}");
            }
        }
Beispiel #8
0
        private object CreateParameterInstance(Type t, object[] paramaters, Dictionary <Type, object> scopedInstances, InjectionBindingType injectionBindingType)
        {
            Console.WriteLine($"Creating {t.FullName} with args");
            var instance = Activator.CreateInstance(t, paramaters);

            StoreInstance(t, instance, scopedInstances, injectionBindingType);

            return(instance);
        }