Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectEntityPersistor.cs =================================================================== diff -u -r97b905a386a23f1b200a86ea29bcd699c33bf537 -re6314b0eda3a1b258c4ce336ac1ddd3ada77970b --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectEntityPersistor.cs (.../ProjectEntityPersistor.cs) (revision 97b905a386a23f1b200a86ea29bcd699c33bf537) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ProjectEntityPersistor.cs (.../ProjectEntityPersistor.cs) (revision e6314b0eda3a1b258c4ce336ac1ddd3ada77970b) @@ -36,7 +36,7 @@ /// /// Persistor for . /// - public class ProjectEntityPersistor + public class ProjectEntityPersistor : IPersistor { private readonly IRingtoetsEntities dbContext; private readonly IDbSet dbSet; @@ -73,27 +73,8 @@ public Project GetEntityAsModel() { var entry = dbSet.SingleOrDefault(); - if (entry == null) - { - return null; - } - var project = converter.ConvertEntityToModel(entry); - var nrOfItems = entry.DikeAssessmentSectionEntities.Count; - var assessmentSections = new object[nrOfItems]; - - foreach (var sectionEntity in entry.DikeAssessmentSectionEntities) - { - assessmentSections[sectionEntity.Order] = dikeAssessmentSectionEntityPersistor.LoadModel(sectionEntity); - } - - // Add to items sorted - foreach (var assessmentSection in assessmentSections) - { - project.Items.Add(assessmentSection); - } - - return project; + return LoadModel(entry); } /// @@ -106,23 +87,7 @@ /// The parentNavigationProperty is read-only. public void InsertModel(Project project) { - if (project == null) - { - throw new ArgumentNullException("project", "Cannot update databaseSet when no project is set."); - } - - var entity = new ProjectEntity(); - dbSet.Add(entity); - insertedList.Add(entity, project); - - converter.ConvertModelToEntity(project, entity); - - if (project.StorageId > 0) - { - modifiedList.Add(entity); - } - - InsertChildren(project, entity); + InsertModel(dbSet.Local, project, 0); } /// @@ -139,14 +104,19 @@ /// The parentNavigationProperty is read-only. public void UpdateModel(Project model) { + UpdateModel(dbSet.Local, model, 0); + } + + public void UpdateModel(ICollection parentNavigationProperty, Project model, int order) + { if (model == null) { throw new ArgumentNullException("model", "Cannot update databaseSet when no project is set."); } ProjectEntity entity; try { - entity = dbSet.SingleOrDefault(db => db.ProjectEntityId == model.StorageId); + entity = parentNavigationProperty.SingleOrDefault(db => db.ProjectEntityId == model.StorageId); } catch (InvalidOperationException exception) { @@ -162,6 +132,27 @@ UpdateChildren(model, entity); } + public void InsertModel(ICollection parentNavigationProperty, Project project, int order) + { + if (project == null) + { + throw new ArgumentNullException("project", "Cannot update databaseSet when no project is set."); + } + + var entity = new ProjectEntity(); + parentNavigationProperty.Add(entity); + insertedList.Add(entity, project); + + converter.ConvertModelToEntity(project, entity); + + if (project.StorageId > 0) + { + modifiedList.Add(entity); + } + + InsertChildren(project, entity); + } + /// /// Removes all entities from that are not marked as 'updated'. /// @@ -196,12 +187,37 @@ dikeAssessmentSectionEntityPersistor.PerformPostSaveActions(); } + public Project LoadModel(ProjectEntity entity) + { + if (entity == null) + { + return null; + } + var project = converter.ConvertEntityToModel(entity); + + var nrOfItems = entity.DikeAssessmentSectionEntities.Count; + var assessmentSections = new object[nrOfItems]; + + foreach (var sectionEntity in entity.DikeAssessmentSectionEntities) + { + assessmentSections[sectionEntity.Order] = dikeAssessmentSectionEntityPersistor.LoadModel(sectionEntity); + } + + // Add to items sorted + foreach (var assessmentSection in assessmentSections) + { + project.Items.Add(assessmentSection); + } + + return project; + } + /// /// Updates the children of , in reference to , in the storage. /// /// The of which children need to be updated. /// Referenced . - private void UpdateChildren(Project project, ProjectEntity entity) + public void UpdateChildren(Project project, ProjectEntity entity) { var order = 0; foreach (var item in project.Items) @@ -220,7 +236,7 @@ /// /// The of which children need to be inserted. /// Referenced . - private void InsertChildren(Project project, ProjectEntity entity) + public void InsertChildren(Project project, ProjectEntity entity) { var order = 0; foreach (var item in project.Items) Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs =================================================================== diff -u -r4adf3910b91fba2fe6e7f7766836082046ab769a -re6314b0eda3a1b258c4ce336ac1ddd3ada77970b --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs (.../DbTestSet.cs) (revision 4adf3910b91fba2fe6e7f7766836082046ab769a) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/DbTestSet.cs (.../DbTestSet.cs) (revision e6314b0eda3a1b258c4ce336ac1ddd3ada77970b) @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Data.Entity; using System.Linq; using Rhino.Mocks; @@ -7,7 +8,7 @@ { public static class DbTestSet { - public static IDbSet GetDbTestSet(MockRepository mockRepository, IList data) where T : class + public static IDbSet GetDbTestSet(MockRepository mockRepository, ObservableCollection data) where T : class { var queryable = data.AsQueryable(); var dbSet = mockRepository.StrictMock>(); @@ -16,6 +17,7 @@ dbSet.Stub(m => m.Expression).Return(queryable.Expression); dbSet.Stub(m => m.ElementType).Return(queryable.ElementType); dbSet.Stub(m => m.GetEnumerator()).Return(queryable.GetEnumerator()); + dbSet.Stub(m => m.Local).Return(data); return dbSet; } } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionEntityPersistorTest.cs =================================================================== diff -u -r8dec462b18aff76313f2836309ef24ddd2f70b50 -re6314b0eda3a1b258c4ce336ac1ddd3ada77970b --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionEntityPersistorTest.cs (.../DikeAssessmentSectionEntityPersistorTest.cs) (revision 8dec462b18aff76313f2836309ef24ddd2f70b50) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikeAssessmentSectionEntityPersistorTest.cs (.../DikeAssessmentSectionEntityPersistorTest.cs) (revision e6314b0eda3a1b258c4ce336ac1ddd3ada77970b) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; @@ -580,7 +581,7 @@ Name = "Entity to delete" }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { entityToDelete }; @@ -635,7 +636,7 @@ Name = "First entity to delete" }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { entityToDelete, entityToUpdate @@ -688,7 +689,7 @@ DikeAssessmentSectionEntityId = 4567L, Name = "Second entity to delete" }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { firstEntityToDelete, secondEntityToDelete Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikesPipingFailureMechanismEntityPersistorTest.cs =================================================================== diff -u -r8dec462b18aff76313f2836309ef24ddd2f70b50 -re6314b0eda3a1b258c4ce336ac1ddd3ada77970b --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikesPipingFailureMechanismEntityPersistorTest.cs (.../DikesPipingFailureMechanismEntityPersistorTest.cs) (revision 8dec462b18aff76313f2836309ef24ddd2f70b50) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/DikesPipingFailureMechanismEntityPersistorTest.cs (.../DikesPipingFailureMechanismEntityPersistorTest.cs) (revision e6314b0eda3a1b258c4ce336ac1ddd3ada77970b) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; @@ -354,7 +355,7 @@ FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { entityToDelete }; @@ -397,7 +398,7 @@ FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { entityToDelete, entityToUpdate @@ -443,7 +444,7 @@ FailureMechanismEntityId = 4567L, FailureMechanismType = (int) FailureMechanismType.DikesPipingFailureMechanism }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { firstEntityToDelete, secondEntityToDelete Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectEntityPersistorTest.cs =================================================================== diff -u -r97b905a386a23f1b200a86ea29bcd699c33bf537 -re6314b0eda3a1b258c4ce336ac1ddd3ada77970b --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectEntityPersistorTest.cs (.../ProjectEntityPersistorTest.cs) (revision 97b905a386a23f1b200a86ea29bcd699c33bf537) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ProjectEntityPersistorTest.cs (.../ProjectEntityPersistorTest.cs) (revision e6314b0eda3a1b258c4ce336ac1ddd3ada77970b) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; @@ -35,7 +36,7 @@ public void Constructor_EmptyDataset_NewInstance() { // Setup - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); mockRepository.ReplayAll(); @@ -53,7 +54,7 @@ // Setup var ringtoetsEntities = mockRepository.StrictMock(); - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); mockRepository.ReplayAll(); ProjectEntityPersistor persistor = new ProjectEntityPersistor(ringtoetsEntities); @@ -73,7 +74,7 @@ // Setup const long storageId = 1234L; const string description = "description"; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { new ProjectEntity { @@ -104,7 +105,7 @@ const long storageId = 1234L; const string description = "description"; string defaultProjectName = new Project().Name; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { new ProjectEntity { @@ -133,7 +134,7 @@ public void GetEntityAsModel_MultipleEntitiesInDataSet_ThrowsInvalidOperationException() { // Setup - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { new ProjectEntity { @@ -164,7 +165,7 @@ // Setup const long storageId = 1234L; const string description = "description"; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { new ProjectEntity { @@ -203,7 +204,7 @@ { // Setup var dbSetMethodAddWasHit = 0; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); dbset.Stub(m => m.Add(new ProjectEntity())).IgnoreArguments().Return(new ProjectEntity()) @@ -227,20 +228,13 @@ // Setup const long storageId = 1234L; const string description = "description"; - ProjectEntity projectEntity = new ProjectEntity(); Project project = new Project { StorageId = storageId, Description = description }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); - dbset.Expect(m => m.Add(null)).IgnoreArguments().WhenCalled(x => - { - var insertedProjectEntity = x.Arguments.GetValue(0); - Assert.IsInstanceOf(insertedProjectEntity); - projectEntity = (ProjectEntity) insertedProjectEntity; - }).Return(projectEntity); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); @@ -252,6 +246,7 @@ persistor.InsertModel(project); // Assert + var projectEntity = dbset.Local.First(); Assert.AreNotEqual(project, projectEntity); Assert.AreEqual(storageId, projectEntity.ProjectEntityId); Assert.AreEqual(description, projectEntity.Description); @@ -265,7 +260,6 @@ // Setup const long storageId = 1234L; const string description = "description"; - ProjectEntity projectEntity = new ProjectEntity(); Project project = new Project { StorageId = storageId, @@ -276,13 +270,7 @@ } }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); - dbset.Expect(m => m.Add(null)).IgnoreArguments().WhenCalled(x => - { - var insertedProjectEntity = x.Arguments.GetValue(0); - Assert.IsInstanceOf(insertedProjectEntity); - projectEntity = (ProjectEntity) insertedProjectEntity; - }).Return(projectEntity); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); @@ -294,6 +282,7 @@ persistor.InsertModel(project); // Assert + var projectEntity = dbset.Local.First(); Assert.AreNotEqual(project, projectEntity); Assert.AreEqual(storageId, projectEntity.ProjectEntityId); Assert.AreEqual(description, projectEntity.Description); @@ -306,7 +295,7 @@ public void UpdateModel_NullData_ThrowsArgumentNullException() { // Setup - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); mockRepository.ReplayAll(); @@ -331,7 +320,7 @@ { StorageId = storageId }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { new ProjectEntity { @@ -364,7 +353,7 @@ { StorageId = storageId }; - var projectEntities = new List + var projectEntities = new ObservableCollection { new ProjectEntity { @@ -412,7 +401,7 @@ ProjectEntityId = storageId }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { entity }); @@ -453,7 +442,7 @@ } }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection { projectEntity }); @@ -480,7 +469,7 @@ { // Setup var ringtoetsEntities = mockRepository.StrictMock(); - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); mockRepository.ReplayAll(); ProjectEntityPersistor persistor = new ProjectEntityPersistor(ringtoetsEntities); @@ -499,20 +488,13 @@ { // Setup const long storageId = 1234L; - ProjectEntity insertedProjectEntity = new ProjectEntity(); Project project = new Project { StorageId = 0L }; - var dbset = DbTestSet.GetDbTestSet(mockRepository, new List()); - dbset.Expect(x => x.Add(null)).IgnoreArguments().Return(insertedProjectEntity).WhenCalled(x => - { - var insertedEntity = x.Arguments.GetValue(0); - Assert.IsInstanceOf(insertedEntity); - insertedProjectEntity = (ProjectEntity) insertedEntity; - }); + var dbset = DbTestSet.GetDbTestSet(mockRepository, new ObservableCollection()); var ringtoetsEntities = mockRepository.StrictMock(); ringtoetsEntities.Expect(x => x.ProjectEntities).Return(dbset); @@ -523,6 +505,7 @@ TestDelegate insertTest = () => persistor.InsertModel(project); Assert.DoesNotThrow(insertTest, "Precondition failed: InsertModel failed"); + var insertedProjectEntity = dbset.Local.First(); insertedProjectEntity.ProjectEntityId = storageId; Assert.AreEqual(0L, project.StorageId, "Precondition failed: Id should not have been set already"); @@ -548,7 +531,7 @@ Description = "Entity to delete" }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { entityToDelete, new ProjectEntity @@ -603,7 +586,7 @@ ProjectEntityId = 4567L, Description = "Second entity to delete" }; - IList parentNavigationProperty = new List + ObservableCollection parentNavigationProperty = new ObservableCollection { firstEntityToDelete, secondEntityToDelete,