private void DisconnectInstanceFromExport( PartCompositionId importingInstance, ImportRegistrationId importId, PartCompositionId exportingInstance) { var importingPartInfo = m_Parts[importingInstance]; if (importingPartInfo.Instance != null) { var importDefinition = importingPartInfo.Definition.Import(importId); if (importDefinition.IsPrerequisite) { ReleaseInstance(importingInstance); } else { var satisfiedPartImports = SatisfiedPartImports(importingInstance) .Where(p => !p.Item2.Equals(exportingInstance)); var partImports = ImportInformationForPart(satisfiedPartImports); var updatedInstances = Instances.UpdateIfRequired(importingPartInfo.Instance, partImports); foreach (var instance in updatedInstances) { if (instance.Change == InstanceChange.Removed) { var updatedInfo = m_Parts.FirstOrDefault(p => instance.Instance.Equals(p.Value.Instance)); updatedInfo.Value.Instance = null; } } } } }
/// <summary> /// Connects the export with the import. /// </summary> /// <param name="importRegistration">The ID of the import.</param> /// <param name="exportRegistration">The ID of the export.</param> public void Connect(ImportRegistrationId importRegistration, ExportRegistrationId exportRegistration) { { Lokad.Enforce.Argument(() => importRegistration); Lokad.Enforce.Argument(() => exportRegistration); } var import = m_Objects.SelectMany(t => t.Value).PartImportById(importRegistration); var export = m_Objects.SelectMany(t => t.Value).PartExportById(exportRegistration); if (!m_ImportEngine.Accepts(import, export)) { throw new CannotMapExportToImportException(); } if (!m_Connections.ContainsKey(importRegistration)) { m_Connections.Add(importRegistration, new List <ExportRegistrationId>()); } var list = m_Connections[importRegistration]; if ((import.Cardinality == ImportCardinality.ExactlyOne || import.Cardinality == ImportCardinality.ZeroOrOne) && (list.Count > 0)) { list[0] = exportRegistration; } else { list.Add(exportRegistration); } }
/// <summary> /// Initializes a new instance of the <see cref="PartImportToPartExportMap"/> class. /// </summary> /// <param name="import">The ID of the import.</param> /// <param name="exports">The collection containing the export IDs.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="import"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="exports"/> is <see langword="null" />. /// </exception> public PartImportToPartExportMap(ImportRegistrationId import, IEnumerable <ExportRegistrationId> exports) { { Lokad.Enforce.Argument(() => import); Lokad.Enforce.Argument(() => exports); } m_Import = import; m_Exports = exports; }
/// <summary> /// Initializes a new instance of the <see cref="PartImportToPartExportMap"/> class. /// </summary> /// <param name="import">The ID of the import.</param> /// <param name="exports">The collection containing the export IDs.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="import"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="exports"/> is <see langword="null" />. /// </exception> public PartImportToPartExportMap(ImportRegistrationId import, IEnumerable<ExportRegistrationId> exports) { { Lokad.Enforce.Argument(() => import); Lokad.Enforce.Argument(() => exports); } m_Import = import; m_Exports = exports; }
/// <summary> /// Provides the part definition that owns the given import registration. /// </summary> /// <param name="partDefinitions">The collection of parts that should be searched for the given import.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested part.</returns> /// <exception cref="UnknownImportDefinitionException">Thrown when the part does not define an import with the given ID.</exception> public static GroupPartDefinition PartByImport( this IEnumerable<GroupPartDefinition> partDefinitions, ImportRegistrationId importRegistration) { var part = partDefinitions.FirstOrDefault(o => o.RegisteredImports.Contains(importRegistration)); if (part == null) { throw new UnknownImportDefinitionException(); } return part; }
/// <summary> /// Provides the part definition that owns the given import registration. /// </summary> /// <param name="partDefinitions">The collection of parts that should be searched for the given import.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested part.</returns> /// <exception cref="UnknownImportDefinitionException">Thrown when the part does not define an import with the given ID.</exception> public static GroupPartDefinition PartByImport( this IEnumerable <GroupPartDefinition> partDefinitions, ImportRegistrationId importRegistration) { var part = partDefinitions.FirstOrDefault(o => o.RegisteredImports.Contains(importRegistration)); if (part == null) { throw new UnknownImportDefinitionException(); } return(part); }
/// <summary> /// Initializes a new instance of the <see cref="PartImportExportEdge{T}"/> class. /// </summary> /// <param name="importPart">The ID of the part that provides the import.</param> /// <param name="importId">The registration ID of the import.</param> /// <param name="exportPart">The ID of the part that provides the export.</param> /// <param name="exportId">The registration ID of the export.</param> public PartImportExportEdge( T importPart, ImportRegistrationId importId, T exportPart, ExportRegistrationId exportId) : base(exportPart, importPart) { { Debug.Assert(importId != null, "The import ID should not be a null reference."); Debug.Assert(exportId != null, "The export ID should not be a null reference."); } m_Import = importId; m_Export = exportId; }
/// <summary> /// Provides the import definition from the part definition that owns the import. /// </summary> /// <param name="partDefinitions">The collection of parts that should be searched for the import definition.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested import definition.</returns> /// <exception cref="UnknownImportDefinitionException">Thrown when none of the parts defines an import with the given ID.</exception> public static SerializableImportDefinition PartImportById( this IEnumerable <GroupPartDefinition> partDefinitions, ImportRegistrationId importRegistration) { var import = partDefinitions .Where(o => o.RegisteredImports.Contains(importRegistration)) .Select(o => o.Import(importRegistration)) .FirstOrDefault(); if (import == null) { throw new UnknownImportDefinitionException(); } return(import); }
/// <summary> /// Connects the export with the import. /// </summary> /// <param name="importRegistration">The ID of the import.</param> /// <param name="exportRegistration">The ID of the export.</param> public void Connect(ImportRegistrationId importRegistration, ExportRegistrationId exportRegistration) { { Lokad.Enforce.Argument(() => importRegistration); Lokad.Enforce.Argument(() => exportRegistration); } var import = m_Objects.SelectMany(t => t.Value).PartImportById(importRegistration); var export = m_Objects.SelectMany(t => t.Value).PartExportById(exportRegistration); if (!m_ImportEngine.Accepts(import, export)) { throw new CannotMapExportToImportException(); } if (!m_Connections.ContainsKey(importRegistration)) { m_Connections.Add(importRegistration, new List<ExportRegistrationId>()); } var list = m_Connections[importRegistration]; if ((import.Cardinality == ImportCardinality.ExactlyOne || import.Cardinality == ImportCardinality.ZeroOrOne) && (list.Count > 0)) { list[0] = exportRegistration; } else { list.Add(exportRegistration); } }
/// <summary> /// Returns the import definition that was registered with the given ID. /// </summary> /// <param name="id">The ID of the import.</param> /// <returns>The requested import definition.</returns> public SerializableImportDefinition Import(ImportRegistrationId id) { return(m_Imports[id]); }
/// <summary> /// Returns the import definition that was registered with the given ID. /// </summary> /// <param name="id">The ID of the import.</param> /// <returns>The requested import definition.</returns> public SerializableImportDefinition Import(ImportRegistrationId id) { return m_Imports[id]; }
/// <summary> /// Provides the import definition from the part definition that owns the import. /// </summary> /// <param name="partDefinitions">The collection of parts that should be searched for the import definition.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested import definition.</returns> /// <exception cref="UnknownImportDefinitionException">Thrown when none of the parts defines an import with the given ID.</exception> public static SerializableImportDefinition PartImportById( this IEnumerable<GroupPartDefinition> partDefinitions, ImportRegistrationId importRegistration) { var import = partDefinitions .Where(o => o.RegisteredImports.Contains(importRegistration)) .Select(o => o.Import(importRegistration)) .FirstOrDefault(); if (import == null) { throw new UnknownImportDefinitionException(); } return import; }
/// <summary> /// Provides the import definition from the part definition based on the import ID. /// </summary> /// <param name="partDefinition">The part definition that owns the import.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested import definition.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="partDefinition"/> is <see langword="null" />. /// </exception> /// <exception cref="UnknownImportDefinitionException">Thrown when the part does not define an import with the given ID.</exception> public static SerializableImportDefinition PartImportById(this GroupPartDefinition partDefinition, ImportRegistrationId importRegistration) { { Lokad.Enforce.Argument(() => partDefinition); } if (partDefinition.RegisteredImports.Contains(importRegistration)) { throw new UnknownImportDefinitionException(); } return partDefinition.Import(importRegistration); }
/// <summary> /// Provides the import definition from the part definition based on the import ID. /// </summary> /// <param name="partDefinition">The part definition that owns the import.</param> /// <param name="importRegistration">The ID of the import.</param> /// <returns>The requested import definition.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="partDefinition"/> is <see langword="null" />. /// </exception> /// <exception cref="UnknownImportDefinitionException">Thrown when the part does not define an import with the given ID.</exception> public static SerializableImportDefinition PartImportById(this GroupPartDefinition partDefinition, ImportRegistrationId importRegistration) { { Lokad.Enforce.Argument(() => partDefinition); } if (partDefinition.RegisteredImports.Contains(importRegistration)) { throw new UnknownImportDefinitionException(); } return(partDefinition.Import(importRegistration)); }