#25: GBase can now get and set values

pull/27/head
Simon G 5 years ago
parent 2d4b9c820d
commit 7ab02d7d3a
  1. 33
      GBase/GBase.cs
  2. 57
      GBase/GBaseTable.cs
  3. 9
      GBase/Interfaces/IGBase.cs
  4. 8
      GBase/Interfaces/IGBaseTable.cs

@ -8,7 +8,6 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GBase.Api;
using GBase.Attributes; using GBase.Attributes;
using GBase.Factories; using GBase.Factories;
using GBase.Interfaces; using GBase.Interfaces;
@ -96,7 +95,7 @@ namespace GBase
return true; return true;
} }
public IGBaseTable<T> GetTable<T>() where T : INotifyGBaseEntryChanged public IGBaseTable<T> GetTable<T>()
{ {
if (Tables.OfType<IGBaseTable<T>>().Any()) if (Tables.OfType<IGBaseTable<T>>().Any())
return Tables.OfType<IGBaseTable<T>>().First(); return Tables.OfType<IGBaseTable<T>>().First();
@ -118,7 +117,7 @@ namespace GBase
return Tables.Remove(table); return Tables.Remove(table);
} }
public async Task<bool> AddEntry<T>(T entry, CancellationToken cancellationToken) where T : INotifyGBaseEntryChanged public async Task<bool> AddEntry<T>(T entry, CancellationToken cancellationToken)
{ {
IGBaseTable<T> table = GetTable<T>(); IGBaseTable<T> table = GetTable<T>();
if (table == null) if (table == null)
@ -126,6 +125,34 @@ namespace GBase
return await table.AddEntry(entry, cancellationToken); return await table.AddEntry(entry, cancellationToken);
} }
public async Task SetValue<T, TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken)
{
IGBaseTable<T> table = GetTable<T>();
if (table == null)
throw new Exception(); //TODO: Create exception
await table.SetValue(entry, propertyName, value, cancellationToken);
}
public async Task<TProperty> GetValue<T, TProperty>(T entry, string propertyName, CancellationToken cancellationToken)
{
IGBaseTable<T> table = GetTable<T>();
if (table == null)
throw new Exception(); //TODO: Create exception
return await table.GetValue<TProperty>(entry, propertyName, cancellationToken);
}
public async Task<IEnumerable<TProperty>> GetValues<T, TProperty>(T entry, string propertyName, CancellationToken cancellationToken)
{
IGBaseTable<T> table = GetTable<T>();
if (table == null)
throw new Exception(); //TODO: Create exception
return await table.GetValues<TProperty>(entry, propertyName, cancellationToken);
}
/// <summary> /// <summary>
/// Dispose used resources asynchronously /// Dispose used resources asynchronously

