Index: Riskeer/Integration/src/Riskeer.Integration.IO/Factories/ExportableFailureMechanismSectionCollectionFactory.cs =================================================================== diff -u -r234c67f206c92dde881cc2c15e2cb4299be50741 -r08cc37ec1d27835d4ccefd628f47a72d5e8d1d1e --- Riskeer/Integration/src/Riskeer.Integration.IO/Factories/ExportableFailureMechanismSectionCollectionFactory.cs (.../ExportableFailureMechanismSectionCollectionFactory.cs) (revision 234c67f206c92dde881cc2c15e2cb4299be50741) +++ Riskeer/Integration/src/Riskeer.Integration.IO/Factories/ExportableFailureMechanismSectionCollectionFactory.cs (.../ExportableFailureMechanismSectionCollectionFactory.cs) (revision 08cc37ec1d27835d4ccefd628f47a72d5e8d1d1e) @@ -38,23 +38,34 @@ /// . /// /// The generator to generate ids for the exportable components. + /// The to keep track of the created items. /// The collection of to create the /// with. /// A . /// Thrown when any parameter is null. public static ExportableFailureMechanismSectionCollection CreateExportableFailureMechanismSectionCollection( - IdentifierGenerator idGenerator, IEnumerable sections) + IdentifierGenerator idGenerator, ExportableModelRegistry registry, IEnumerable sections) { if (idGenerator == null) { throw new ArgumentNullException(nameof(idGenerator)); } + if (registry == null) + { + throw new ArgumentNullException(nameof(registry)); + } + if (sections == null) { throw new ArgumentNullException(nameof(sections)); } + if (registry.Contains(sections)) + { + return registry.Get(sections); + } + var exportableSections = new List(); double startDistance = 0; foreach (FailureMechanismSection section in sections) @@ -66,8 +77,10 @@ startDistance = endDistance; } - return new ExportableFailureMechanismSectionCollection(idGenerator.GetNewId(Resources.ExportableFailureMechanismSectionCollection_IdPrefix), - exportableSections); + var exportableCollection = new ExportableFailureMechanismSectionCollection(idGenerator.GetNewId(Resources.ExportableFailureMechanismSectionCollection_IdPrefix), + exportableSections); + registry.Register(exportableCollection, sections); + return exportableCollection; } } } \ No newline at end of file Index: Riskeer/Integration/test/Riskeer.Integration.IO.Test/Factories/ExportableFailureMechanismSectionCollectionFactoryTest.cs =================================================================== diff -u -r234c67f206c92dde881cc2c15e2cb4299be50741 -r08cc37ec1d27835d4ccefd628f47a72d5e8d1d1e --- Riskeer/Integration/test/Riskeer.Integration.IO.Test/Factories/ExportableFailureMechanismSectionCollectionFactoryTest.cs (.../ExportableFailureMechanismSectionCollectionFactoryTest.cs) (revision 234c67f206c92dde881cc2c15e2cb4299be50741) +++ Riskeer/Integration/test/Riskeer.Integration.IO.Test/Factories/ExportableFailureMechanismSectionCollectionFactoryTest.cs (.../ExportableFailureMechanismSectionCollectionFactoryTest.cs) (revision 08cc37ec1d27835d4ccefd628f47a72d5e8d1d1e) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Linq; using Core.Common.Base.Geometry; using NUnit.Framework; @@ -39,7 +40,7 @@ { // Call void Call() => ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection( - null, Enumerable.Empty()); + null, new ExportableModelRegistry(), Enumerable.Empty()); // Assert var exception = Assert.Throws(Call); @@ -51,7 +52,7 @@ { // Call void Call() => ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection( - new IdentifierGenerator(), null); + new IdentifierGenerator(), new ExportableModelRegistry(), null); // Assert var exception = Assert.Throws(Call); @@ -82,9 +83,11 @@ }) }; + var registry = new ExportableModelRegistry(); + // Call ExportableFailureMechanismSectionCollection collection = - ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection(idGenerator, sections); + ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection(idGenerator, registry, sections); // Assert Assert.AreEqual("Vi.0", collection.Id); @@ -108,5 +111,28 @@ Assert.AreEqual(20, thirdExportableSection.StartDistance); Assert.AreEqual(40, thirdExportableSection.EndDistance); } + + [Test] + public void CreateExportableFailureMechanismSectionCollection_SectionsAlreadyRegistered_ReturnsRegisteredExportableMode() + { + // Setup + var sections = new List(); + + var registry = new ExportableModelRegistry(); + var idGenerator = new IdentifierGenerator(); + ExportableFailureMechanismSectionCollection exportableCollection1 = + ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection(idGenerator, registry, sections); + + + // Precondition + Assert.True(registry.Contains(sections)); + + // Call + ExportableFailureMechanismSectionCollection exportableCollection2 = + ExportableFailureMechanismSectionCollectionFactory.CreateExportableFailureMechanismSectionCollection(idGenerator, registry, sections); + + // Assert + Assert.AreSame(exportableCollection1, exportableCollection2); + } } } \ No newline at end of file