From 4a20023c561f78165c497bcbbfeba63d09d5c38c Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Mon, 17 Feb 2020 10:20:20 +0100 Subject: [PATCH] - implement init, create table for each type with table attribute --- GBase/GBase.cs | 28 ++++++++++++++++++++++++++-- GBase/Interfaces/IGBase.cs | 4 +++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/GBase/GBase.cs b/GBase/GBase.cs index e665d65..60a17d0 100644 --- a/GBase/GBase.cs +++ b/GBase/GBase.cs @@ -2,10 +2,14 @@ // Created: 2020-02-12 // Copyright(c) 2020 SimonG. All Rights Reserved. +using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading; using System.Threading.Tasks; +using GBase.Attributes; +using GBase.Factories; using GBase.Interfaces; using GBase.Interfaces.Settings; @@ -21,12 +25,17 @@ namespace GBase /// public const string GBASE_TABLE_FILE_EXTENSION = "gb"; //TODO: Find correct place for this const + private readonly IGBaseTableFactory _gBaseTableFactory; + /// /// The base class of the GBase database /// /// The for this - public GBase(IGBaseSettings settings) + /// Factory for the + public GBase(IGBaseSettings settings, IGBaseTableFactory gBaseTableFactory) { + _gBaseTableFactory = gBaseTableFactory; + Settings = settings; Tables = new List(); } @@ -51,11 +60,26 @@ namespace GBase /// Initialize this /// /// The name of this + /// The of the database /// A to cancel the asynchronous operation /// True if successful, false if not - public async Task Init(string name, CancellationToken cancellationToken) + public async Task Init(string name, Assembly databaseAssembly, CancellationToken cancellationToken) { Name = name; + + Type[] types = databaseAssembly.GetTypes(); + foreach (var type in types) + { + GBaseTableAttribute gBaseTableAttribute = type.GetCustomAttribute(); + if (gBaseTableAttribute == null) + continue; + + IGBaseTable gBaseTable = _gBaseTableFactory.Create(); + await gBaseTable.Init(type, type.Name, Settings.DatabasePath, cancellationToken); + + AddTable(gBaseTable); + } + return true; } diff --git a/GBase/Interfaces/IGBase.cs b/GBase/Interfaces/IGBase.cs index 4bc0578..614211a 100644 --- a/GBase/Interfaces/IGBase.cs +++ b/GBase/Interfaces/IGBase.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Threading; using System.Threading.Tasks; using GBase.Interfaces.Settings; @@ -35,9 +36,10 @@ namespace GBase.Interfaces /// Initialize this /// /// The name of this + /// The of the database /// A to cancel the asynchronous operation /// True if successful, false if not - Task Init(string name, CancellationToken cancellationToken); + Task Init(string name, Assembly databaseAssembly, CancellationToken cancellationToken); /// /// Add a given to this