public ConstructorInjectionComposablePart(Type type)
        {
            this._type = type;

            // Note that this just blindly takes the first constructor...
            this._constructor = this._type.GetConstructors().FirstOrDefault();
            Assert.IsNotNull(this._constructor);

            foreach (var param in this._constructor.GetParameters())
            {
                string name = AttributedModelServices.GetContractName(param.ParameterType);
                AddImport(name, ImportCardinality.ExactlyOne, true, true);
            }

            string contractName = AttributedModelServices.GetContractName(type);
            string typeIdentity = AttributedModelServices.GetTypeIdentity(type);
            var    metadata     = new Dictionary <string, object>();

            metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity);

            Export composableExport = ExportFactory.Create(
                contractName,
                metadata,
                GetInstance);

            this.AddExport(composableExport);

            this._imports = new Dictionary <ImportDefinition, object>();
        }
        public new  void AddExport(string contractName, object value)
        {
            var export = ExportFactory.Create(contractName, () =>
            {
                this.OnGetExport(contractName); return(value);
            });

            base.AddExport(export);
        }
Ejemplo n.º 3
0
        public void AddExportedValue_ExportAsExportedValueArgument_ShouldBeWrappedInExport()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            batch.AddExportedValue(export);
            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Same(export, this.GetSingleLazy <Export>(batch.PartsToAdd[0]).Value);
        }
Ejemplo n.º 4
0
        public void AddExport_ReturnedComposablePart_IsInAddedPartsCollection()
        {
            CompositionBatch batch = new CompositionBatch();

            var export = ExportFactory.Create("Contract", "Value");
            var part   = batch.AddExport(export);

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Equal("Value", this.GetSingleExport(batch.PartsToAdd[0], "Contract").Value);
            Assert.Contains(part, batch.PartsToAdd);
        }
Ejemplo n.º 5
0
        public void AddExport_ExportWithNullExportedValueAsExportArgument_CanBeExported()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", (object)null);

            batch.AddExport(export);
            Assert.Equal(1, batch.PartsToAdd.Count);

            var result = this.GetSingleExport(batch.PartsToAdd[0], "Contract");

            Assert.NotNull(result);
            Assert.Null(result.Value);
        }
Ejemplo n.º 6
0
        public void AddExport_ReturnedComposablePart_NullAsDefinitionArgumentToSetImports_ShouldThrowArgumentNull()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            var part = batch.AddExport(export);

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Throws <ArgumentNullException>("definition", () =>
            {
                part.SetImport((ImportDefinition)null, Enumerable.Empty <Export>());
            });
        }
Ejemplo n.º 7
0
        public void AddExport_ExportWithEmptyMetadata_IsExportedWithEmptyMetadata()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value", new Dictionary <string, object>());

            Assert.Equal(0, export.Metadata.Count);

            batch.AddExport(export);
            Assert.Equal(1, batch.PartsToAdd.Count);

            var result = this.GetSingleExport(batch.PartsToAdd[0], "Contract");

            Assert.Equal(0, result.Metadata.Count);
        }
Ejemplo n.º 8
0
        public void AddExport_ReturnedComposablePart_NullAsDefinitionArgumentToGetExportedValue_ShouldThrowArgumentNull()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            var part = batch.AddExport(export);

            Assert.AreEqual(1, batch.PartsToAdd.Count);

            ExceptionAssert.ThrowsArgument <ArgumentNullException>("definition", () =>
            {
                part.GetExportedValue((ExportDefinition)null);
            });
        }
Ejemplo n.º 9
0
        public void AddExport_ReturnedComposablePart_ExportsArrayWithNullElementAsExportsArgumentToSetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            var part       = batch.AddExport(export);
            var definition = ImportDefinitionFactory.Create();

            Assert.Equal(1, batch.PartsToAdd.Count);

            Assert.Throws <ArgumentException>("exports", () =>
            {
                part.SetImport(definition, new Export[] { null });
            });
        }
Ejemplo n.º 10
0
        public void AddExport_ReturnedComposablePart_SetImports_ShouldThrowArgument()
        {
            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value");

            var part       = batch.AddExport(export);
            var definition = ImportDefinitionFactory.Create();

            Assert.AreEqual(1, batch.PartsToAdd.Count);

            ExceptionAssert.ThrowsArgument <ArgumentException>("definition", () =>
            {
                part.SetImport(definition, Enumerable.Empty <Export>());
            });
        }
