diff --git a/GBase/GBase.cs b/GBase/GBase.cs index eba244f..7c0b03f 100644 --- a/GBase/GBase.cs +++ b/GBase/GBase.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; +using GBase.Api; using GBase.Attributes; using GBase.Exceptions; using GBase.Factories; @@ -96,7 +97,7 @@ namespace GBase return true; } - public IGBaseTable GetTable() + public IGBaseTable GetTable() where T : IGBaseObject, new() { if (Tables.OfType>().Any()) return Tables.OfType>().First(); @@ -119,7 +120,7 @@ namespace GBase return Tables.Remove(table); } - public async Task AddEntry(T entry, CancellationToken cancellationToken) + public async Task AddEntry(T entry, CancellationToken cancellationToken) where T : IGBaseObject, new() { IGBaseTable table = GetTable(); if (table == null) @@ -128,7 +129,7 @@ namespace GBase return await table.AddEntry(entry, cancellationToken); } - public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) + public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) where T : IGBaseObject, new() { IGBaseTable table = GetTable(); if (table == null) @@ -137,7 +138,7 @@ namespace GBase await table.SetValue(entry, propertyName, value, cancellationToken); } - public async Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) + public async Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) where T : IGBaseObject, new() { IGBaseTable table = GetTable(); if (table == null) @@ -146,7 +147,7 @@ namespace GBase return await table.GetValue(entry, propertyName, cancellationToken); } - public async Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) + public async Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) where T : IGBaseObject, new() { IGBaseTable table = GetTable(); if (table == null) diff --git a/GBase/GBaseObject.cs b/GBase/GBaseObject.cs index 2711bed..24a355d 100644 --- a/GBase/GBaseObject.cs +++ b/GBase/GBaseObject.cs @@ -14,7 +14,7 @@ namespace GBase /// /// GBase object that supplies inheriting classes with methods to get data from a /// - public abstract class GBaseObject : IGBaseObject + public abstract class GBaseObject : IGBaseObject where T : IGBaseObject, new() { private readonly IGBaseTable _gBaseTable; @@ -43,5 +43,7 @@ namespace GBase /// /// The given public abstract void InitializeFromString(string @string); + + public abstract void Initialize(List parameters); } } \ No newline at end of file diff --git a/GBase/GBaseTable.cs b/GBase/GBaseTable.cs index 644e0fe..23912cc 100644 --- a/GBase/GBaseTable.cs +++ b/GBase/GBaseTable.cs @@ -12,6 +12,7 @@ using GBase.Api; using GBase.Attributes; using GBase.Factories; using GBase.FileHandling.Factories; +using GBase.Helpers; using GBase.Interfaces; using GBase.Interfaces.DataHandling; using GBase.Interfaces.DataHandling.Pool; @@ -22,7 +23,7 @@ namespace GBase /// /// A table /// - public class GBaseTable : IGBaseTable + public class GBaseTable : IGBaseTable where T : IGBaseObject, new() { private readonly IFileHandler _fileHandler; private readonly IDataHandlerPool _dataHandlerPool; @@ -74,11 +75,10 @@ namespace GBase /// /// A to cancel the asynchronous operation /// True if successful, false if not - public async Task Init(Type type, string databasePath, string folderName, CancellationToken cancellationToken) + public async Task Init(Type type, string databasePath, string folderName, CancellationToken cancellationToken) //TODO: Remove bool return value? { Type = type; FolderName = folderName; - await _fileHandler.Init(databasePath, cancellationToken); //TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable foreach (var property in type.GetProperties()) @@ -87,9 +87,38 @@ namespace GBase if (gBaseColumnAttribute == null) continue; - IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(property.Name); + IGBaseColumn gBaseColumn = _gBaseColumnFactory.Create(property.Name, property.PropertyType); AddColumn(gBaseColumn); } + + List files = _fileHandler.Init(databasePath, FolderName, cancellationToken); + if (files == null) + return true; + + foreach (var file in files) //create entries for existing files + { + using (file.UseFile()) + { + T entry = new T(); + + List parameters = new List(); + foreach (var column in Columns) + { + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); + + parameters.Add(await GenericMethodCaller.CallAsync(dataHandler, + nameof(IDataHandler.GetValue), + BindingFlags.Public | BindingFlags.Instance, + typeof(T), column.Type, + file.File, column.Name, cancellationToken)); + } + + entry.Initialize(parameters); + + file.Entry = entry; + Entries.Add(entry); + } + } return true; } @@ -156,21 +185,21 @@ namespace GBase public async Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) { using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); await dataHandler.SetValue(file.File, propertyName, value, cancellationToken); } public async Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) { using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); return await dataHandler.GetValue(file.File, propertyName, cancellationToken); } public async Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) { using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); - IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); return await dataHandler.GetValues(file.File, propertyName, cancellationToken); } diff --git a/GBase/Interfaces/IGBase.cs b/GBase/Interfaces/IGBase.cs index 74b571c..b33b335 100644 --- a/GBase/Interfaces/IGBase.cs +++ b/GBase/Interfaces/IGBase.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks; +using GBase.Api; using GBase.Interfaces.Settings; namespace GBase.Interfaces @@ -48,7 +49,7 @@ namespace GBase.Interfaces /// True if successful, false if not bool AddTable(IGBaseTable table); - IGBaseTable GetTable(); + IGBaseTable GetTable() where T : IGBaseObject, new(); /// /// Removes a given from this @@ -57,10 +58,10 @@ namespace GBase.Interfaces /// True if successful, false if not bool RemoveTable(IGBaseTable table); - Task AddEntry(T entry, CancellationToken cancellationToken); + Task AddEntry(T entry, CancellationToken cancellationToken) where T : IGBaseObject, new(); - Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken); - Task GetValue(T entry, string propertyName, CancellationToken cancellationToken); - Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken); + Task SetValue(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) where T : IGBaseObject, new(); + Task GetValue(T entry, string propertyName, CancellationToken cancellationToken) where T : IGBaseObject, new(); + Task> GetValues(T entry, string propertyName, CancellationToken cancellationToken) where T : IGBaseObject, new(); } } \ No newline at end of file diff --git a/GBase/Interfaces/IGBaseTable.cs b/GBase/Interfaces/IGBaseTable.cs index 277c1c5..a43a036 100644 --- a/GBase/Interfaces/IGBaseTable.cs +++ b/GBase/Interfaces/IGBaseTable.cs @@ -10,7 +10,7 @@ using GBase.Api; namespace GBase.Interfaces { - public interface IGBaseTable : IGBaseTable + public interface IGBaseTable : IGBaseTable where T : IGBaseObject, new() { /// /// The entries of this