A database based on .net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.2 KiB

// Author: Gockner, Simon
// 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;
namespace GBase
{
/// <summary>
/// The base class of the GBase database
/// </summary>
public class GBase : IGBase
{
/// <summary>
/// The file extension for all GBase tables
/// </summary>
public const string GBASE_TABLE_FILE_EXTENSION = "gb"; //TODO: Find correct place for this const
private readonly IGBaseTableFactory _gBaseTableFactory;
/// <summary>
/// The base class of the GBase database
/// </summary>
/// <param name="settings">The <see cref="IGBaseSettings"/> for this <see cref="GBase"/></param>
/// <param name="gBaseTableFactory">Factory for the <see cref="IGBaseTable"/></param>
public GBase(IGBaseSettings settings, IGBaseTableFactory gBaseTableFactory)
{
_gBaseTableFactory = gBaseTableFactory;
Settings = settings;
Tables = new List<IGBaseTable>();
}
/// <summary>
/// The name of this <see cref="GBase"/>
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The settings of this <see cref="GBase"/>
/// </summary>
public IGBaseSettings Settings { get; }
/// <summary>
/// The <see cref="IGBaseTable"/>s of this <see cref="GBase"/>
/// </summary>
public List<IGBaseTable> Tables { get; }
/// <summary>
/// Initialize this <see cref="GBase"/>
/// </summary>
/// <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>
/// <returns>True if successful, false if not</returns>
public async Task<bool> Init(string name, Assembly databaseAssembly, CancellationToken cancellationToken)
{
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;
}
/// <summary>
/// Add a given <see cref="IGBaseTable"/> to this <see cref="GBase"/>
/// </summary>
/// <param name="table">The given <see cref="IGBaseTable"/></param>
/// <returns>True if successful, false if not</returns>
public bool AddTable(IGBaseTable table)
{
if (Tables.Contains(table))
return false;
if (Tables.Any(t => t.Name.Equals(table.Name)))
return false;
Tables.Add(table);
return true;
}
/// <summary>
/// Removes a given <see cref="IGBaseTable"/> from this <see cref="GBase"/>
/// </summary>
/// <param name="table">The given <see cref="IGBaseTable"/></param>
/// <returns>True if successful, false if not</returns>
public bool RemoveTable(IGBaseTable table)
{
if (!Tables.Contains(table))
return false;
return Tables.Remove(table);
}
/// <summary>
/// Dispose used resources asynchronously
/// </summary>
/// <returns>A <see cref="ValueTask"/> to await</returns>
public async ValueTask DisposeAsync()
{
foreach (var table in Tables)
{
await table.DisposeAsync();
}
Tables.Clear();
}
}
}