/CodeDomSeedGenerator

A library for generating seed data from in memory objects

Primary LanguageC#

CodeDomSeedGenerator

A library for generating seed data from in memory objects.

Sample usage

CDSeedGenerator is currently the main target class. It can be used to generate a string which contains C# code which will recreate the list that has been added as seed data. The resultant class will have a method named GenerateSeedData that returns an IEnumerable<DataTypePassedIn>

List<SomeClass> testData = ...
var cdCompile = new CDSeedGenerator(nameSpace, className);
cdCompile.AddSeedData(testData);

string seedFile;
using (StringWriter tw = new StringWriter())
{
  cdCompileUnit.WriteToStream(tw);
  seedFile = tw.ToString();
}

An example of the generated code is below (this example can be generated by running CodeDomConsole and the output will be written to CodeDomeConsoles output directory in a file names TestClass.cs)

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace SampleNameSpace
{
    using CodeDomConsole;
    
    
    public sealed class TestClass
    {
        
        public static System.Collections.Generic.IEnumerable<CodeDomConsole.ClassWithProps> GenerateSeedData()
        {
            System.Collections.Generic.List<CodeDomConsole.ClassWithProps> seedData = new System.Collections.Generic.List<CodeDomConsole.ClassWithProps>();
            ClassWithProps classwithprops0 = new ClassWithProps();
            classwithprops0.IntProperty = 4;
            classwithprops0.EnumProperty = OtherNameSpace.EnumValues.Value2;
            seedData.Add(classwithprops0);
            ClassWithProps classwithprops1 = new ClassWithProps();
            classwithprops1.IntProperty = 4;
            classwithprops1.EnumProperty = OtherNameSpace.EnumValues.Value2;
            seedData.Add(classwithprops1);
            return seedData;
        }
    }
}

TODO

  • Support adding multiple seed sets (currently the generated method name will be the same so if multiple types are seeded the resultant code would not compile).
  • Use roslyn compiler to validate that the generated code compiles
  • Add support for Seeding related data through ef core