- implement init, create table for each type with table attribute

pull/26/head
Simon Gockner 6 years ago
parent c3a97a2514
commit 4a20023c56
  1. 28
      GBase/GBase.cs
  2. 4
      GBase/Interfaces/IGBase.cs

@ -2,10 +2,14 @@
// Created: 2020-02-12 // Created: 2020-02-12
// Copyright(c) 2020 SimonG. All Rights Reserved. // Copyright(c) 2020 SimonG. All Rights Reserved.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GBase.Attributes;
using GBase.Factories;
using GBase.Interfaces; using GBase.Interfaces;
using GBase.Interfaces.Settings; using GBase.Interfaces.Settings;
@ -21,12 +25,17 @@ namespace GBase
/// </summary> /// </summary>
public const string GBASE_TABLE_FILE_EXTENSION = "gb"; //TODO: Find correct place for this const public const string GBASE_TABLE_FILE_EXTENSION = "gb"; //TODO: Find correct place for this const
private readonly IGBaseTableFactory _gBaseTableFactory;
/// <summary> /// <summary>
/// The base class of the GBase database /// The base class of the GBase database
/// </summary> /// </summary>
/// <param name="settings">The <see cref="IGBaseSettings"/> for this <see cref="GBase"/></param> /// <param name="settings">The <see cref="IGBaseSettings"/> for this <see cref="GBase"/></param>
public GBase(IGBaseSettings settings) /// <param name="gBaseTableFactory">Factory for the <see cref="IGBaseTable"/></param>
public GBase(IGBaseSettings settings, IGBaseTableFactory gBaseTableFactory)
{ {
_gBaseTableFactory = gBaseTableFactory;
Settings = settings; Settings = settings;
Tables = new List<IGBaseTable>(); Tables = new List<IGBaseTable>();
} }
@ -51,11 +60,26 @@ namespace GBase
/// Initialize this <see cref="GBase"/> /// Initialize this <see cref="GBase"/>
/// </summary> /// </summary>
/// <param name="name">The name of this <see cref="GBase"/></param> /// <param name="name">The name of this <see cref="GBase"/></param>
/// <param name="databaseAssembly">The <see cref="Assembly"/> of the database</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
public async Task<bool> Init(string name, CancellationToken cancellationToken) public async Task<bool> Init(string name, Assembly databaseAssembly, CancellationToken cancellationToken)
{ {
Name = name; Name = name;
Type[] types = databaseAssembly.GetTypes();
foreach (var type in types)
{
GBaseTableAttribute gBaseTableAttribute = type.GetCustomAttribute<GBaseTableAttribute>();
if (gBaseTableAttribute == null)
continue;
IGBaseTable gBaseTable = _gBaseTableFactory.Create();
await gBaseTable.Init(type, type.Name, Settings.DatabasePath, cancellationToken);
AddTable(gBaseTable);
}
return true; return true;
} }

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using GBase.Interfaces.Settings; using GBase.Interfaces.Settings;
@ -35,9 +36,10 @@ namespace GBase.Interfaces
/// Initialize this <see cref="IGBase"/> /// Initialize this <see cref="IGBase"/>
/// </summary> /// </summary>
/// <param name="name">The name of this <see cref="IGBase"/></param> /// <param name="name">The name of this <see cref="IGBase"/></param>
/// <param name="databaseAssembly">The <see cref="Assembly"/> of the database</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
Task<bool> Init(string name, CancellationToken cancellationToken); Task<bool> Init(string name, Assembly databaseAssembly, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Add a given <see cref="IGBaseTable"/> to this <see cref="IGBase"/> /// Add a given <see cref="IGBaseTable"/> to this <see cref="IGBase"/>

Loading…
Cancel
Save