Пример #1
0
        public void MapType_OverridingSelectionOfConventionSelectedConstructor()
        {
            var builder = new RegistrationBuilder();

            builder.
            ForTypesDerivedFrom <IFoo>().
            Export <IFoo>();

            builder.ForType <FooImplWithConstructors>()
            .SelectConstructor(cis => cis[1]);

            var projectedType1 = builder.MapType(typeof(FooImpl).GetTypeInfo().GetTypeInfo());
            var projectedType2 = builder.MapType(typeof(FooImplWithConstructors).GetTypeInfo().GetTypeInfo());

            var constructor1 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 0).Single();
            var constructor2 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 1).Single();
            var constructor3 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 2).Single();


            // necessary as BuildConventionConstructorAttributes is only called for type level query for attributes
            var typeLevelAttrs = projectedType2.GetCustomAttributes(false);

            Assert.AreEqual(0, constructor1.GetCustomAttributes(false).Length);
            Assert.AreEqual(0, constructor3.GetCustomAttributes(false).Length);

            var ci    = constructor2;
            var attrs = ci.GetCustomAttributes(false);

            Assert.AreEqual(1, attrs.Length);
            Assert.AreEqual(typeof(ImportingConstructorAttribute), attrs[0].GetType());
        }
        public void MapType_OverridingSelectionOfConventionSelectedConstructor_WithPartBuilderOfT()
        {
            var builder = new RegistrationBuilder();

            builder.ForTypesDerivedFrom <IFoo>()
            .Export <IFoo>();

            builder.ForType <FooImplWithConstructors2>().
            SelectConstructor(param => new FooImplWithConstructors2(param.Import <IEnumerable <IFoo> >()));

            TypeInfo projectedType1 = builder.MapType(typeof(FooImpl).GetTypeInfo().GetTypeInfo());
            TypeInfo projectedType2 = builder.MapType(typeof(FooImplWithConstructors2).GetTypeInfo().GetTypeInfo());

            ConstructorInfo constructor1 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 0).Single();
            ConstructorInfo constructor2 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 1).Single();
            ConstructorInfo constructor3 = projectedType2.GetConstructors().Where(c => c.GetParameters().Length == 2).Single();

            // necessary as BuildConventionConstructorAttributes is only called for type level query for attributes
            var typeLevelAttrs = projectedType2.GetCustomAttributes(false);

            Assert.Equal(0, constructor1.GetCustomAttributes(false).Length);
            Assert.Equal(0, constructor3.GetCustomAttributes(false).Length);

            ConstructorInfo ci    = constructor2;
            var             attrs = ci.GetCustomAttributes(false);

            Assert.Equal(1, attrs.Length);
            Assert.IsType <ImportingConstructorAttribute>(attrs[0]);
        }
        private static void AssertHasAttributesUnderConvention <TSource>(RegistrationBuilder convention, IEnumerable <object> expected)
        {
            TypeInfo mapped  = convention.MapType(typeof(TSource).GetTypeInfo());
            var      applied = mapped.GetCustomAttributes(true);

            // Was: CollectionAssert.AreEquivalent(expected, applied) - output is not much good.
            AssertEquivalentAttributes(expected, applied);
        }
        private static void AssertHasAttributesUnderConvention <TSource>(Expression <Func <TSource, object> > property, RegistrationBuilder convention, IEnumerable <object> expected)
        {
            TypeInfo     mapped  = convention.MapType(typeof(TSource).GetTypeInfo());
            PropertyInfo pi      = GetPropertyFromAccessor(property);
            var          applied = mapped.GetProperty(pi.Name).GetCustomAttributes(true);

            // Was: CollectionAssert.AreEquivalent(expected, applied) - output is not much good.
            AssertEquivalentAttributes(expected, applied);
        }
        public void ConstructorParameterConvention_ImportOnDeclaration_ConventionIgnored()
        {
            RegistrationBuilder convention = ConfigureImportConstructorParameterConvention <ConstructorParameterImportContractX>();
            TypeInfo            mapped     = convention.MapType(typeof(ConstructorParameterImportContractX).GetTypeInfo());
            ParameterInfo       pi         = mapped.GetConstructors().Single().GetParameters().Single();
            var actual = pi.GetCustomAttributes(true);

            AssertEquivalentAttributes(new object[] { new ImportAttribute(ContractNames.ContractX) }, actual);
        }
        public void ConstructorParameterConvention_NoOverride_ConventionApplied()
        {
            RegistrationBuilder convention = ConfigureImportConstructorParameterConvention <NoConstructorParameterOverrides>();
            TypeInfo            mapped     = convention.MapType(typeof(NoConstructorParameterOverrides).GetTypeInfo());
            ParameterInfo       pi         = mapped.GetConstructors().Single().GetParameters().Single();
            var actual = pi.GetCustomAttributes(true);

            AssertEquivalentAttributes(ImportParameterConventionAttributes, actual);
        }
        public void ConstructorConvention_OverrideOnDeclaration_ConventionIgnored()
        {
            var rb = new RegistrationBuilder();

            rb.ForType <TwoConstructorsWithOverride>()
            .SelectConstructor(pi => new TwoConstructorsWithOverride(pi.Import <IContractA>(), pi.Import <IContractB>()));

            TypeInfo mapped = rb.MapType(typeof(TwoConstructorsWithOverride).GetTypeInfo());

            ConstructorInfo conventional      = mapped.GetConstructor(new[] { rb.MapType(typeof(IContractA).GetTypeInfo()), rb.MapType(typeof(IContractB).GetTypeInfo()) });
            var             conventionalAttrs = conventional.GetCustomAttributes(true);

            Assert.False(conventionalAttrs.Any());

            ConstructorInfo overridden     = mapped.GetConstructor(new[] { rb.MapType(typeof(IContractA).GetTypeInfo()) });
            var             overriddenAttr = overridden.GetCustomAttributes(true).Single();

            Assert.Equal(new ImportingConstructorAttribute(), overriddenAttr);
        }
        public void MapType_ShouldReturnProjectedAttributesForType()
        {
            var builder = new RegistrationBuilder();

            builder.ForTypesDerivedFrom <IFoo>()
            .Export <IFoo>();

            TypeInfo projectedType1 = builder.MapType(typeof(FooImpl).GetTypeInfo());
            TypeInfo projectedType2 = builder.MapType(typeof(FooImplWithConstructors).GetTypeInfo());

            var exports = new List <object>();

            exports.AddRange(projectedType1.GetCustomAttributes(typeof(ExportAttribute), false));
            exports.AddRange(projectedType2.GetCustomAttributes(typeof(ExportAttribute), false));
            Assert.Equal(2, exports.Count);

            foreach (var exportAttribute in exports)
            {
                Assert.Equal(typeof(IFoo), ((ExportAttribute)exportAttribute).ContractType);
                Assert.Null(((ExportAttribute)exportAttribute).ContractName);
            }
        }
        public void WhenExportingInterfaces_PredicateSpecified_OnlyContractInterfacesAreSeenByThePredicate()
        {
            var seenInterfaces = new List <Type>();

            var rb = new RegistrationBuilder();

            rb.ForType <ClassWithLifetimeConcerns>()
            .ExportInterfaces(i => { seenInterfaces.Add(i); return(true); });

            rb.MapType(typeof(ClassWithLifetimeConcerns).GetTypeInfo());

            Primitives.ComposablePartDefinition part = new TypeCatalog(new[] { typeof(ClassWithLifetimeConcerns) }, rb).Single();

            Assert.Equal(s_contractInterfaces, seenInterfaces);
        }
        public void WhenExportingInterfaces_PredicateSpecified_OnlyContractInterfacesAreSeenByThePredicate()
        {
            var seenInterfaces = new List <Type>();

            var rb = new RegistrationBuilder();

            rb.ForType <ClassWithLifetimeConcerns>()
            .ExportInterfaces(i => { seenInterfaces.Add(i); return(true); });

            rb.MapType(typeof(ClassWithLifetimeConcerns).GetTypeInfo());

            var part = new TypeCatalog(new[] { typeof(ClassWithLifetimeConcerns) }, rb).Single();

            CollectionAssert.AreEquivalent(ContractInterfaces, seenInterfaces);
        }
        public void ConventionsInInnerAndOuterRCs_InnerRCTakesPrecendence()
        {
            var innerConvention = new RegistrationBuilder();

            innerConvention.ForType <ConventionTarget>().Export(eb => eb.AsContractName(ContractNames.ContractX));
            TypeInfo innerType = innerConvention.MapType(typeof(ConventionTarget).GetTypeInfo());

            var outerConvention = new RegistrationBuilder();

            outerConvention.ForType <ConventionTarget>().Export(eb => eb.AsContractName(ContractNames.ContractY));
            TypeInfo outerType = outerConvention.MapType(innerType /*.GetTypeInfo()*/);

            ExportAttribute export = outerType.GetCustomAttributes(false).OfType <ExportAttribute>().Single();

            Assert.Equal(ContractNames.ContractX, export.ContractName);
        }