From 1cfa80c19994252fed151bf3f8ecaed621da8f86 Mon Sep 17 00:00:00 2001 From: Simon G Date: Mon, 9 Nov 2020 16:04:50 +0100 Subject: [PATCH] #25: adapt XmlDataHandler --- GBase/DataHandling/XmlDataHandler.cs | 25 +++++++++++++++---- GBase/DataHandling/XmlDataWriter.cs | 20 ++++++++++++--- GBase/Factories/IGBaseColumnFactory.cs | 2 +- GBase/FileHandling/FileHandler.cs | 4 +-- GBase/GBaseColumn.cs | 6 +++-- GBase/GBaseTable.cs | 2 +- GBase/Interfaces/DataHandling/IDataHandler.cs | 12 ++++++--- GBase/Interfaces/DataHandling/IDataWriter.cs | 13 ++++++++++ GBase/Interfaces/IGBaseColumn.cs | 2 +- .../XmlDataHandlerLocalIntegrationTest.cs | 4 +-- Test.GBase/DataHandling/XmlDataHandlerTest.cs | 18 ++++++------- 11 files changed, 79 insertions(+), 29 deletions(-) diff --git a/GBase/DataHandling/XmlDataHandler.cs b/GBase/DataHandling/XmlDataHandler.cs index c168908..83de8eb 100644 --- a/GBase/DataHandling/XmlDataHandler.cs +++ b/GBase/DataHandling/XmlDataHandler.cs @@ -7,6 +7,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using GBase.DataHandling.Cache.Factories; @@ -91,6 +92,17 @@ namespace GBase.DataHandling foreach (var column in table.Columns) { //TODO: Set value for each column + PropertyInfo property = entry.GetType().GetProperty(column.Name); + if (property == null) + continue; //TODO: What to do in this case? (Shouldn't really happen...) + + string valueString; + if (property.PropertyType != typeof(string) && property.GetValue(entry) is IEnumerable enumerable) + valueString = enumerable.ToGBaseString(); + else + valueString = property.GetValue(entry).ToString(); + + await _xmlDataWriter.Write(entryFile, property.Name, valueString, property.PropertyType, _overwrite, cancellationToken); } return true; @@ -106,10 +118,12 @@ namespace GBase.DataHandling /// /// The that implements the property /// The of the property + /// /// The name of the property /// The value to set + /// /// A to await - public async Task SetValue(string propertyName, TProperty value) + public async Task SetValue(FileStream entryFile, string propertyName, TProperty value, CancellationToken cancellationToken) { if (value == null) return; @@ -121,7 +135,7 @@ namespace GBase.DataHandling valueString = value.ToString(); await _cache.SetValue(propertyName, value, _overwrite); - await _xmlDataWriter.Write(TODO, propertyName, valueString, _overwrite, TODO); + await _xmlDataWriter.Write(entryFile, propertyName, valueString, _overwrite, cancellationToken); } /// @@ -129,10 +143,12 @@ namespace GBase.DataHandling /// /// The of the property /// The of the property + /// /// The name of the property /// The value to set + /// /// A to await - public async Task RemoveValue(string propertyName, TProperty value) + public async Task RemoveValue(FileStream entryFile, string propertyName, TProperty value, CancellationToken cancellationToken) { if (value == null) return; @@ -144,7 +160,7 @@ namespace GBase.DataHandling valueString = value.ToString(); await _cache.TryRemoveValue(propertyName, value); - await _xmlDataWriter.Remove(TODO, propertyName, valueString, TODO); + await _xmlDataWriter.Remove(entryFile, propertyName, valueString, cancellationToken); } /// @@ -183,7 +199,6 @@ namespace GBase.DataHandling public async ValueTask DisposeAsync() { await _xmlDataReader.DisposeAsync(); - await _xmlDataWriter.DisposeAsync(); } } } \ No newline at end of file diff --git a/GBase/DataHandling/XmlDataWriter.cs b/GBase/DataHandling/XmlDataWriter.cs index 5e3e54f..4dce15d 100644 --- a/GBase/DataHandling/XmlDataWriter.cs +++ b/GBase/DataHandling/XmlDataWriter.cs @@ -44,7 +44,7 @@ namespace GBase.DataHandling /// /// Write the data of a property /// - /// The that implements the property + /// The /// The of the property /// /// The name of the property @@ -52,9 +52,23 @@ namespace GBase.DataHandling /// If true an existing value is overwritten, if false an additional value is added /// /// A to await + public async Task Write(FileStream file, string propertyName, string value, bool overwrite, CancellationToken cancellationToken) => + await Write(file, propertyName, value, typeof(TProperty), overwrite, cancellationToken); + + /// + /// Write the data of a property + /// + /// The that implements the property + /// + /// The name of the property + /// The value of the property + /// + /// If true an existing value is overwritten, if false an additional value is added + /// + /// A to await /// /// No root element is set - public async Task Write(FileStream file, string propertyName, string value, bool overwrite, CancellationToken cancellationToken) + public async Task Write(FileStream file, string propertyName, string value, Type propertyType, bool overwrite, CancellationToken cancellationToken) { string typeName = typeof(T).FullName; if (typeName == null) @@ -87,7 +101,7 @@ namespace GBase.DataHandling else //property element doesn't exist { propertyElement = new XElement(propertyName, new XElement(XmlDataHandler.VALUE_ELEMENT_NAME) { Value = value }); //create the new property element with the value element - propertyElement.SetAttributeValue(XmlDataHandler.VALUE_TYPE_ATTRIBUTE_NAME, typeof(TProperty).FullName); //add the property type attribute + propertyElement.SetAttributeValue(XmlDataHandler.VALUE_TYPE_ATTRIBUTE_NAME, propertyType.FullName); //add the property type attribute rootElement.Add(propertyElement); //create new property element with the value element } diff --git a/GBase/Factories/IGBaseColumnFactory.cs b/GBase/Factories/IGBaseColumnFactory.cs index 46f5b4c..7bd39ed 100644 --- a/GBase/Factories/IGBaseColumnFactory.cs +++ b/GBase/Factories/IGBaseColumnFactory.cs @@ -16,6 +16,6 @@ namespace GBase.Factories /// Creates an /// /// A newly created instance of the implementation for - IGBaseColumn Create(); + IGBaseColumn Create(string name); } } \ No newline at end of file diff --git a/GBase/FileHandling/FileHandler.cs b/GBase/FileHandling/FileHandler.cs index e4915bc..1e997d2 100644 --- a/GBase/FileHandling/FileHandler.cs +++ b/GBase/FileHandling/FileHandler.cs @@ -81,7 +81,7 @@ namespace GBase.FileHandling public async Task SetValue(string propertyName, TProperty value, CancellationToken cancellationToken) { IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - await dataHandler.SetValue(propertyName, value); + await dataHandler.SetValue(TODO, propertyName, value, cancellationToken); } /// @@ -96,7 +96,7 @@ namespace GBase.FileHandling public async Task RemoveValue(string propertyName, TProperty value, CancellationToken cancellationToken) { IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); - await dataHandler.RemoveValue(propertyName, value); + await dataHandler.RemoveValue(TODO, propertyName, value, cancellationToken); } /// diff --git a/GBase/GBaseColumn.cs b/GBase/GBaseColumn.cs index ee3f45b..25d2da2 100644 --- a/GBase/GBaseColumn.cs +++ b/GBase/GBaseColumn.cs @@ -16,10 +16,12 @@ namespace GBase /// /// A column of a /// - public GBaseColumn() + public GBaseColumn(string name) { - + Name = name; } + + public string Name { get; } /// /// The method diff --git a/GBase/GBaseTable.cs b/GBase/GBaseTable.cs index 790c9b5..1bddadd 100644 --- a/GBase/GBaseTable.cs +++ b/GBase/GBaseTable.cs @@ -80,7 +80,7 @@ namespace GBase if (gBaseColumnAttribute == null) continue; - IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(); + IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(property.Name); AddColumn(gBaseColumn); } diff --git a/GBase/Interfaces/DataHandling/IDataHandler.cs b/GBase/Interfaces/DataHandling/IDataHandler.cs index f42cb82..7ff4d30 100644 --- a/GBase/Interfaces/DataHandling/IDataHandler.cs +++ b/GBase/Interfaces/DataHandling/IDataHandler.cs @@ -26,26 +26,32 @@ namespace GBase.Interfaces.DataHandling Task AddEntry(T entry, IGBaseTable table, FileStream entryFile, CancellationToken cancellationToken); Task RemoveEntry(T entry); - + /// /// Set the value for the given property /// /// The of the property /// The of the property + /// /// The name of the property /// The value to set + /// /// A to await - Task SetValue(string propertyName, TProperty value); + Task SetValue(FileStream entryFile, string propertyName, TProperty value, + CancellationToken cancellationToken); /// /// Remove the value for the given property /// /// The of the property /// The of the property + /// /// The name of the property /// The value to set + /// /// A to await - Task RemoveValue(string propertyName, TProperty value); + Task RemoveValue(FileStream entryFile, string propertyName, TProperty value, + CancellationToken cancellationToken); /// /// Get the value for the given property, if multiple values are set the first is returned diff --git a/GBase/Interfaces/DataHandling/IDataWriter.cs b/GBase/Interfaces/DataHandling/IDataWriter.cs index c7a67c2..98ffef4 100644 --- a/GBase/Interfaces/DataHandling/IDataWriter.cs +++ b/GBase/Interfaces/DataHandling/IDataWriter.cs @@ -35,6 +35,19 @@ namespace GBase.Interfaces.DataHandling /// /// A to await Task Write(FileStream file, string propertyName, string value, bool overwrite, CancellationToken cancellationToken); + + /// + /// Write the data of a property + /// + /// The + /// + /// The name of the property + /// The value of the property + /// + /// If true an existing value is overwritten, if false an additional value is added + /// + /// A to await + Task Write(FileStream file, string propertyName, string value, Type propertyType, bool overwrite, CancellationToken cancellationToken); /// /// Remove the value for the given property diff --git a/GBase/Interfaces/IGBaseColumn.cs b/GBase/Interfaces/IGBaseColumn.cs index d78a691..59cd728 100644 --- a/GBase/Interfaces/IGBaseColumn.cs +++ b/GBase/Interfaces/IGBaseColumn.cs @@ -11,6 +11,6 @@ namespace GBase.Interfaces /// public interface IGBaseColumn : IAsyncDisposable //TODO: Make column generic (generic type is type of the value of the column?)? { - + string Name { get; } } } \ No newline at end of file diff --git a/Test.GBase/DataHandling/XmlDataHandlerLocalIntegrationTest.cs b/Test.GBase/DataHandling/XmlDataHandlerLocalIntegrationTest.cs index afc6caf..0b56644 100644 --- a/Test.GBase/DataHandling/XmlDataHandlerLocalIntegrationTest.cs +++ b/Test.GBase/DataHandling/XmlDataHandlerLocalIntegrationTest.cs @@ -55,10 +55,10 @@ namespace Test.GBase.DataHandling // //await xmlDataHandler.SetValue(nameof(TestProperty), TestProperty); //await xmlDataHandler.SetValue(nameof(TestProperty), "empty"); - await xmlDataHandler.SetValue(nameof(TestProperty), "test"); + await xmlDataHandler.SetValue(TODO, nameof(TestProperty), "test", TODO); //await xmlDataHandler.SetValue(nameof(TestProperty), "OverwrittenString"); // - await xmlDataHandler.SetValue(nameof(TestInt), 1); + await xmlDataHandler.SetValue(TODO, nameof(TestInt), 1, TODO); //await xmlDataHandler.SetValue(nameof(TestInt), 2); //await xmlDataHandler.SetValue(nameof(TestInt), 3); TestProperty = await xmlDataHandler.GetValue(nameof(TestProperty)); diff --git a/Test.GBase/DataHandling/XmlDataHandlerTest.cs b/Test.GBase/DataHandling/XmlDataHandlerTest.cs index 548b2fe..b8d9384 100644 --- a/Test.GBase/DataHandling/XmlDataHandlerTest.cs +++ b/Test.GBase/DataHandling/XmlDataHandlerTest.cs @@ -137,10 +137,10 @@ namespace Test.GBase.DataHandling XmlDataHandler xmlDataHandler = new XmlDataHandler("path", xmlDataReaderFactoryMock.Object, xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); - await xmlDataHandler.SetValue("property", "SomeString"); + await xmlDataHandler.SetValue(TODO, "property", "SomeString", TODO); xmlDataHandlerCacheMock.Verify(c => c.SetValue("property", "SomeString", false), Times.Once); - xmlDataWriterMock.Verify(w => w.Write(TODO, "property", "SomeString", false, TODO), Times.Once); + xmlDataWriterMock.Verify(w => w.Write(TODO, "property", "SomeString", TODO, false, TODO), Times.Once); } [Test] @@ -162,10 +162,10 @@ namespace Test.GBase.DataHandling xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); List stringList = new List() {"string", "secondString", "thirdString"}; - await xmlDataHandler.SetValue>("property", stringList); + await xmlDataHandler.SetValue>(TODO, "property", stringList, TODO); xmlDataHandlerCacheMock.Verify(c => c.SetValue>("property", stringList, false), Times.Once); - xmlDataWriterMock.Verify(w => w.Write>(TODO, "property", $"{stringList[0]},{stringList[1]},{stringList[2]}", false, TODO), Times.Once); + xmlDataWriterMock.Verify(w => w.Write>(TODO, "property", $"{stringList[0]},{stringList[1]},{stringList[2]}", TODO, false, TODO), Times.Once); } [Test] @@ -186,10 +186,10 @@ namespace Test.GBase.DataHandling XmlDataHandler xmlDataHandler = new XmlDataHandler("path", xmlDataReaderFactoryMock.Object, xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); - await xmlDataHandler.SetValue("property", null); + await xmlDataHandler.SetValue(TODO, "property", null, TODO); xmlDataHandlerCacheMock.Verify(c => c.SetValue(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); - xmlDataWriterMock.Verify(w => w.Write(TODO, It.IsAny(), It.IsAny(), It.IsAny(), TODO), Times.Never); + xmlDataWriterMock.Verify(w => w.Write(TODO, It.IsAny(), It.IsAny(), TODO, It.IsAny(), TODO), Times.Never); } [Test] @@ -210,7 +210,7 @@ namespace Test.GBase.DataHandling XmlDataHandler xmlDataHandler = new XmlDataHandler("path", xmlDataReaderFactoryMock.Object, xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); - await xmlDataHandler.RemoveValue("property", "SomeString"); + await xmlDataHandler.RemoveValue(TODO, "property", "SomeString", TODO); xmlDataHandlerCacheMock.Verify(c => c.TryRemoveValue("property", "SomeString"), Times.Once); xmlDataWriterMock.Verify(w => w.Remove(TODO, "property", "SomeString", TODO), Times.Once); @@ -235,7 +235,7 @@ namespace Test.GBase.DataHandling xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); List stringList = new List() { "string", "secondString", "thirdString" }; - await xmlDataHandler.RemoveValue>("property", stringList); + await xmlDataHandler.RemoveValue>(TODO, "property", stringList, TODO); xmlDataHandlerCacheMock.Verify(c => c.TryRemoveValue>("property", stringList), Times.Once); xmlDataWriterMock.Verify(w => w.Remove>(TODO, "property", $"{stringList[0]},{stringList[1]},{stringList[2]}", TODO), Times.Once); @@ -259,7 +259,7 @@ namespace Test.GBase.DataHandling XmlDataHandler xmlDataHandler = new XmlDataHandler("path", xmlDataReaderFactoryMock.Object, xmlDataWriterFactoryMock.Object, xmlDataHandlerCacheFactoryMock.Object); - await xmlDataHandler.RemoveValue("property", null); + await xmlDataHandler.RemoveValue(TODO, "property", null, TODO); xmlDataHandlerCacheMock.Verify(c => c.TryRemoveValue(It.IsAny(), It.IsAny()), Times.Never); xmlDataWriterMock.Verify(w => w.Remove(TODO, It.IsAny(), It.IsAny(), TODO), Times.Never);