Skip to content

Commit

Permalink
Initialize repo with working class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
kesac committed Oct 26, 2021
1 parent abe8297 commit ef566a3
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions Archigen/Archigen.Example/Archigen.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Archigen\Archigen.csproj" />
</ItemGroup>

</Project>
61 changes: 61 additions & 0 deletions Archigen/Archigen.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;

namespace Archigen.Example
{

public class StringGenerator : IGenerator<string>
{
public string Next()
{
return "This is a string";
}
}

public class NumberGenerator : IGenerator<int>
{
public int Next()
{
return 42;
}
}

public class UserGenerator : IGenerator<User>
{
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<User>()
.ForProperty<string>(x => x.FirstName, new StringGenerator())
.ForProperty<string>(x => x.LastName, new StringGenerator())
.ForProperty<int>(x => x.Age, new NumberGenerator())
.ForProperty<User>(x => x.Partner, new UserGenerator());

for(int i = 0; i < 10; i++)
{
Console.WriteLine(g.Next());
}

}
}
}
31 changes: 31 additions & 0 deletions Archigen/Archigen.sln
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions Archigen/Archigen/Archigen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.9.0-alpha</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/kesac/Archigen</PackageProjectUrl>
<RepositoryUrl>https://github.com/kesac/Archigen</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
46 changes: 46 additions & 0 deletions Archigen/Archigen/Generator.cs
Original file line number Diff line number Diff line change
@@ -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<T> : IGenerator<T> where T : new()
{
public Dictionary<string, GeneratorInfo> Generators { get; set; }

public Generator()
{
this.Generators = new Dictionary<string, GeneratorInfo>();
}

public Generator<T> ForProperty<U>(Expression<Func<T, U>> expression, IGenerator<U> 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;
}
}
}
20 changes: 20 additions & 0 deletions Archigen/Archigen/GeneratorInfo.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
16 changes: 16 additions & 0 deletions Archigen/Archigen/IGenerator.cs
Original file line number Diff line number Diff line change
@@ -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<T> : IGenerator
{
T Next();
}
}

0 comments on commit ef566a3

Please sign in to comment.