public void TestSetUp()
        {
            var mockInterceptor = new Mock <IExportedValueInterceptor>();

            interceptedPartDefinition  = AttributedModelServices.CreatePartDefinition(typeof(OrderProcessor), null);
            interceptingPartDefinition = new InterceptingComposablePartDefinition(interceptedPartDefinition, mockInterceptor.Object);
        }
 public NonFilteringTypeCatalog(params Type[] types)
 {
     this._definitions = new List <ComposablePartDefinition>();
     foreach (Type type in types)
     {
         this._definitions.Add(AttributedModelServices.CreatePartDefinition(type, null));
     }
 }
示例#3
0
        public void CreatePartDefinition2_NullAsType_ShouldThrowArgumentNull()
        {
            var origin = ElementFactory.Create();

            Assert.Throws <ArgumentNullException>("type", () =>
            {
                AttributedModelServices.CreatePartDefinition((Type)null, origin, false);
            });
        }
        public void When_calling_create_part_it_should_create_a_disposable_intercepting_part()
        {
            var mockInterceptor = new Mock <IExportedValueInterceptor>();

            interceptedPartDefinition  = AttributedModelServices.CreatePartDefinition(typeof(DisposablePart), null);
            interceptingPartDefinition = new InterceptingComposablePartDefinition(interceptedPartDefinition, mockInterceptor.Object);
            var part = interceptingPartDefinition.CreatePart();

            part.ShouldBeOfType <DisposableInterceptingComposablePart>();
        }
        public void CreatePartDefinition2_TypeMarkedWithPartNotDiscoverableAttribute_ShouldTraceInformation()
        {
            var types = GetTypesMarkedWithPartNotDiscoverableAttribute();

            foreach (Type type in types)
            {
                using (TraceContext context = new TraceContext(SourceLevels.Information))
                {
                    AttributedModelServices.CreatePartDefinition(type, null, true);

                    Assert.IsNotNull(context.LastTraceEvent);
                    Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
                    Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionMarkedWithPartNotDiscoverableAttribute);
                }
            }
        }
        public void CreatePartDefinition2_TypeWithNoExports_ShouldTraceInformation()
        {
            var types = GetTypesWithNoExports();

            foreach (Type type in types)
            {
                using (TraceContext context = new TraceContext(SourceLevels.Information))
                {
                    var result = AttributedModelServices.CreatePartDefinition(type, null, true);

                    Assert.IsNotNull(context.LastTraceEvent);
                    Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
                    Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionContainsNoExports);
                }
            }
        }
        public void CreatePartDefinition2_OpenGenericType_ShouldTraceInformation()
        {
            var types = GetOpenGenericTypesWithMismatchedArity();

            foreach (Type type in types)
            {
                using (TraceContext context = new TraceContext(SourceLevels.Information))
                {
                    AttributedModelServices.CreatePartDefinition(type, null, true);

                    Assert.IsNotNull(context.LastTraceEvent);
                    Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
                    Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionMismatchedExportArity);
                }
            }
        }
        private void ComposeDecoration()
        {
            if (myParts.Any())
            {
                return;
            }

            var contracts = new List <string>();

            foreach (var type in myDecoratorChain)
            {
                var originalPart = AttributedModelServices.CreatePartDefinition(type, null);

                var importDefs = originalPart.ImportDefinitions.ToList();

                if (type != myDecoratorChain.First())
                {
                    RewriteContract(type, importDefs, contracts.Last());
                }

                var exportDefs = originalPart.ExportDefinitions.ToList();

                if (type != myDecoratorChain.Last())
                {
                    contracts.Add(Guid.NewGuid().ToString());
                    RewriteContract(type, exportDefs, type, contracts.Last());
                }

                // as we pass it to lazy below we have to copy it to local variable - otherwise we create a closure with the loop iterator variable
                // and this will cause the actual part type to be changed
                var partType = type;
                var part     = ReflectionModelServices.CreatePartDefinition(
                    new Lazy <Type>(() => partType),
                    ReflectionModelServices.IsDisposalRequired(originalPart),
                    new Lazy <IEnumerable <ImportDefinition> >(() => importDefs),
                    new Lazy <IEnumerable <ExportDefinition> >(() => exportDefs),
                    new Lazy <IDictionary <string, object> >(() => new Dictionary <string, object>()),
                    null);

                myParts.Add(part);
            }

            // no add possible any longer
            myDecoratorChain = null;
        }
示例#9
0
        private ComposablePartDefinition GetPartForType(Type type)
        {
            ComposablePartDefinition ret;

            if (_typeToPart.TryGetValue(type, out ret))
            {
                return(ret);
            }

            ret = AttributedModelServices.CreatePartDefinition(type, new TypeCompositionElement(type), false);

            if (!ret.ExportDefinitions.Any() && !ret.ImportDefinitions.Any())
            {
                ret = null;
            }

            _typeToPart[type] = ret;
            return(ret);
        }
