From ef566a351d4e35f4140771b7324bdb4688ad7f8a Mon Sep 17 00:00:00 2001 From: kesac Date: Mon, 25 Oct 2021 21:16:23 -0600 Subject: [PATCH] Initialize repo with working class hierarchy --- .gitignore | 10 +++ .../Archigen.Example/Archigen.Example.csproj | 12 ++++ Archigen/Archigen.Example/Program.cs | 61 +++++++++++++++++++ Archigen/Archigen.sln | 31 ++++++++++ Archigen/Archigen/Archigen.csproj | 19 ++++++ Archigen/Archigen/Generator.cs | 46 ++++++++++++++ Archigen/Archigen/GeneratorInfo.cs | 20 ++++++ Archigen/Archigen/IGenerator.cs | 16 +++++ 8 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 Archigen/Archigen.Example/Archigen.Example.csproj create mode 100644 Archigen/Archigen.Example/Program.cs create mode 100644 Archigen/Archigen.sln create mode 100644 Archigen/Archigen/Archigen.csproj create mode 100644 Archigen/Archigen/Generator.cs create mode 100644 Archigen/Archigen/GeneratorInfo.cs create mode 100644 Archigen/Archigen/IGenerator.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c33912 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +################################################################################ +# This .gitignore file was automatically created by Microsoft(R) Visual Studio. +################################################################################ + +/.vs +/Archigen/.vs/Archigen/v16 +/Archigen/Archigen/bin +/Archigen/Archigen/obj +/Archigen/Archigen.Example/bin +/Archigen/Archigen.Example/obj diff --git a/Archigen/Archigen.Example/Archigen.Example.csproj b/Archigen/Archigen.Example/Archigen.Example.csproj new file mode 100644 index 0000000..8945540 --- /dev/null +++ b/Archigen/Archigen.Example/Archigen.Example.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/Archigen/Archigen.Example/Program.cs b/Archigen/Archigen.Example/Program.cs new file mode 100644 index 0000000..634d07f --- /dev/null +++ b/Archigen/Archigen.Example/Program.cs @@ -0,0 +1,61 @@ +using System; + +namespace Archigen.Example +{ + + public class StringGenerator : IGenerator + { + public string Next() + { + return "This is a string"; + } + } + + public class NumberGenerator : IGenerator + { + public int Next() + { + return 42; + } + } + + public class UserGenerator : IGenerator + { + public User Next() + { + return new User() { FirstName = "John", LastName = "Smith", Age = 9001 }; + } + } + + public class User + { + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } + public User Partner { get; set; } + + public override string ToString() + { + return FirstName + " " + LastName + " " + Age + " " + Partner?.ToString(); + } + + } + + public class Program + { + public static void Main(string[] args) + { + var g = new Generator() + .ForProperty(x => x.FirstName, new StringGenerator()) + .ForProperty(x => x.LastName, new StringGenerator()) + .ForProperty(x => x.Age, new NumberGenerator()) + .ForProperty(x => x.Partner, new UserGenerator()); + + for(int i = 0; i < 10; i++) + { + Console.WriteLine(g.Next()); + } + + } + } +} diff --git a/Archigen/Archigen.sln b/Archigen/Archigen.sln new file mode 100644 index 0000000..1eeba43 --- /dev/null +++ b/Archigen/Archigen.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31229.75 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Archigen", "Archigen\Archigen.csproj", "{C04E0C13-120C-4A7E-87C8-5AF8198D29FE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Archigen.Example", "Archigen.Example\Archigen.Example.csproj", "{4F72A841-2B84-43C4-80EF-08C14D35FBFA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C04E0C13-120C-4A7E-87C8-5AF8198D29FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C04E0C13-120C-4A7E-87C8-5AF8198D29FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C04E0C13-120C-4A7E-87C8-5AF8198D29FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C04E0C13-120C-4A7E-87C8-5AF8198D29FE}.Release|Any CPU.Build.0 = Release|Any CPU + {4F72A841-2B84-43C4-80EF-08C14D35FBFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F72A841-2B84-43C4-80EF-08C14D35FBFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F72A841-2B84-43C4-80EF-08C14D35FBFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F72A841-2B84-43C4-80EF-08C14D35FBFA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EE1EDF08-9374-4FE7-AF4D-1F94281F2F87} + EndGlobalSection +EndGlobal diff --git a/Archigen/Archigen/Archigen.csproj b/Archigen/Archigen/Archigen.csproj new file mode 100644 index 0000000..1758136 --- /dev/null +++ b/Archigen/Archigen/Archigen.csproj @@ -0,0 +1,19 @@ + + + + netstandard2.0 + 0.9.0-alpha + true + LICENSE + https://github.com/kesac/Archigen + https://github.com/kesac/Archigen + + + + + True + + + + + diff --git a/Archigen/Archigen/Generator.cs b/Archigen/Archigen/Generator.cs new file mode 100644 index 0000000..60b9f22 --- /dev/null +++ b/Archigen/Archigen/Generator.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; + +namespace Archigen +{ + public class Generator : IGenerator where T : new() + { + public Dictionary Generators { get; set; } + + public Generator() + { + this.Generators = new Dictionary(); + } + + public Generator ForProperty(Expression> expression, IGenerator generator) + { + var type = generator.GetType(); + var next = type.GetMethod("Next"); + + var memberExpression = (MemberExpression) expression.Body; + var property = (PropertyInfo) memberExpression.Member; + + this.Generators.Add(property.Name, new GeneratorInfo(generator, next)); + return this; + } + + + public T Next() + { + var result = new T(); + + foreach(var targetProperty in this.Generators.Keys) + { + var property = result.GetType().GetProperty(targetProperty); + var gInfo = this.Generators[targetProperty]; + + property.SetValue(result, gInfo.Next.Invoke(gInfo.Generator, null)); + } + + return result; + } + } +} diff --git a/Archigen/Archigen/GeneratorInfo.cs b/Archigen/Archigen/GeneratorInfo.cs new file mode 100644 index 0000000..425ce85 --- /dev/null +++ b/Archigen/Archigen/GeneratorInfo.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Text; + +namespace Archigen +{ + public class GeneratorInfo + { + public IGenerator Generator { get; set; } + public MethodInfo Next { get; set; } + + public GeneratorInfo(IGenerator g, MethodInfo next) + { + this.Generator = g; + this.Next = next; + } + + } +} diff --git a/Archigen/Archigen/IGenerator.cs b/Archigen/Archigen/IGenerator.cs new file mode 100644 index 0000000..5739b30 --- /dev/null +++ b/Archigen/Archigen/IGenerator.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Archigen +{ + + public interface IGenerator { } + + // Represents anything that can procedurally + // generate something + public interface IGenerator : IGenerator + { + T Next(); + } +}