- try adding generic GBaseTable

pull/27/head
Simon G 5 years ago
parent 893fdf33d3
commit 7c2f52a265
  1. 19
      GBase/GBase.cs
  2. 12
      GBase/GBaseTable.cs
  3. 5
      GBase/Interfaces/IGBase.cs
  4. 44
      GBase/Interfaces/IGBaseTable.cs

@ -8,6 +8,7 @@ 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;
@ -100,6 +101,15 @@ namespace GBase
return true; return true;
} }
public IGBaseTable<T> GetTable<T>() where T : INotifyGBaseEntryChanged
{
if (Tables.OfType<IGBaseTable<T>>().Any())
return Tables.OfType<IGBaseTable<T>>().First();
//TODO: This probably doesn't work, because even though t.Type : T, IGBaseTable<t.Type> !: IGBaseTable<T>
return (IGBaseTable<T>) Tables.FirstOrDefault(t => typeof(T).IsAssignableFrom(t.Type)); //TestMe
}
/// <summary> /// <summary>
/// Removes a given <see cref="IGBaseTable"/> from this <see cref="GBase"/> /// Removes a given <see cref="IGBaseTable"/> from this <see cref="GBase"/>
/// </summary> /// </summary>
@ -113,6 +123,15 @@ namespace GBase
return Tables.Remove(table); return Tables.Remove(table);
} }
public bool AddEntry<T>(T entry) where T : INotifyGBaseEntryChanged
{
IGBaseTable<T> table = GetTable<T>();
if (table == null)
throw new Exception(); //TODO: Create exception
return table.AddEntry(entry);
}
/// <summary> /// <summary>
/// Dispose used resources asynchronously /// Dispose used resources asynchronously
/// </summary> /// </summary>

@ -21,7 +21,7 @@ namespace GBase
/// <summary> /// <summary>
/// A <see cref="IGBase"/> table /// A <see cref="IGBase"/> table
/// </summary> /// </summary>
public class GBaseTable : IGBaseTable public class GBaseTable<T> : IGBaseTable<T> where T : INotifyGBaseEntryChanged
{ {
private readonly IFileHandler _fileHandler; private readonly IFileHandler _fileHandler;
private readonly IGBaseColumnFactory _gBaseColumnFactory; private readonly IGBaseColumnFactory _gBaseColumnFactory;
@ -35,7 +35,7 @@ namespace GBase
_gBaseColumnFactory = gBaseColumnFactory; _gBaseColumnFactory = gBaseColumnFactory;
Columns = new List<IGBaseColumn>(); Columns = new List<IGBaseColumn>();
Entries = new List<object>(); Entries = new List<T>();
} }
@ -57,7 +57,7 @@ namespace GBase
/// <summary> /// <summary>
/// The entries of this <see cref="IGBaseTable"/> /// The entries of this <see cref="IGBaseTable"/>
/// </summary> /// </summary>
public List<object> Entries { get; } public List<T> Entries { get; }
/// <summary> /// <summary>
@ -123,7 +123,7 @@ namespace GBase
/// </summary> /// </summary>
/// <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>
public bool AddEntry(INotifyGBaseEntryChanged entry) //TODO: Write to file public bool AddEntry(T entry) //TODO: Write to file
{ {
Entries.Add(entry); Entries.Add(entry);
entry.GBaseEntryChanged += OnGBaseEntryChanged; entry.GBaseEntryChanged += OnGBaseEntryChanged;
@ -136,7 +136,7 @@ namespace GBase
/// </summary> /// </summary>
/// <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>
public bool RemoveEntry(INotifyGBaseEntryChanged entry) //TODO: remove from file public bool RemoveEntry(T entry) //TODO: remove from file
{ {
if (!Entries.Contains(entry)) if (!Entries.Contains(entry))
return false; return false;
@ -188,7 +188,7 @@ namespace GBase
Columns.Clear(); Columns.Clear();
foreach (var entry in Entries.OfType<INotifyGBaseEntryChanged>().ToList()) foreach (var entry in Entries.ToList())
{ {
RemoveEntry(entry); RemoveEntry(entry);
} }

@ -7,6 +7,7 @@ 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
@ -48,11 +49,15 @@ 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;
/// <summary> /// <summary>
/// Removes a given <see cref="IGBaseTable"/> from this <see cref="IGBase"/> /// Removes a given <see cref="IGBaseTable"/> from this <see cref="IGBase"/>
/// </summary> /// </summary>
/// <param name="table">The given <see cref="IGBaseTable"/></param> /// <param name="table">The given <see cref="IGBaseTable"/></param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
bool RemoveTable(IGBaseTable table); bool RemoveTable(IGBaseTable table);
bool AddEntry<T>(T entry) where T : INotifyGBaseEntryChanged;
} }
} }

@ -10,6 +10,31 @@ using GBase.Api;
namespace GBase.Interfaces namespace GBase.Interfaces
{ {
public interface IGBaseTable<T> : IGBaseTable where T : INotifyGBaseEntryChanged
{
/// <summary>
/// The entries of this <see cref="IGBaseTable"/>
/// </summary>
List<T> Entries { get; }
/// <summary>
/// Add an entry that implements <see cref="INotifyGBaseEntryChanged"/> to this <see cref="IGBaseTable"/>
/// </summary>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns>
bool AddEntry(T entry);
//T GetEntry(T entry); //TODO: This doesn't make sense... (passing instance of T to get the same instance back...)
/// <summary>
/// Remove an entry that implements <see cref="INotifyGBaseEntryChanged"/> from this <see cref="IGBaseTable"/>
/// </summary>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns>
bool RemoveEntry(T entry);
}
/// <summary> /// <summary>
/// A <see cref="IGBase"/> table /// A <see cref="IGBase"/> table
/// </summary> /// </summary>
@ -30,11 +55,6 @@ namespace GBase.Interfaces
/// </summary> /// </summary>
List<IGBaseColumn> Columns { get; } List<IGBaseColumn> Columns { get; }
/// <summary>
/// The entries of this <see cref="IGBaseTable"/>
/// </summary>
List<object> Entries { get; }
/// <summary> /// <summary>
/// Initialize this <see cref="IGBase"/> /// Initialize this <see cref="IGBase"/>
/// </summary> /// </summary>
@ -58,19 +78,5 @@ namespace GBase.Interfaces
/// <param name="column">The given <see cref="IGBaseColumn"/></param> /// <param name="column">The given <see cref="IGBaseColumn"/></param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
bool RemoveColumn(IGBaseColumn column); bool RemoveColumn(IGBaseColumn column);
/// <summary>
/// Add an entry that implements <see cref="INotifyGBaseEntryChanged"/> to this <see cref="IGBaseTable"/>
/// </summary>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns>
bool AddEntry(INotifyGBaseEntryChanged entry);
/// <summary>
/// Remove an entry that implements <see cref="INotifyGBaseEntryChanged"/> from this <see cref="IGBaseTable"/>
/// </summary>
/// <param name="entry">The entry implementing <see cref="INotifyGBaseEntryChanged"/></param>
/// <returns>True if successful, false if not</returns>
bool RemoveEntry(INotifyGBaseEntryChanged entry);
} }
} }
Loading…
Cancel
Save