protected WebScopedComposablePart(ComposablePart composablePart)
        {
            _composablePart = composablePart;

            _exportDef = composablePart.ExportDefinitions.First();
            _key = ((string)_exportDef.Metadata["ExportTypeIdentity"]).Replace('.', '_');
        }
 /// <summary>
 /// Initializes the context from a part and the export.
 /// </summary>
 internal DecoratedExport(ComposablePartDefinition part, ExportDefinition export)
 {
     this.ExportDefinition = export;
     this.ExportingMember = ReflectionModelServices.GetExportingMember(export);
     this.ExportingType = ReflectionModelServices.GetPartType(part);
     this.NewMetadata = new Dictionary<string, object>(export.Metadata);
 }
 public PartCreatorExport(CatalogExportProvider catalogExportProvider, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition)
 {
     this._catalogExportProvider = catalogExportProvider;
     this._partDefinition = partDefinition;
     this._exportDefinition = exportDefinition;
     this._partCreatorExportDefinition = new PartCreatorExportDefinition(this._exportDefinition);
 }
Beispiel #4
0
        public static CompositionException CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException)
        {
            Assumes.NotNull(part, definition, innerException);

            return new CompositionException(
                ErrorBuilder.CreateCannotGetExportedValue(part, definition, innerException));
        }
 public CatalogExport(CatalogExportProvider catalogExportProvider,
     ComposablePartDefinition partDefinition, ExportDefinition definition)
 {
     this._catalogExportProvider = catalogExportProvider;
     this._partDefinition = partDefinition;
     this._definition = definition;
 }
        public ExportingMember(ExportDefinition definition, ReflectionMember member)
        {
            Assumes.NotNull(definition, member);

            this._definition = definition;
            this._member = member;
        }
 /// <summary>
 /// Recupera o valor exportado.
 /// </summary>
 /// <param name="definition"></param>
 /// <returns></returns>
 /// <exception cref="InvalidOperationException"></exception>
 public override object GetExportedValue(System.ComponentModel.Composition.Primitives.ExportDefinition definition)
 {
     try
     {
         ProviderExportDefinition export = definition as ProviderExportDefinition;
         if (export == null)
         {
             throw new InvalidOperationException("The supplied export definition was of an unknown type.");
         }
         if (export.Member == null)
         {
             throw new InvalidOperationException(string.Format("Not found member to export definition. {0}", export.ContractName));
         }
         ComposableMember exportable = export.Member.ToComposableMember();
         object           instance   = null;
         if (exportable.IsInstanceNeeded)
         {
             instance = this.GetActivatedInstance(exportable);
         }
         object value = exportable.GetValue(instance);
         return(value);
     }
     catch (Exception ex)
     {
         ComposablePartErrorHandler.NotifyGetExportedValueError(definition, ex);
         throw;
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="XElementExportProvider"/> class.
        /// Includes *.xml and *.config by default.
        /// </summary>
        /// <param name="path">The path. Defaults to Directory.GetCurrentDirectory()</param>
        /// <param name="filters">A list of additional file filters to include.</param>
        public XElementExportProvider(string path = null, IEnumerable<string> filters = null)
        {
            if (path == null)
            {
                path = Directory.GetCurrentDirectory();
            }
            List<string> include = new List<string>(new[] { "*.xml", "*.config" });
            if (filters != null)
            {
                foreach (string filter in filters.Where(filter => !string.IsNullOrWhiteSpace(filter)).Where(filter => !include.Contains(filter)))
                {
                    include.Add(filter);
                }
            }

            List<string> xmlFiles = new List<string>(include.SelectMany(ext => Directory.GetFiles(path, ext)));

            _exportsDictionary = xmlFiles.Select(filePath => new FileInfo(filePath)).ToDictionary(
                fileInfo => fileInfo.Name,
                fileInfo =>
                {
                    ExportDefinition def = new ExportDefinition(fileInfo.Name, null);
                    Export e = new Export(def, () => XElement.Load(fileInfo.FullName));
                    return e;
                });
        }
Beispiel #9
0
        protected bool IsMatch(ExportDefinition exportDefinition)
        {
            // If the feature is not enabled don't allow it
            if (exportDefinition.Metadata.ContainsKey("EnabledFeatures"))
            {
                if (!EnabledFeatures.IsEnabled((string)exportDefinition.Metadata["EnabledFeatures"]))
                {
                    return false;
                }
            }

            // If the feature is turned off in the options don't allow it
            if (exportDefinition.Metadata.ContainsKey("FeatureEnabled"))
            {
                if (!OptionApi.GetBool((string)exportDefinition.Metadata["FeatureEnabled"]))
                {
                    return false;
                }
            }

            if (exportDefinition.Metadata.ContainsKey("SupportedApplication"))
            {
                var applicationType = ((ApplicationType)exportDefinition.Metadata["SupportedApplication"]);
                return applicationType.HasFlag(_applicationType);
            }

            return false;
        }
        public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement origin)
        {
            Assumes.NotNull(exportDefinition);

            this._member = member;
            this._exportDefinition = exportDefinition;
            this._origin = origin;
        }
        public override object GetExportedObject(ExportDefinition definition)
        {
            Guard.Against(!definition.Equals(_export.Definition),
                          Resources.Error_PartDoesNotContainAnExportForContract,
                          definition.ContractName);

            return _export.GetExportedObject();
        }
 public void GetExportedObject_Guards_Against_Non_Matching_ExportDefinition()
 {
     object expected = new object();
     Export export = new Export("Foo", new Dictionary<string, object>(), () => expected);
     ExportDefinition nonMatching = new ExportDefinition("Bar", new Dictionary<string, object>());
     ExceptionAssert.Guards(() => new SingleExportComposablePart(export).GetExportedObject(nonMatching),
                            TargetResources.Error_PartDoesNotContainAnExportForContract, nonMatching.ContractName);
 }
 public override bool IsConstraintSatisfiedBy(ExportDefinition exportDefinition)
 {
     if (!base.IsConstraintSatisfiedBy(exportDefinition))
     {
         return false;
     }
     return PartCreatorExportDefinition.IsProductConstraintSatisfiedBy(this._productImportDefinition, exportDefinition);
 }
Beispiel #14
0
        public void Constructor2_NullAsMetadataArgument_ShouldSetMetadataPropertyToReadOnlyDictionary()
        {
            var definition = new ExportDefinition("Contract", (IDictionary<string, object>)null);

            ExceptionAssert.Throws<NotSupportedException>(() =>
            {
                definition.Metadata["Value"] = "Value";
            });
        }
 protected override bool IsMatch(ExportDefinition exportDefinition)
 {
     object bundle;
     if (exportDefinition.Metadata.TryGetValue("Bundle", out bundle))
     {
         var bundleName = bundle as string;
         if (bundleName != null)
             return Bundles.Contains(bundleName, StringComparer.OrdinalIgnoreCase);
     }
     return base.IsMatch(exportDefinition);
 }
Beispiel #16
0
        public void Constructor2_ValueAsContractNameArgument_ShouldSetContractNameProperty()
        {
            var expectations = Expectations.GetContractNames();

            foreach (var e in expectations)
            {
                var definition = new ExportDefinition(e, new Dictionary<string, object>());

                Assert.AreEqual(e, definition.ContractName);
            }
        }
Beispiel #17
0
        public override object GetExportedValue(ExportDefinition definition)
        {
            if (definition == null)
                throw new ArgumentNullException("definition");

            var rdef = (RubyExportDefinition)definition;

            var instance = GetOrCreateInstance();

            return rdef.GetExportedObject(instance);
        }
            public override object GetExportedValue(ExportDefinition definition)
            {
                Requires.NotNull(definition, "definition");

                if (definition != _export.Definition)
                {
                    throw ExceptionBuilder.CreateExportDefinitionNotOnThisComposablePart("definition");
                }

                return _export.Value;
            }
 public ApplicationSettingExportProvider(IDictionary<string, string> settings)
 {
     exports = new List<Export>();
     foreach (var key in settings.Keys)
     {
         var metadata = new Dictionary<string, object>();
         metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeof(string).FullName);
         var value = settings[key];
         var exportDefinition = new ExportDefinition(key, metadata);
         exports.Add(new Export(exportDefinition, () => value));
     }
 }
        public override object GetExportedValue(ExportDefinition exportDefinition)
        {
            exportDefinition.ShouldNotBeNull("exportDefinition");

            if (_values.ContainsKey(exportDefinition))
                return _values[exportDefinition];

            var value = InterceptedPart.GetExportedValue(exportDefinition);
            var interceptingValue = _valueInterceptor.Intercept(value);
            _values.Add(exportDefinition, interceptingValue);
            return interceptingValue;
        }
