this is my favorite style, though always a pain

reflect derived types, then instantiate. No registration, just derive :)
This commit is contained in:
Adam R Grey 2023-03-22 12:21:38 -04:00
parent b64edf8f8a
commit 72d6e1e254
5 changed files with 50 additions and 15 deletions

3
.vscode/launch.json vendored
View File

@ -15,7 +15,8 @@
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
"stopAtEntry": false,
"requireExactSource": true
},
{
"name": ".NET Core Attach",

View File

@ -7,7 +7,7 @@ namespace newsletter
public List<Reporter> Reporters { get; set; }
public class Reporter
{
public string Type { get; set; }
public string ReporterClass { get; set; }
public string Url { get; set; }
public string Username { get; set; }
public string Password { get; set; }

View File

@ -63,8 +63,41 @@ namespace newsletter
static List<Task<Report>> CollectReporters(Configuration conf)
{
//TODO
throw new NotImplementedException();
var toReturn = new List<Task<Report>>();
var reporterTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(domainAssembly => domainAssembly.GetTypes())
.Where(type => type.IsSubclassOf(typeof(newsletter.Reporters.Reporter)) && !type.IsAbstract)
.ToList();
var skipPortionInName = "newsletter.Reporters.".Length;
foreach(var discoveredConfType in conf.Reporters.Select(r => r.ReporterClass))
{
Console.WriteLine($"{discoveredConfType} configuration type discovered");
}
foreach (var reporterType in reporterTypes)
{
Console.WriteLine($"{reporterType.ToString().Substring(skipPortionInName).ToLower()} reporter class discovered");
Func<Configuration.Reporter, bool> predicate = r => r.ReporterClass == reporterType.ToString().Substring(skipPortionInName).ToLower();
if (!conf.Reporters.Any(predicate))
{
Console.WriteLine($"no configurations for it");
}
else
{
var configurationsFor = conf.Reporters.Where(predicate);
Console.WriteLine($"{configurationsFor.Count()} configurations for it");
foreach (var confFor in configurationsFor)
{
var instance = (newsletter.Reporters.Reporter) Activator.CreateInstance(reporterType);
toReturn.Add(instance.Report(confFor));
}
}
}
return toReturn;
}
static async Task<List<Report>> CollectReports(List<Task<Report>> tasks)
@ -81,6 +114,7 @@ namespace newsletter
static HtmlDocument AssembleHTML(IEnumerable<HtmlNode> reportSections)
{
//TODO
Console.WriteLine($"assembling {reportSections.Count()} sections");
throw new NotImplementedException();
}

View File

@ -6,7 +6,7 @@ namespace newsletter.Reporters
public class Dummy : Reporter
{
#pragma warning disable CS1998
public async Task<Report> Report(Configuration.Reporter config)
public override async Task<Report> Report(Configuration.Reporter config)
{
return new Report()
{

View File

@ -2,8 +2,8 @@ using System.Threading.Tasks;
namespace newsletter.Reporters
{
public interface Reporter
public abstract class Reporter
{
Task<Report> Report(Configuration.Reporter config);
public abstract Task<Report> Report(Configuration.Reporter config);
}
}