Exemplo n.º 1
0
        public void PartCreatorConstraint_ShouldMatchPartCreatorExportDefinition()
        {
            var partCreatorImportDef = ReflectionModelServices.CreateImportDefinition(
                new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ConstraintServicesTests) }),
                "Foo",
                "Foo",
                new KeyValuePair <string, Type>[] { new KeyValuePair <string, Type>("MDKey", typeof(string)) },
                ImportCardinality.ZeroOrMore,
                false,
                CreationPolicy.Any,
                true, // IsPartCreator
                null);

            var metadata = new Dictionary <string, object>();

            metadata["MDKey"] = "MDValue";
            metadata[CompositionConstants.ExportTypeIdentityMetadataName] = "Foo";

            var productExportDefinition = new ExportDefinition("Foo", metadata);

            metadata = new Dictionary <string, object>(metadata);
            metadata[CompositionConstants.ExportTypeIdentityMetadataName] = CompositionConstants.PartCreatorTypeIdentity;
            metadata[CompositionConstants.ProductDefinitionMetadataName]  = productExportDefinition;

            var exportDefinition = new ExportDefinition(CompositionConstants.PartCreatorContractName, metadata);

            var predicate = partCreatorImportDef.Constraint.Compile();

            Assert.IsTrue(partCreatorImportDef.IsConstraintSatisfiedBy(exportDefinition));
            Assert.IsTrue(predicate(exportDefinition));
        }
Exemplo n.º 2
0
        static IEnumerable <ImportDefinition> GetImportDefinitions(Type implementationType)
        {
            var constructors = implementationType.GetConstructors()[0];
            var imports      = new List <ImportDefinition>();

            foreach (var param in constructors.GetParameters())
            {
                var parameter   = param;
                var cardinality = GetCardinality(parameter);
                var importType  = cardinality == ImportCardinality.ZeroOrMore
                                                        ? GetCollectionContractType(parameter.ParameterType)
                                                        : param.ParameterType;

                imports.Add(
                    ReflectionModelServices.CreateImportDefinition(
                        new Lazy <ParameterInfo>(() => parameter),
                        AttributedModelServices.GetContractName(importType),
                        AttributedModelServices.GetTypeIdentity(importType),
                        Enumerable.Empty <KeyValuePair <string, Type> >(),
                        cardinality,
                        CreationPolicy.Any,
                        null)
                    );
            }

            return(imports);
        }