Beispiel #21
0
 internal static bool IsImportDependentOnPart(this ImportDefinition import, ComposablePartDefinition part, ExportDefinition export, bool expandGenerics)
 {
     import = import.GetProductImportDefinition();
     if (expandGenerics)
     {
         return part.GetExports(import).Any();
     }
     else
     {
         return TranslateImport(import, part).IsConstraintSatisfiedBy(export);
     }
 }
 /// <summary>
 /// Create an UnsuitableExportDefinitionInfo.
 /// </summary>
 /// <param name="target">Import for which the export is unsuitable.</param>
 /// <param name="exportDefinition">Unsuitable export.</param>
 /// <param name="partDefinition">Part definition that provided the export.</param>
 public UnsuitableExportDefinitionInfo(
     ContractBasedImportDefinition target,
     ExportDefinition exportDefinition,
     PartDefinitionInfo partDefinition)
 {
     ExportDefinition = exportDefinition;
     PartDefinition = partDefinition;
     _issues = CheckForRequiredMetadataIssues(target, exportDefinition)
                     .Concat(CheckForTypeIdentityIssues(target, exportDefinition))
                     .Concat(CheckForCreationPolicyIssues(target, partDefinition.PartDefinition))
                     .ToArray();
 }
        public override object GetExportedValue(ExportDefinition exportDefinition)
        {
            if (exportDefinition == null) throw new ArgumentNullException("exportDefinition");

            if (this.values.ContainsKey(exportDefinition))
                return this.values[exportDefinition];

            var value = InterceptedPart.GetExportedValue(exportDefinition);
            var interceptingValue = this.valueInterceptor.Intercept(value);
            this.values.Add(exportDefinition, interceptingValue);

            return interceptingValue;
        }
