|
@@ -4,6 +4,7 @@ using InABox.Core.Postable;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace InABox.Core
|
|
@@ -22,6 +23,24 @@ namespace InABox.Core
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+ internal static class PosterEngineUtils
|
|
|
+ {
|
|
|
+ private static Type[]? _posters;
|
|
|
+
|
|
|
+ public static Type GetPoster(Type TPoster)
|
|
|
+ {
|
|
|
+ _posters ??= CoreUtils.TypeList(
|
|
|
+ AppDomain.CurrentDomain.GetAssemblies(),
|
|
|
+ x => x.IsClass
|
|
|
+ && !x.IsAbstract
|
|
|
+ && !x.IsGenericType
|
|
|
+ && x.HasInterface(typeof(IPoster<,>))
|
|
|
+ ).ToArray();
|
|
|
+ return _posters.Where(x => TPoster.IsAssignableFrom(x)).FirstOrDefault()
|
|
|
+ ?? throw new Exception($"No poster of type {TPoster}.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// A base class for all <see cref="IPosterEngine{TPostable}"/>. A concrete instance of this will be loaded by
|
|
|
/// <see cref="PosterUtils.Process{T}(IDataModel{T})"/>; a new instance is guaranteed to be created each time that method is called.
|
|
@@ -31,26 +50,21 @@ namespace InABox.Core
|
|
|
/// <typeparam name="TSettings"></typeparam>
|
|
|
public abstract class PosterEngine<TPostable, TPoster, TSettings> : IPosterEngine<TPostable, TPoster, TSettings>
|
|
|
where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
|
|
|
- where TPoster : IPoster<TPostable, TSettings>
|
|
|
+ where TPoster : class, IPoster<TPostable, TSettings>
|
|
|
where TSettings : PosterSettings, new()
|
|
|
{
|
|
|
- protected static TPoster Poster = GetPoster();
|
|
|
-
|
|
|
- private static Type[]? _posters;
|
|
|
+ protected TPoster Poster;
|
|
|
|
|
|
- private static TPoster GetPoster()
|
|
|
+ public PosterEngine()
|
|
|
{
|
|
|
- _posters ??= CoreUtils.TypeList(
|
|
|
- AppDomain.CurrentDomain.GetAssemblies(),
|
|
|
- x => x.IsClass
|
|
|
- && !x.IsAbstract
|
|
|
- && !x.IsGenericType
|
|
|
- && x.HasInterface(typeof(IPoster<,>))
|
|
|
- ).ToArray();
|
|
|
+ Poster = CreatePoster();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static readonly Type? PosterType = PosterEngineUtils.GetPoster(typeof(TPoster));
|
|
|
|
|
|
- var type = _posters.Where(x => typeof(TPoster).IsAssignableFrom(x)).FirstOrDefault()
|
|
|
- ?? throw new Exception($"No poster of type {typeof(TPoster)}.");
|
|
|
- return (TPoster)Activator.CreateInstance(type);
|
|
|
+ protected virtual TPoster CreatePoster()
|
|
|
+ {
|
|
|
+ return (Activator.CreateInstance(PosterType!) as TPoster)!;
|
|
|
}
|
|
|
|
|
|
protected static TSettings GetSettings()
|
|
@@ -124,6 +138,7 @@ namespace InABox.Core
|
|
|
{
|
|
|
post.Posted = DateTime.Now;
|
|
|
post.PostedStatus = PostedStatus.Posted;
|
|
|
+ post.PostedNote = "";
|
|
|
}
|
|
|
new Client<TPostable>().Save(entities, "Posted by user.");
|
|
|
}
|
|
@@ -132,6 +147,7 @@ namespace InABox.Core
|
|
|
foreach (var post in entities)
|
|
|
{
|
|
|
post.PostedStatus = PostedStatus.PostFailed;
|
|
|
+ post.PostedNote = "Post failed.";
|
|
|
}
|
|
|
new Client<TPostable>().Save(entities, "Post failed by user.");
|
|
|
}
|
|
@@ -145,6 +161,7 @@ namespace InABox.Core
|
|
|
foreach (var post in entities)
|
|
|
{
|
|
|
post.PostedStatus = PostedStatus.PostFailed;
|
|
|
+ post.PostedNote = e.Message;
|
|
|
}
|
|
|
new Client<TPostable>().Save(entities, "Post failed by user.");
|
|
|
throw;
|