Exemplo n.º 3
0
 private ImportDefinition CreateWrapped(ContractBasedImportDefinition import, Type type)
 {
     if (ReflectionModelServices.IsImportingParameter(import))
     {
         return(ReflectionModelServices.CreateImportDefinition(
                    this.CreateWrapped(ReflectionModelServices.GetImportingParameter(import), type),
                    import.ContractName,
                    import.RequiredTypeIdentity,
                    import.RequiredMetadata,
                    import.Cardinality,
                    import.RequiredCreationPolicy,
                    null));
     }
     else
     {
         return(ReflectionModelServices.CreateImportDefinition(
                    this.CreateWrapped(ReflectionModelServices.GetImportingMember(import), type),
                    import.ContractName,
                    import.RequiredTypeIdentity,
                    import.RequiredMetadata,
                    import.Cardinality,
                    import.IsRecomposable,
                    import.RequiredCreationPolicy,
                    null));
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Gets an <see cref="ImportDefinition"/> for a <see cref="MemberInfo"/> instance using the provided <see cref="IImportConvention"/> instance.
 /// </summary>
 /// <param name="importConvention"><see cref="IImportConvention"/> instances that should be used to create the <see cref="ImportDefinition"/> instances.</param>
 /// <param name="member">The <see cref="MemberInfo"/> for which the <see cref="ImportDefinition"/> instances should be created.</param>
 /// <returns>An <see cref="ImportDefinition"/> instance.</returns>
 private ImportDefinition GetImportDefinition(IImportConvention importConvention, MemberInfo member)
 {
     return(ReflectionModelServices.CreateImportDefinition(
                member.ToLazyMemberInfo(),
                this.ContractService.GetImportContractName(importConvention, member),
                this.ContractService.GetImportTypeIdentity(importConvention, member),
                importConvention.RequiredMetadata.Select(x => new KeyValuePair <string, Type>(x.Name, x.Type)),
                member.GetCardinality(importConvention.AllowDefaultValue),
                importConvention.Recomposable,
                importConvention.CreationPolicy,
                null));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Gets an <see cref="ImportDefinition"/> for a <see cref="ParameterInfo"/> instance using the provided <see cref="IImportConvention"/> instance.
        /// </summary>
        /// <param name="importConvention"><see cref="IImportConvention"/> instances that should be used to create the <see cref="ImportDefinition"/> instances.</param>
        /// <param name="parameter">The <see cref="ParameterInfo"/> for which the <see cref="ImportDefinition"/> instances should be created.</param>
        /// <returns>An <see cref="ImportDefinition"/> instance.</returns>
        private ImportDefinition GetImportParameterDefinition(IImportConvention importConvention, ParameterInfo parameter)
        {
            var actualType =
                parameter.ParameterType.GetActualType();

            return(ReflectionModelServices.CreateImportDefinition(
                       new Lazy <ParameterInfo>(() => parameter),
                       this.ContractService.GetImportContractName(importConvention, actualType),
                       this.ContractService.GetImportTypeIdentity(importConvention, actualType),
                       null,
                       parameter.ParameterType.GetCardinality(importConvention.AllowDefaultValue),
                       importConvention.CreationPolicy,
                       null));
        }
Exemplo n.º 6
0
        public void PartCreator_QueryContainerDirectly_ShouldWork()
        {
            var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Foo));

            var importDef = ReflectionModelServices.CreateImportDefinition(
                new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(PartCreatorTests) }), // Give it a bogus member
                AttributedModelServices.GetContractName(typeof(Foo)),
                AttributedModelServices.GetTypeIdentity(typeof(Foo)),
                Enumerable.Empty <KeyValuePair <string, Type> >(),
                ImportCardinality.ZeroOrMore,
                true,
                CreationPolicy.Any,
                true, // isPartCreator
                null);

            var exports = container.GetExports(importDef);

            var partCreator = exports.Single();

            // Manually walk the steps of using a raw part creator which is modeled as a PartDefinition with
            // a single ExportDefinition.
            var partDef = (ComposablePartDefinition)partCreator.Value;
            var part    = partDef.CreatePart();
            var foo     = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single());

            Assert.IsNotNull(foo);

            var foo1 = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single());

            Assert.AreEqual(foo, foo1, "Retrieving the exported value from the same part should return the same value");

            // creating a new part should result in getting a new exported value
            var part2 = partDef.CreatePart();
            var foo2  = (Foo)part2.GetExportedValue(partDef.ExportDefinitions.Single());

            Assert.AreNotEqual(foo, foo2, "New part should equate to a new exported value");

            // Disposing of part should cause foo to be disposed
            ((IDisposable)part).Dispose();
            Assert.IsTrue(foo.IsDisposed);
        }
Exemplo n.º 7
0
        private IEnumerable <ImportDefinition> GetImportDefinitions(Type implementationType)
        {
            var imports            = new List <ImportDefinition>();
            var defaultConstructor = implementationType.GetConstructors().FirstOrDefault();

            if (defaultConstructor != null)
            {
                foreach (var param in defaultConstructor.GetParameters())
                {
                    imports.Add(
                        ReflectionModelServices.CreateImportDefinition(
                            new Lazy <ParameterInfo>(() => param),
                            AttributedModelServices.GetContractName(param.ParameterType),
                            AttributedModelServices.GetTypeIdentity(param.ParameterType),
                            Enumerable.Empty <KeyValuePair <string, Type> >(),
                            ImportCardinality.ExactlyOne,
                            CreationPolicy.Any,
                            null));
                }
            }

            return(imports);
        }
Exemplo n.º 8
0
        private void RewriteContract(Type typeToDecorate, IList <ImportDefinition> importDefs, string newContract)
        {
            var importToDecorate = importDefs.SingleOrDefault(d => d.ContractName == myContractName);

            Contract.Requires(importToDecorate != null, "No import found for contract {0} on type {1}", myContractName, typeToDecorate);

            importDefs.Remove(importToDecorate);

            Contract.Invariant(importToDecorate.Cardinality == ImportCardinality.ExactlyOne, "Decoration of Cardinality " + importToDecorate.Cardinality + " not supported");
            Contract.Invariant(ReflectionModelServices.IsImportingParameter(importToDecorate), "Decoration of property injection not supported");

            var param     = ReflectionModelServices.GetImportingParameter(importToDecorate);
            var importDef = ReflectionModelServices.CreateImportDefinition(
                param,
                newContract,
                AttributedModelServices.GetTypeIdentity(param.Value.ParameterType),
                Enumerable.Empty <KeyValuePair <string, Type> >(),
                importToDecorate.Cardinality,
                CreationPolicy.Any,
                null);

            importDefs.Add(importDef);
        }