Beispiel #24
0
        public static CompositionError CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException)
        {
            Assumes.NotNull(part, definition, innerException);

            ICompositionElement element = definition.ToElement();
            return CompositionError.Create(
                CompositionErrorId.ImportEngine_PartCannotGetExportedValue,
                element,
                innerException,
                Strings.ImportEngine_PartCannotGetExportedValue,
                element.DisplayName,
                part.ToElement().DisplayName);
        }
        public static LazyMemberInfo GetExportingMember(ExportDefinition exportDefinition)
        {
            Requires.NotNull(exportDefinition, "exportDefinition");

            ReflectionMemberExportDefinition reflectionExportDefinition = exportDefinition as ReflectionMemberExportDefinition;
            if (reflectionExportDefinition == null)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidExportDefinition, exportDefinition.GetType()),
                    "exportDefinition");
            }

            return reflectionExportDefinition.ExportingLazyMember;
        }
        public void CreationPolicyConstraint_ValidMatchingCreationPolicy_ShouldMatch()
        {
            var contractName = "MyContract";
            var metadata = new Dictionary<string, object>();
            metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, CreationPolicy.Shared);

            var exportDefinition = new ExportDefinition(contractName, metadata);

            var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared);

            var predicate = constraint.Compile();

            Assert.IsTrue(predicate(exportDefinition));
        }
        public void TypeIdentityConstraint_InvalidExportDef_ShouldNotMatch()
        {
            var contractName = "MyContract";
            var typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(ConstraintServicesTests));
            var metadata = new Dictionary<string, object>();

            var exportDefinition = new ExportDefinition(contractName, metadata);

            var constraint = ConstraintServices.CreateConstraint(contractName, typeIdentity, null, CreationPolicy.Any);

            var predicate = constraint.Compile();

            Assert.IsFalse(predicate(exportDefinition));
        }
        protected ConfigurationPart(IEnumerable<FileInfo> configurationFileInfos, Type configurationType, object configurationInstance)
        {
            ConfigurationType = configurationType;
            ConfigurationInstance = configurationInstance;
            _configurationFileInfos = configurationFileInfos;
            var metadata = new Dictionary<string, object>()
                               {
                                   {"ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(ConfigurationType)}
                               };

            var contractName = AttributedModelServices.GetContractName(ConfigurationType);
            ExportDefinition = new ExportDefinition(contractName, metadata);

            PopulateFromFiles(configurationInstance);
        }
        internal static bool IsProductConstraintSatisfiedBy(ImportDefinition productImportDefinition, ExportDefinition exportDefinition)
        {
            object productValue = null;
            if (exportDefinition.Metadata.TryGetValue(CompositionConstants.ProductDefinitionMetadataName, out productValue))
            {
                ExportDefinition productDefinition = productValue as ExportDefinition;

                if (productDefinition != null)
                {
                    return productImportDefinition.IsConstraintSatisfiedBy(productDefinition);
                }
            }

            return false;
        }
        public override object GetExportedValue(ExportDefinition definition)
        {
            string requestKey = string.Format("__Request_{0}", Key);

            var obj = CurrentHttpContext.Items[requestKey];

            if (obj != null)
                return obj;

            obj = CreatePart();

            CurrentHttpContext.Items[requestKey] = obj;

            return obj;
        }
        public static IDictionary<string, object> WriteExportDefinition(ComposablePartDefinition owner, ExportDefinition exportDefinition)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(exportDefinition);

            LazyMemberInfo exportingMemberInfo = ReflectionModelServices.GetExportingMember(exportDefinition);

            IDictionary<string, object> cache = new Dictionary<string, object>();
            cache.WriteContractName(exportDefinition.ContractName);
            cache.WriteMetadata(exportDefinition.Metadata);
            cache.WriteValue(AttributedCacheServices.CacheKeys.MemberType, exportingMemberInfo.MemberType, MemberTypes.TypeInfo);
            cache.WriteLazyAccessors(exportingMemberInfo.GetAccessors(), ReflectionModelServices.GetPartType(owner));

            return cache;
        }
 public override bool IsConstraintSatisfiedBy(ExportDefinition exportDefinition)
 {
     return(default(bool));
 }
 public virtual new bool IsConstraintSatisfiedBy(ExportDefinition exportDefinition)
 {
     return(default(bool));
 }
 public Export(ExportDefinition definition, Func <Object> exportedValueGetter)
 {
 }
Beispiel #35
0
 /// <summary>
 ///     Gets the exported value described by the specified definition.
 /// </summary>
 /// <param name="definition">
 ///     One of the <see cref="ExportDefinition"/> objects from the
 ///     <see cref="ExportDefinitions"/> property describing the exported value
 ///     to return.
 /// </param>
 /// <returns>
 ///     The exported value described by <paramref name="definition"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="definition"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="ArgumentException">
 ///     <paramref name="definition"/> did not originate from the <see cref="ExportDefinitions"/>
 ///     property on the <see cref="ComposablePart"/>.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 ///     One or more pre-requisite imports, indicated by <see cref="ImportDefinition.IsPrerequisite"/>,
 ///     have not been set.
 /// </exception>
 /// <exception cref="ObjectDisposedException">
 ///     The <see cref="ComposablePart"/> has been disposed of.
 /// </exception>
 /// <exception cref="ComposablePartException">
 ///     An error occurred getting the exported value described by the <see cref="ExportDefinition"/>.
 /// </exception>
 public abstract object GetExportedValue(ExportDefinition definition);
 public static ICompositionElement ToElement(this ExportDefinition definition)
 {
     return(ToElementCore(definition));
 }