#25: implement workaround for missing open generic registration possibility

pull/27/head
Simon G 5 years ago
parent 781c6b3c03
commit a854354e85
  1. 27
      GBase/Exceptions/InvalidTableTypeException.cs
  2. 46
      GBase/Factories/GBaseTableFactory.cs
  3. 3
      GBase/Factories/IGBaseTableFactory.cs
  4. 2
      GBase/GBase.cs
  5. 3
      GBase/Installers/GBaseInstaller.cs

@ -0,0 +1,27 @@
// Author: Simon Gockner
// Created: 2020-09-18
// Copyright(c) 2020 SimonG. All Rights Reserved.
using System;
using GBase.Api;
namespace GBase.Exceptions
{
/// <summary>
/// <see cref="Exception"/> that the passed table type doesn't implement <see cref="INotifyGBaseEntryChanged"/>"/>
/// </summary>
public class InvalidTableTypeException : Exception
{
/// <summary>
/// <see cref="Exception"/> that the passed table type doesn't implement <see cref="INotifyGBaseEntryChanged"/>"/>
/// </summary>
/// <param name="type">The table type</param>
public InvalidTableTypeException(Type type)
: base($"Table type ({type}) doesn't implement {nameof(INotifyGBaseEntryChanged)}.")
{
Type = type;
}
public Type Type { get; }
}
}

@ -0,0 +1,46 @@
// Author: Simon Gockner
// Created: 2020-09-18
// Copyright(c) 2020 SimonG. All Rights Reserved.
using System;
using GBase.Api;
using GBase.Exceptions;
using GBase.FileHandling.Factories;
using GBase.Interfaces;
using GBase.Interfaces.FileHandling;
namespace GBase.Factories
{
/// <summary>
/// Factory for the <see cref="IGBaseTable"/>
/// </summary>
public class GBaseTableFactory : IGBaseTableFactory
{
private readonly IFileHandlerFactory _fileHandlerFactory;
private readonly IGBaseColumnFactory _gBaseColumnFactory;
/// <summary>
/// Factory for the <see cref="IGBaseTable"/>
/// </summary>
/// <param name="fileHandlerFactory">Factory for the <see cref="IFileHandler"/></param>
/// <param name="gBaseColumnFactory">Factory for the <see cref="IGBaseColumn"/></param>
public GBaseTableFactory(IFileHandlerFactory fileHandlerFactory, IGBaseColumnFactory gBaseColumnFactory)
{
_fileHandlerFactory = fileHandlerFactory;
_gBaseColumnFactory = gBaseColumnFactory;
}
/// <summary>
/// Creates an <see cref="IGBaseTable"/>
/// </summary>
/// <returns>A newly created instance of the implementation for <see cref="IGBaseTable"/></returns>
public IGBaseTable Create(Type type)
{
if (!typeof(INotifyGBaseEntryChanged).IsAssignableFrom(type))
throw new InvalidTableTypeException(type);
Type gBaseTableType = typeof(GBaseTable<>).MakeGenericType(type);
return (IGBaseTable) Activator.CreateInstance(gBaseTableType, _fileHandlerFactory, _gBaseColumnFactory);
}
}
}

@ -3,6 +3,7 @@
// Copyright(c) 2020 SimonG. All Rights Reserved.
using System;
using GBase.Interfaces;
namespace GBase.Factories
@ -16,6 +17,6 @@ namespace GBase.Factories
/// Creates an <see cref="IGBaseTable"/>
/// </summary>
/// <returns>A newly created instance of the implementation for <see cref="IGBaseTable"/></returns>
IGBaseTable Create();
IGBaseTable Create(Type type);
}
}

@ -75,7 +75,7 @@ namespace GBase
if (gBaseTableAttribute == null)
continue;
IGBaseTable gBaseTable = _gBaseTableFactory.Create();
IGBaseTable gBaseTable = _gBaseTableFactory.Create(type);
await gBaseTable.Init(type, type.Name, Settings.DatabasePath, cancellationToken);
AddTable(gBaseTable);

@ -19,12 +19,11 @@ namespace GBase.Installers
public void Install(IIocContainer container)
{
container.Register<IGBase, GBase>();
container.Register<IGBaseTable, GBaseTable>();
container.Register<IGBaseColumn, GBaseColumn>();
//factories
container.RegisterFactory<IGBaseFactory>();
container.RegisterFactory<IGBaseTableFactory>();
container.Register<IGBaseTableFactory, GBaseTableFactory>(); //TODO: Once LightweightIocContainer can handle open generic registrations this can be changed back again
container.RegisterFactory<IGBaseColumnFactory>();
}
}

Loading…
Cancel
Save