Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/StochasticSoilProfilePersistor.cs =================================================================== diff -u -r6d514ec60f68620d78015ac58ba6a966ef6b14e3 -r16846de8f0e08ace5456684f2f342e941e7c6386 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/StochasticSoilProfilePersistor.cs (.../StochasticSoilProfilePersistor.cs) (revision 6d514ec60f68620d78015ac58ba6a966ef6b14e3) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/StochasticSoilProfilePersistor.cs (.../StochasticSoilProfilePersistor.cs) (revision 16846de8f0e08ace5456684f2f342e941e7c6386) @@ -27,7 +27,9 @@ using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; using Application.Ringtoets.Storage.Properties; +using Core.Common.Utils; using Ringtoets.Piping.Data; +using Ringtoets.Piping.Primitives; namespace Application.Ringtoets.Storage.Persistors { @@ -39,6 +41,9 @@ private readonly ICollection modifiedList = new List(); private readonly StochasticSoilProfileConverter stochasticSoilProfileConverter = new StochasticSoilProfileConverter(); + private readonly Dictionary loadedProfiles; + private readonly Dictionary savedProfiles; + /// /// New instance of . /// @@ -51,6 +56,8 @@ throw new ArgumentNullException("ringtoetsContext"); } stochasticSoilProfileSet = ringtoetsContext.StochasticSoilProfileEntities; + loadedProfiles = new Dictionary(new ReferenceEqualityComparer()); + savedProfiles = new Dictionary(new ReferenceEqualityComparer()); } /// @@ -64,7 +71,17 @@ { throw new ArgumentNullException("entity"); } - return stochasticSoilProfileConverter.ConvertEntityToModel(entity); + + var model = stochasticSoilProfileConverter.ConvertEntityToModel(entity); + if (loadedProfiles.ContainsKey(entity.SoilProfileEntity)) + { + model.SoilProfile = loadedProfiles[entity.SoilProfileEntity]; + } + else + { + loadedProfiles[entity.SoilProfileEntity] = model.SoilProfile; + } + return model; } /// @@ -149,8 +166,17 @@ private void InsertStochasticSoilProfile(ICollection parentNavigationProperty, StochasticSoilProfile stochasticSoilProfile) { - var entity = new StochasticSoilProfileEntity(); + StochasticSoilProfileEntity entity = new StochasticSoilProfileEntity(); stochasticSoilProfileConverter.ConvertModelToEntity(stochasticSoilProfile, entity); + + if (savedProfiles.ContainsKey(stochasticSoilProfile.SoilProfile)) + { + entity.SoilProfileEntity = savedProfiles[stochasticSoilProfile.SoilProfile]; + } + else + { + savedProfiles[stochasticSoilProfile.SoilProfile] = entity.SoilProfileEntity; + } parentNavigationProperty.Add(entity); insertedList.Add(entity, stochasticSoilProfile); } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs =================================================================== diff -u -r6d514ec60f68620d78015ac58ba6a966ef6b14e3 -r16846de8f0e08ace5456684f2f342e941e7c6386 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision 6d514ec60f68620d78015ac58ba6a966ef6b14e3) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision 16846de8f0e08ace5456684f2f342e941e7c6386) @@ -24,15 +24,13 @@ using System.IO; using System.Linq; using Application.Ringtoets.Storage.TestUtil; -using Core.Common.Base; using Core.Common.Base.Data; using Core.Common.Base.Plugin; using Core.Common.Gui; using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.Settings; using Core.Common.TestUtil; using NUnit.Framework; -using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Integration.Data; using Ringtoets.Piping.Data; Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/StochasticSoilProfilePersistorTest.cs =================================================================== diff -u -r6d514ec60f68620d78015ac58ba6a966ef6b14e3 -r16846de8f0e08ace5456684f2f342e941e7c6386 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/StochasticSoilProfilePersistorTest.cs (.../StochasticSoilProfilePersistorTest.cs) (revision 6d514ec60f68620d78015ac58ba6a966ef6b14e3) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/StochasticSoilProfilePersistorTest.cs (.../StochasticSoilProfilePersistorTest.cs) (revision 16846de8f0e08ace5456684f2f342e941e7c6386) @@ -122,6 +122,46 @@ } [Test] + public void LoadModel_DifferentStochasticProfileEntitiesWithSameSoilProfileEntity_ReturnStochasticProfileWithSameSoilProfile() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new StochasticSoilProfilePersistor(ringtoetsEntitiesMock); + + + var soilProfileEntity = new SoilProfileEntity + { + SoilLayerEntities = new List + { + new SoilLayerEntity() + } + }; + + var firstStochasticProfileEntity = new StochasticSoilProfileEntity + { + Probability = Convert.ToDecimal(1.0), + SoilProfileEntity = soilProfileEntity + }; + var secondStochasticProfileEntity = new StochasticSoilProfileEntity + { + Probability = Convert.ToDecimal(1.0), + SoilProfileEntity = soilProfileEntity + }; + + StochasticSoilProfile firstModel = persistor.LoadModel(firstStochasticProfileEntity); + + // Call + StochasticSoilProfile secondModel = persistor.LoadModel(secondStochasticProfileEntity); + + // Assert + Assert.AreSame(firstModel.SoilProfile, secondModel.SoilProfile); + + mockRepository.VerifyAll(); + } + + [Test] public void InsertModel_NullParentNavigationProperty_ThrowsArgumentNullException() { // Setup @@ -199,6 +239,48 @@ } [Test] + public void InsertModel_DifferentStochasticProfileWithSameSoilProfile_ReturnStochasticProfileEntityWithSameSoilProfileEntity() + { + // Setup + var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); + mockRepository.ReplayAll(); + + var persistor = new StochasticSoilProfilePersistor(ringtoetsEntitiesMock); + + const long storageId = 1234L; + + IList parentNavigationProperty = new List(); + + var pipingSoilProfile = new TestPipingSoilProfile(); + StochasticSoilProfile firstSoilProfile = new StochasticSoilProfile(0.1, SoilProfileType.SoilProfile1D, -1) + { + StorageId = storageId, + SoilProfile = pipingSoilProfile + }; + + StochasticSoilProfile secondSoilProfile = new StochasticSoilProfile(0.1, SoilProfileType.SoilProfile1D, -1) + { + StorageId = storageId, + SoilProfile = pipingSoilProfile + }; + + persistor.InsertModel(parentNavigationProperty, firstSoilProfile); + + // Call + persistor.InsertModel(parentNavigationProperty, secondSoilProfile); + + // Assert + Assert.AreEqual(2, parentNavigationProperty.Count); + var parentNavigationPropertyList = parentNavigationProperty.ToArray(); + var firstStochasticProfileEntity = parentNavigationPropertyList[0]; + var secondStochasticProfileEntity = parentNavigationPropertyList[1]; + + Assert.AreSame(firstStochasticProfileEntity.SoilProfileEntity, secondStochasticProfileEntity.SoilProfileEntity); + + mockRepository.VerifyAll(); + } + + [Test] public void UpdateModel_NullDatasetValidModel_ThrowsArgumentNullException() { // Setup @@ -414,8 +496,6 @@ public void UpdateModel_SingleEntityInParentNavigationPropertySingleStochasticSoilProfileWithoutStorageId_DbSetCleared() { // Setup - const long storageId = 0L; // Newly inserted entities have Id = 0 untill they are saved - var ringtoetsEntitiesMock = RingtoetsEntitiesHelper.Create(mockRepository); mockRepository.ReplayAll();