@ -13,6 +13,8 @@ using GBase.Attributes;
using GBase.Factories; using GBase.Factories;
using GBase.FileHandling.Factories; using GBase.FileHandling.Factories;
using GBase.Interfaces; using GBase.Interfaces;
using GBase.Interfaces.DataHandling;
using GBase.Interfaces.DataHandling.Pool;
using GBase.Interfaces.FileHandling; using GBase.Interfaces.FileHandling;
namespace GBase namespace GBase
@ -20,17 +22,22 @@ namespace GBase
/// <summary> /// <summary>
/// A <see cref="IGBase"/> table /// A <see cref="IGBase"/> table
/// </summary> /// </summary>
public class GBaseTable<T> : IGBaseTable<T> where T : INotifyGBaseEntryChanged public class GBaseTable<T> : IGBaseTable<T>
{ {
private readonly IFileHandler _fileHandler; private readonly IFileHandler _fileHandler;
private readonly IDataHandlerPool _dataHandlerPool;
private readonly IGBaseColumnFactory _gBaseColumnFactory; private readonly IGBaseColumnFactory _gBaseColumnFactory;
/// <summary> /// <summary>
/// A <see cref="IGBase"/> table /// A <see cref="IGBase"/> table
/// </summary> /// </summary>
public GBaseTable(IFileHandlerFactory fileHandlerFactory, IGBaseColumnFactory gBaseColumnFactory) /// <param name="fileHandlerFactory"></param>
/// <param name="dataHandlerPool">The <see cref="IDataHandlerPool"/></param>
/// <param name="gBaseColumnFactory"></param>
public GBaseTable(IFileHandlerFactory fileHandlerFactory, IDataHandlerPool dataHandlerPool, IGBaseColumnFactory gBaseColumnFactory)
{ {
_fileHandler = fileHandlerFactory.Create(); _fileHandler = fileHandlerFactory.Create();
_dataHandlerPool = dataHandlerPool;
_gBaseColumnFactory = gBaseColumnFactory; _gBaseColumnFactory = gBaseColumnFactory;
Columns = new List<IGBaseColumn>(); Columns = new List<IGBaseColumn>();
@ -123,9 +130,10 @@ namespace GBase
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken) public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken)
{ {
Entries.Add(entry); Entries.Add(entry);
entry.GBaseEntryChanged += OnGBaseEntryChanged;
await _fileHandler.AddEntry(entry, this, cancellationToken); using IGBaseFile file = _fileHandler.CreateEntryFile(entry, this);
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken);
await dataHandler.AddEntry(entry, this, file.File, cancellationToken);
return true; return true;
} }
@ -140,38 +148,30 @@ namespace GBase
if (!Entries.Contains(entry)) if (!Entries.Contains(entry))
return false; return false;
entry.GBaseEntryChanged -= OnGBaseEntryChanged; await _fileHandler.DeleteEntryFile(entry);
await _fileHandler.RemoveEntry(entry);
return Entries.Remove(entry); return Entries.Remove(entry);
} }
/// <summary> public async Task SetValue<TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken)
/// Modify the property of a given entry with the given value
/// </summary>
/// <param name="entry">The entry</param>
/// <param name="propertyName">The name of the property</param>
/// <param name="value">The new value to set</param>
/// <returns>True if successful, false if not</returns>
private bool ModifyEntry(object entry, string propertyName, object value) //TODO: Write to file
{ {
//don't need to change value of property in `Entries` list, the instance is saved there, any property change is already changed there as well using IGBaseFile file = await _fileHandler.RequestEntryFile(entry);
return true; IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None);
await dataHandler.SetValue<T, TProperty>(file.File, propertyName, value, cancellationToken);
} }
/// <summary> public async Task<TProperty> GetValue<TProperty>(T entry, string propertyName, CancellationToken cancellationToken)
/// A <see cref="IGBase"/> entry changed
/// </summary>
/// <param name="entry">The entry (sender)</param>
/// <param name="args">The <see cref="GBaseEntryChangedEventArgs"/></param>
private void OnGBaseEntryChanged(object entry, GBaseEntryChangedEventArgs args)
{ {
//TODO: Might change, depending on #23 using IGBaseFile file = await _fileHandler.RequestEntryFile(entry);
bool success = ModifyEntry(entry, args.ColumnName, args.Value); IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None);
return await dataHandler.GetValue<T, TProperty>(file.File, propertyName, cancellationToken);
if (!success) }
throw new Exception("Failed to handle EntryChanged"); //TODO: Decide what to do here
public async Task<IEnumerable<TProperty>> GetValues<TProperty>(T entry, string propertyName, CancellationToken cancellationToken)
{
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry);
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None);
return await dataHandler.GetValues<T, TProperty>(file.File, propertyName, cancellationToken);
} }
/// <summary> /// <summary>
@ -181,6 +181,7 @@ namespace GBase
public async ValueTask DisposeAsync() public async ValueTask DisposeAsync()
{ {
await _fileHandler.DisposeAsync(); await _fileHandler.DisposeAsync();
await _dataHandlerPool.DisposeAsync();
foreach (var column in Columns) foreach (var column in Columns)
{ {

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GBase.Api;
using GBase.Interfaces.Settings; using GBase.Interfaces.Settings;
namespace GBase.Interfaces namespace GBase.Interfaces
@ -49,7 +48,7 @@ namespace GBase.Interfaces
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
bool AddTable(IGBaseTable table); bool AddTable(IGBaseTable table);
IGBaseTable<T> GetTable<T>() where T : INotifyGBaseEntryChanged; IGBaseTable<T> GetTable<T>();
/// <summary> /// <summary>
/// Removes a given <see cref="IGBaseTable"/> from this <see cref="IGBase"/> /// Removes a given <see cref="IGBaseTable"/> from this <see cref="IGBase"/>
@ -58,6 +57,10 @@ namespace GBase.Interfaces
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
bool RemoveTable(IGBaseTable table); bool RemoveTable(IGBaseTable table);
Task<bool> AddEntry<T>(T entry, CancellationToken cancellationToken) where T : INotifyGBaseEntryChanged; Task<bool> AddEntry<T>(T entry, CancellationToken cancellationToken);
Task SetValue<T, TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken);
Task<TProperty> GetValue<T, TProperty>(T entry, string propertyName, CancellationToken cancellationToken);
Task<IEnumerable<TProperty>> GetValues<T, TProperty>(T entry, string propertyName, CancellationToken cancellationToken);
} }
} }

@ -10,7 +10,7 @@ using GBase.Api;
namespace GBase.Interfaces namespace GBase.Interfaces
{ {
public interface IGBaseTable<T> : IGBaseTable where T : INotifyGBaseEntryChanged public interface IGBaseTable<T> : IGBaseTable
{ {
/// <summary> /// <summary>
/// The entries of this <see cref="IGBaseTable"/> /// The entries of this <see cref="IGBaseTable"/>
@ -34,6 +34,12 @@ namespace GBase.Interfaces
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param> /// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
Task<bool> RemoveEntry(T entry); Task<bool> RemoveEntry(T entry);
Task SetValue<TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken);
Task<TProperty> GetValue<TProperty>(T entry, string propertyName, CancellationToken cancellationToken);
Task<IEnumerable<TProperty>> GetValues<TProperty>(T entry, string propertyName, CancellationToken cancellationToken);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save