Ejemplo n.º 11
0
            public void VerifyImport(params int[] expectedValues)
            {
                object[] untypedExpectedValues = expectedValues.Cast <object>().ToArray();

                ExportsAssert.AreEqual(CollectionPlain, untypedExpectedValues);
                ExportsAssert.AreEqual(CollectionPlainRawMetadata, untypedExpectedValues);
                EnumerableAssert.IsTrueForAll(CollectionPlainRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
                EnumerableAssert.IsEmpty(CollectionPlainEmpty);
                EnumerableAssert.IsEmpty(CollectionPlainEmptyRawMetadata);

                // Add a new Export to this collection to ensure that it doesn't
                // modifiy the other collections because they should each have there
                // own collection instance
                CollectionPlain.Add(ExportFactory.Create <object>("Value"));

                ExportsAssert.AreEqual(CollectionTyped, expectedValues);
                ExportsAssert.AreEqual(CollectionTypedRawMetadata, expectedValues);
                EnumerableAssert.IsTrueForAll(CollectionTypedRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
                EnumerableAssert.IsEmpty(CollectionTypedEmpty);

                ExportsAssert.AreEqual(CollectionTypedMetadata, expectedValues);
#if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
                EnumerableAssert.IsTrueForAll(CollectionTypedMetadata, i => true == i.Metadata.PropertyName);
#endif //!SILVERLIGHT
                EnumerableAssert.IsEmpty(CollectionTypedMetadataEmpty);

                EnumerableAssert.AreEqual(ReadWriteEnumerable, expectedValues);
                EnumerableAssert.IsEmpty(ReadWriteEnumerableEmpty);

                ExportsAssert.AreEqual(MetadataUntypedEnumerable, untypedExpectedValues);
                ExportsAssert.AreEqual(MetadataUntypedEnumerableRawMetadata, untypedExpectedValues);
                EnumerableAssert.IsTrueForAll(MetadataUntypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
                EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmpty);
                EnumerableAssert.IsEmpty(MetadataUntypedEnumerableEmptyRawMetadata);

                ExportsAssert.AreEqual(MetadataTypedEnumerable, expectedValues);
                ExportsAssert.AreEqual(MetadataTypedEnumerableRawMetadata, expectedValues);
                EnumerableAssert.IsTrueForAll(MetadataTypedEnumerableRawMetadata, i => true.Equals(i.Metadata["PropertyName"]));
                EnumerableAssert.IsEmpty(MetadataTypedEnumerableEmpty);

                ExportsAssert.AreEqual(MetadataFullyTypedEnumerable, expectedValues);
#if !SILVERLIGHT // Silverlight doesn't support strongly typed metadata
                EnumerableAssert.IsTrueForAll(MetadataFullyTypedEnumerable, i => true == i.Metadata.PropertyName);
#endif //!SILVERLIGHT
                EnumerableAssert.IsEmpty(MetadataFullyTypedEnumerableEmpty);
            }
Ejemplo n.º 12
0
        public void AddExport_ReturnedComposablePart_ContainsExportDefinitionRepresentingExport()
        {
            var metadata = new Dictionary <string, object>();

            metadata["Name"] = "Value";

            CompositionBatch batch = new CompositionBatch();
            var export             = ExportFactory.Create("Contract", "Value", metadata);

            var part = batch.AddExport(export);

            Assert.Equal(1, batch.PartsToAdd.Count);

            var definition = part.ExportDefinitions.Single();

            Assert.Equal("Contract", definition.ContractName);
            Assert.Equal("Value", part.GetExportedValue(definition));
            EnumerableAssert.AreEqual(metadata, definition.Metadata);
        }
Ejemplo n.º 13
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void InAdditionToCatalogTest()
        {
            var container = ContainerFactory.CreateWithDefaultAttributedCatalog();

            IDictionary <string, object> multMetadata = new Dictionary <string, object>();

            multMetadata["Var1"] = "mult";
            multMetadata[CompositionConstants.ExportTypeIdentityMetadataName] = AttributedModelServices.GetTypeIdentity(typeof(Func <int, int, int>));
            var basicValue = ExportFactory.Create("Add", multMetadata, (() => (Func <int, int, int>) delegate(int a, int b)
                                                                        { return(a * b); }));

            CompositionBatch batch = new CompositionBatch();

            batch.AddExport(basicValue);
            container.Compose(batch);

            var exports = container.GetExports <Func <int, int, int>, ITrans_ExportableTest>("Add");

            Assert.Equal(3, exports.Count());

            foreach (var export in exports)
            {
                if (export.Metadata.Var1 == "mult")
                {
                    Assert.Equal(2, export.Value(1, 2));
                }
                else if (export.Metadata.Var1 == "add")
                {
                    Assert.Equal(3, export.Value(1, 2));
                }
                else if (export.Metadata.Var1 == "sub")
                {
                    Assert.Equal(-1, export.Value(1, 2));
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }
Ejemplo n.º 14
0
        public void InAdditionToCatalogTest()
        {
            var container = ContainerFactory.CreateWithDefaultAttributedCatalog();

            IDictionary <string, object> multMetadata = new Dictionary <string, object>();

            multMetadata["Var1"] = "mult";
            multMetadata[CompositionConstants.ExportTypeIdentityMetadataName] = AttributedModelServices.GetTypeIdentity(typeof(Func <int, int, int>));
            var basicValue = ExportFactory.Create("Add", multMetadata, (() => (Func <int, int, int>) delegate(int a, int b) { return(a * b); }));

            CompositionBatch batch = new CompositionBatch();

            batch.AddExport(basicValue);
            container.Compose(batch);

            var exports = container.GetExports <Func <int, int, int>, IExportableTest>("Add");

            Assert.AreEqual(3, exports.Count(), "There should be 3 entries for 'Add'");

            foreach (var export in exports)
            {
                if (export.Metadata.Var1 == "mult")
                {
                    Assert.AreEqual(2, export.Value(1, 2), "1 * 2 == 2");
                }
                else if (export.Metadata.Var1 == "add")
                {
                    Assert.AreEqual(3, export.Value(1, 2), "1 + 2 == 3");
                }
                else if (export.Metadata.Var1 == "sub")
                {
                    Assert.AreEqual(-1, export.Value(1, 2), "1 - 2 == -1");
                }
                else
                {
                    Assert.Fail("Unexpected value");
                }
            }
        }
Ejemplo n.º 15
0
 SelfExportWithExportFactoryParameterImport(ExportFactory <IImport <T1, T2> > value)
 {
     this.Value = value;
 }
 public void AddExport(string contractName, object value)
 {
     this.AddExport(ExportFactory.Create(contractName, () => value));
 }
 public MyEmptyClassWithFactoryConstructor([Import] ExportFactory <IContract> myFactoryProperty)
 {
     this.MyFactoryProperty = myFactoryProperty;
 }
Ejemplo n.º 18
0
 public MyEmptyClassWithFactoryConstructor([Import(RequiredCreationPolicy = CreationPolicy.NewScope)] ExportFactory <IContract> myFactoryProperty)
 {
     this.MyFactoryProperty = myFactoryProperty;
 }