public TypeRegistration(Type typeFrom, Type typeTo, string name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
        {
            Name            = string.IsNullOrEmpty(name) ? null : name;
            RegisteredType  = typeFrom ?? typeTo;
            MappedToType    = typeTo;
            LifetimeManager = lifetimeManager ?? TransientLifetimeManager;

            if (LifetimeManager.InUse)
            {
                throw new InvalidOperationException(Constants.LifetimeManagerInUse);
            }
            LifetimeManager.InUse = true;

            if (typeFrom != null && typeFrom != typeTo)
            {
                if (typeFrom.GetTypeInfo().IsGenericTypeDefinition&& typeTo.GetTypeInfo().IsGenericTypeDefinition)
                {
                    this[typeof(IBuildKeyMappingPolicy)] = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeTo, name));
                }
                else
                {
                    var policy = (null != injectionMembers && injectionMembers.Length > 0) || lifetimeManager is IRequireBuildUpPolicy
                        ? new BuildKeyMappingPolicy(new NamedTypeBuildKey(typeTo, name))
                        : new ResolveMappingPolicy(new NamedTypeBuildKey(typeTo, name));

                    this[typeof(IBuildKeyMappingPolicy)] = policy;
                }
            }
        }
示例#2
0
        public void PolicyThrowsIfWrongNumberOfGenericParameters()
        {
            var original = new NamedTypeBuildKey(typeof(IList <string>));
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(Dictionary <,>)));

            policy.Map(original, null);
        }
        public void CanMapGenericTypeFromNamedTypeBuildKey()
        {
            NamedTypeBuildKey original = new NamedTypeBuildKey(typeof (IList<string>), "test");
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof (List<>), "test"));

            NamedTypeBuildKey result = policy.Map(original, null);

            Assert.AreEqual(typeof (List<string>), result.Type);
            Assert.AreEqual(original.Name, result.Name);
        }
        public void CanMapGenericTypeToNewGenericType()
        {
            var original = new NamedTypeBuildKey(typeof (IList<int>));
            var expected = new NamedTypeBuildKey(typeof (List<int>));

            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof (List<>)));

            var result = policy.Map(original, null);
            Assert.AreEqual(expected, result);
        }
示例#5
0
        public void CanMapGenericTypeFromNamedTypeBuildKey()
        {
            NamedTypeBuildKey      original = new NamedTypeBuildKey(typeof(IList <string>), "test");
            IBuildKeyMappingPolicy policy   = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List <>), "test"));

            NamedTypeBuildKey result = policy.Map(original, null);

            Assert.Equal(typeof(List <string>), result.Type);
            Assert.Equal(original.Name, result.Name);
        }
示例#6
0
        public void CanMapGenericTypeToNewGenericType()
        {
            var original = new NamedTypeBuildKey(typeof(IList <int>));
            var expected = new NamedTypeBuildKey(typeof(List <int>));

            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List <>)));

            var result = policy.Map(original, null);

            Assert.Equal(expected, result);
        }
示例#7
0
        public void CanMapGenericTypeToNewGenericType()
        {
            Type original = typeof(IList <int>);
            Type expected = typeof(List <int>);

            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(typeof(List <>));

            Type result = (Type)policy.Map(original);

            Assert.AreEqual(expected, result);
        }
示例#8
0
        public void PolicyThrowsIfInputIsNotAGeneric()
        {
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List <>)));

            try
            {
                policy.Map(new NamedTypeBuildKey <int>(), null);
                Assert.True(false, string.Format("Expected Exception"));
            }
            catch (ArgumentException)
            {
                // expected
            }
        }
示例#9
0
 public void PolicyThrowsIfWrongNumberOfGenericParameters()
 {
     var original = new NamedTypeBuildKey(typeof(IList<string>));
     IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(Dictionary<,>)));
     try
     {
         policy.Map(original, null);
         Assert.Fail("Expected exception");
     }
     catch (ArgumentException)
     {
         // expected
     }
 }
示例#10
0
        public void PolicyThrowsIfInputIsNotAGeneric()
        {
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List<>)));

            try
            {
                policy.Map(new NamedTypeBuildKey<int>(), null);
                Assert.Fail("Expected Exception");
            }
            catch (ArgumentException)
            {
                // expected
            }
        }
示例#11
0
        public void PolicyThrowsIfWrongNumberOfGenericParameters()
        {
            var original = new NamedTypeBuildKey(typeof(IList <string>));
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(Dictionary <,>)));

            try
            {
                policy.Map(original, null);
                Assert.True(false, string.Format("Expected exception"));
            }
            catch (ArgumentException)
            {
                // expected
            }
        }
 public void PolicyThrowsIfInputIsNotAGeneric()
 {
     IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof (List<>)));
     policy.Map(new NamedTypeBuildKey<int>(), null);
 }
 public void PolicyThrowsIfWrongNumberOfGenericParameters()
 {
     var original = new NamedTypeBuildKey(typeof (IList<string>));
     IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof (Dictionary<,>)));
     policy.Map(original, null);
 }
示例#14
0
        public void PolicyThrowsIfInputIsNotAGeneric()
        {
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List <>)));

            policy.Map(new NamedTypeBuildKey <int>(), null);
        }
示例#15
0
        public void PolicyThrowsIfInputIsNotAGeneric()
        {
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(typeof(List <>)));

            Assert.Throws <ArgumentException>(() => policy.Map(new NamedTypeBuildKey <int>(), null));
        }
示例#16
0
        public void PolicyThrowsIfInputIsNotAGeneric()
        {
            IBuildKeyMappingPolicy policy = new GenericTypeBuildKeyMappingPolicy(typeof(List <>));

            policy.Map(typeof(int));
        }