#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. 55
      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.Threading;
using System.Threading.Tasks;
using GBase.Api;
using GBase.Attributes;
using GBase.Factories;
using GBase.Interfaces;
@ -96,7 +95,7 @@ namespace GBase
return true;
}
public IGBaseTable<T> GetTable<T>() where T : INotifyGBaseEntryChanged
public IGBaseTable<T> GetTable<T>()
{
if (Tables.OfType<IGBaseTable<T>>().Any())
return Tables.OfType<IGBaseTable<T>>().First();
@ -118,7 +117,7 @@ namespace GBase
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>();
if (table == null)
@ -127,6 +126,34 @@ namespace GBase
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>
/// Dispose used resources asynchronously
/// </summary>

@ -13,6 +13,8 @@ using GBase.Attributes;
using GBase.Factories;
using GBase.FileHandling.Factories;
using GBase.Interfaces;
using GBase.Interfaces.DataHandling;
using GBase.Interfaces.DataHandling.Pool;
using GBase.Interfaces.FileHandling;
namespace GBase
@ -20,17 +22,22 @@ namespace GBase
/// <summary>
/// A <see cref="IGBase"/> table
/// </summary>
public class GBaseTable<T> : IGBaseTable<T> where T : INotifyGBaseEntryChanged
public class GBaseTable<T> : IGBaseTable<T>
{
private readonly IFileHandler _fileHandler;
private readonly IDataHandlerPool _dataHandlerPool;
private readonly IGBaseColumnFactory _gBaseColumnFactory;
/// <summary>
/// A <see cref="IGBase"/> table
/// </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();
_dataHandlerPool = dataHandlerPool;
_gBaseColumnFactory = gBaseColumnFactory;
Columns = new List<IGBaseColumn>();
@ -123,9 +130,10 @@ namespace GBase
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken)
{
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;
}
@ -140,38 +148,30 @@ namespace GBase
if (!Entries.Contains(entry))
return false;
entry.GBaseEntryChanged -= OnGBaseEntryChanged;
await _fileHandler.RemoveEntry(entry);
await _fileHandler.DeleteEntryFile(entry);
return Entries.Remove(entry);
}
/// <summary>
/// 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
public async Task SetValue<TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken)
{
//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
return true;
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry);
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None);
await dataHandler.SetValue<T, TProperty>(file.File, propertyName, value, cancellationToken);
}
/// <summary>
/// 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)
public async Task<TProperty> GetValue<TProperty>(T entry, string propertyName, CancellationToken cancellationToken)
{
//TODO: Might change, depending on #23
bool success = ModifyEntry(entry, args.ColumnName, args.Value);
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry);
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>
@ -181,6 +181,7 @@ namespace GBase
public async ValueTask DisposeAsync()
{
await _fileHandler.DisposeAsync();
await _dataHandlerPool.DisposeAsync();
foreach (var column in Columns)
{

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using GBase.Api;
using GBase.Interfaces.Settings;
namespace GBase.Interfaces
@ -49,7 +48,7 @@ namespace GBase.Interfaces
/// <returns>True if successful, false if not</returns>
bool AddTable(IGBaseTable table);
IGBaseTable<T> GetTable<T>() where T : INotifyGBaseEntryChanged;
IGBaseTable<T> GetTable<T>();
/// <summary>
/// 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>
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
{
public interface IGBaseTable<T> : IGBaseTable where T : INotifyGBaseEntryChanged
public interface IGBaseTable<T> : IGBaseTable
{
/// <summary>
/// The entries of this <see cref="IGBaseTable"/>
@ -34,6 +34,12 @@ namespace GBase.Interfaces
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns>
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>

Loading…
Cancel
Save