示例#10
0
        /// <summary>
        /// Register a MEF-attributed type as a component.
        /// </summary>
        /// <param name="builder">The container builder.</param>
        /// <param name="partType">The attributed type to register.</param>
        /// <param name="exposedServicesMapper">A mapping function to transform ExportDefinitions into Services.</param>
        public static void RegisterComposablePartType(this ContainerBuilder builder, Type partType, Func <ExportDefinition, IEnumerable <Service> > exposedServicesMapper)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (partType == null)
            {
                throw new ArgumentNullException(nameof(partType));
            }
            if (exposedServicesMapper == null)
            {
                throw new ArgumentNullException(nameof(exposedServicesMapper));
            }

            RegisterComposablePartDefinition(
                builder,
                AttributedModelServices.CreatePartDefinition(partType, null, true),
                exposedServicesMapper);
        }
        public void CreatePartDefinition_NotEnsureIsDiscoverable()
        {
            var expectations = new ExpectationCollection <Type, bool>();

            expectations.Add(typeof(ClassWithTwoZeroParameterConstructors), true);
            expectations.Add(typeof(SimpleConstructorInjectedObject), true);
            expectations.Add(typeof(StaticExportClass), true);
            expectations.Add(typeof(PublicComponentWithPublicExports), true);
            expectations.Add(typeof(ClassWithMultipleMarkedConstructors), true);
            expectations.Add(typeof(ClassWithNoMarkedOrDefaultConstructor), true);
            expectations.Add(typeof(ClassWhichOnlyHasDefaultConstructor), false);
            expectations.Add(typeof(ClassWithOnlyHasImportingConstructorButInherits), true);
            expectations.Add(typeof(ClassWithOnlyHasMultipleImportingConstructorButInherits), true);

            foreach (var e in expectations)
            {
                var definition = AttributedModelServices.CreatePartDefinition(e.Input, null, false);
                Assert.NotNull(definition);
            }
        }
        private static ComposablePartDefinition CreatePartDefinition(IEnumerable <ImportDefinition> ctorImports, ExportDefinition contractExport, Type type)
        {
            var originalPartDefinition = AttributedModelServices.CreatePartDefinition(type, null);

            if (originalPartDefinition == null)
            {
                throw new InvalidOperationException();
            }

            var imports = originalPartDefinition.ImportDefinitions
                          .Where(idef => !ReflectionModelServices.IsImportingParameter(idef))
                          .Concat(ctorImports)
                          .ToList();

            var exports = originalPartDefinition.ExportDefinitions.ToList();

            exports.Add(contractExport);
            var metadata = originalPartDefinition.Metadata;

            return(CreatePartDefinition(type, imports, exports, metadata));
        }
示例#13
0
        private List <ComposablePartDefinition> GetParts()
        {
            var parts = new List <ComposablePartDefinition>();
            var types = SelectTypes();

            foreach (var type in types)
            {
                try
                {
                    var definition = AttributedModelServices.CreatePartDefinition(type, null, false);
                    if (definition != null)
                    {
                        parts.Add(definition);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }
            return(parts);
        }
        public void ImportDefinitions_TypeWithMemberMarkedWithMultipleImports_ShouldTraceError()
        {
            var types = new Type[] { typeof(ClassWithMultipleParameterImports),
                                     typeof(ClassWithMultipleFieldImports),
                                     typeof(ClassWithMultiplePropertyImports),
                                     typeof(ClassWithMultipleCustomPropertyImports),
                                     typeof(ClassWithMultipleCustomPropertyImportManys),
                                     typeof(ClassWithMultipleCustomPropertyImportAndImportManys) };

            foreach (Type type in types)
            {
                using (TraceContext context = new TraceContext(SourceLevels.Error))
                {
                    var definition = AttributedModelServices.CreatePartDefinition(type, null, true);
                    definition.ImportDefinitions.Count();

                    Assert.IsNotNull(context.LastTraceEvent);
                    Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Error);
                    Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_MemberMarkedWithMultipleImportAndImportMany);
                }
            }
        }
        public void MixedDuplicateExports_ShouldOnlyCollapseInheritedExport()
        {
            var def = AttributedModelServices.CreatePartDefinition(typeof(DuplicateMixedExporter1), null);

            Assert.AreEqual(2, def.ExportDefinitions.Count(), "Should have 1 from the Export and only 1 collapsed InhertedExport");
        }
示例#16
0
            private ComposablePartDefinition CreatePartDefinition(Type type)
            {
                ComposablePartDefinition partDefinition = AttributedModelServices.CreatePartDefinition(type, null);

                return(this.CreateWrapped(partDefinition, type));
            }
示例#17
0
 private static string[] GetDisplayNames(IEnumerable <Type> types)
 {
     return(GetDisplayNames(types.Select(t => AttributedModelServices.CreatePartDefinition(t, null))));
 }
        public void MixedDuplicateExports_ShouldOnlyCollapseInheritedExport()
        {
            var def = AttributedModelServices.CreatePartDefinition(typeof(DuplicateMixedExporter1), null);

            Assert.Equal(2, def.ExportDefinitions.Count());
        }
示例#19
0
 public MefSubstituteBuilder AddValueFactory <T1, T2, T3, T4, TValue>(string contractName, Func <T1, T2, T3, T4, TValue> valueFactory)
 {
     return(AddValueFactory(contractName, AttributedModelServices.CreatePartDefinition(typeof(ImportStub <T1, T2, T3, T4>), null), typeof(TValue), valueFactory));
 }