예제 #1
0
		private BindingKey ResolveBindingForGeneric(BindingKey explicitKey, Type bindingType) {
			var genericBindingKey = BindingKey.Get (bindingType.GetGenericTypeDefinition (), explicitKey.Qualifier);
			var genericBindingType = bindingType.GetGenericTypeDefinition ();
			var genericTypeArguments = bindingType.GetGenericArguments ();

			IBindingConfig genericBindingConfig;
			Type genericConcreteType = GetGenericImplementation (genericBindingKey, genericBindingType, out genericBindingConfig);

			// Have 'implementedBy OR explicit binding'
			if (genericConcreteType != null) {
				OpenGenericBinding.For (genericBindingType).To (genericConcreteType); // validate binding
				Type concreteType = genericConcreteType.MakeGenericType (genericTypeArguments);

				var binding = new GenericBinding () {
					BindingConfig = new BindingConfig(concreteType),
					BindingKey = explicitKey.ToImplicit(),
					ConcreteType = concreteType
				};

				if (genericBindingConfig != null) {
					binding.BindingConfig.Lifestyle = genericBindingConfig.Lifestyle;
				}

				injector.Register (binding);

				return binding.BindingKey;
			}

			return null;
		}
예제 #2
0
        public void TestBasicRegistrationWithLifestyles()
        {
            var injector = new Injector();

            injector.Register(OpenGenericBinding.For(typeof(IBasic <>)).To(typeof(Basic <>)).SetLifestyle(Lifestyle.Singleton));

            var obj = injector.Resolve <IBasic <object> > ();

            Assert.NotNull(obj);
            Assert.AreEqual(typeof(Basic <object>), obj.GetType());

            var obj2 = injector.Resolve <IBasic <object> > ();

            Assert.AreSame(obj, obj2);
        }
예제 #3
0
        public void TestBasicRegistration()
        {
            var injector = new Injector();

            injector.Register(OpenGenericBinding.For(typeof(IBasic <>)).To(typeof(Basic <>)));

            var obj = injector.Resolve <IBasic <object> > ();

            Assert.NotNull(obj);
            Assert.AreEqual(typeof(Basic <object>), obj.GetType());

            var obj2 = injector.Resolve <IBasic <object> > ();

            Assert.AreNotSame(obj, obj2);

            var ex = injector.Resolve <IBasic <Exception> > ();

            Assert.NotNull(ex);
            Assert.AreEqual(typeof(Basic <Exception>), ex.GetType());
        }
예제 #4
0
 public void TestRejectIncorrectGenericArgsToBinding()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(IDouble <,>)).To(typeof(MDouble <>));
     }, InjectorErrors.ErrorGenericsBindToTypeMustHaveSameTypeArgsAsKey, typeof(IDouble <,>), typeof(MDouble <>));
 }
예제 #5
0
 public void TestRejectNonDerivedToBinding()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Basic <>)).To(typeof(MImplicit <>));
     }, InjectorErrors.ErrorGenericsBindToTypeIsNotDerivedFromKey, typeof(Basic <>), typeof(MImplicit <>));
 }
예제 #6
0
 public void TestRejectClosedGenericTypeParameter()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Basic <object>));
     }, InjectorErrors.ErrorGenericsCannotCreateBindingForClosedGeneric, typeof(Basic <object>));
 }
예제 #7
0
 public void TestRejectNonGenericTypeParameter()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Exception));
     }, InjectorErrors.ErrorGenericsCannotCreateBindingForNonGeneric, typeof(Exception));
 }