Exemplo n.º 9
0
        internal static ImportDefinition CreateImportDefinition(SerializableImportDefinition serializableImportDefinition)
        {
            if (serializableImportDefinition.ImportingMember.MemberType == MemberTypes.Constructor)
            {
                var lazyParameter = new Lazy <ParameterInfo>(() =>
                {
                    Type declaringType = Type.GetType(serializableImportDefinition.ImportingMember.DeclaringTypeAssemblyQualifiedName);
                    return(MefUtils.GetImportingConstructor(declaringType).GetParameters().Single(x => x.Name == serializableImportDefinition.ImportingMember.MemberName));
                });

                return(ReflectionModelServices.CreateImportDefinition(
                           lazyParameter,
                           serializableImportDefinition.ContractName,
                           serializableImportDefinition.RequiredTypeIdentity,
                           serializableImportDefinition.RequiredMetadata.Select(kvp => new KeyValuePair <string, Type>(kvp.Key, Type.GetType(kvp.Value))),
                           serializableImportDefinition.Cardinality,
                           serializableImportDefinition.RequiredCreationPolicy,
                           serializableImportDefinition.Metadata,
                           serializableImportDefinition.IsExportFactory,
                           null)); // TODO: Is it OK to have null for origin?
            }
            else
            {
                return(ReflectionModelServices.CreateImportDefinition(
                           CreateLazyMemberInfo(serializableImportDefinition.ImportingMember),
                           serializableImportDefinition.ContractName,
                           serializableImportDefinition.RequiredTypeIdentity,
                           serializableImportDefinition.RequiredMetadata.Select(kvp => new KeyValuePair <string, Type>(kvp.Key, Type.GetType(kvp.Value))),
                           serializableImportDefinition.Cardinality,
                           serializableImportDefinition.IsRecomposable,
                           serializableImportDefinition.IsPrerequisite,
                           serializableImportDefinition.RequiredCreationPolicy,
                           serializableImportDefinition.Metadata,
                           serializableImportDefinition.IsExportFactory,
                           null)); // TODO: Is it OK to have null for origin?
            }
        }
        public static ContractBasedImportDefinition ReadImportDefinition(ComposablePartDefinition owner, IDictionary <string, object> cache)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(cache);

            Lazy <Type>         partType = ReflectionModelServices.GetPartType(owner);
            ICompositionElement origin   = owner as ICompositionElement;

            if (cache.ReadValue <string>(AttributedCacheServices.CacheKeys.ImportType) == AttributedCacheServices.ImportTypes.Parameter)
            {
                return(ReflectionModelServices.CreateImportDefinition(
                           cache.ReadLazyParameter(partType),
                           cache.ReadContractName(),
                           cache.ReadValue <string>(AttributedCacheServices.CacheKeys.RequiredTypeIdentity),
                           cache.ReadRequiredMetadata(),
                           cache.ReadValue <ImportCardinality>(AttributedCacheServices.CacheKeys.Cardinality, ImportCardinality.ExactlyOne),
                           cache.ReadValue <CreationPolicy>(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, CreationPolicy.Any),
                           origin));
            }
            else
            {
                LazyMemberInfo importingMemberInfo = new LazyMemberInfo(
                    cache.ReadValue <MemberTypes>(AttributedCacheServices.CacheKeys.MemberType, MemberTypes.Property),
                    cache.ReadLazyAccessors(partType));

                return(ReflectionModelServices.CreateImportDefinition(
                           importingMemberInfo,
                           cache.ReadContractName(),
                           cache.ReadValue <string>(AttributedCacheServices.CacheKeys.RequiredTypeIdentity),
                           cache.ReadRequiredMetadata(),
                           cache.ReadValue <ImportCardinality>(AttributedCacheServices.CacheKeys.Cardinality, ImportCardinality.ExactlyOne),
                           cache.ReadValue <bool>(AttributedCacheServices.CacheKeys.IsRecomposable, false),
                           cache.ReadValue <CreationPolicy>(AttributedCacheServices.CacheKeys.RequiredCreationPolicy, CreationPolicy.Any),
                           origin));
            }
        }