public ServerNamespace(ServerSchema serverSchema, TableSchema tableSchema) { // Initialize the object. this.serverchema = serverSchema; this.tableSchema = tableSchema; //namespace MarkThree.UnitTest.Server //{ // using MarkThree; // using System; // using System.Collections.Generic; // using System.Data; // using System.Data.SqlClient; // using System.Threading; this.Name = this.serverchema.TargetNamespace; foreach (string reference in serverSchema.References) { this.Imports.Add(new CodeNamespaceImport(reference)); } this.Imports.Add(new CodeNamespaceImport("MarkThree")); this.Imports.Add(new CodeNamespaceImport("System")); this.Imports.Add(new CodeNamespaceImport("System.Collections.Generic")); this.Imports.Add(new CodeNamespaceImport("System.Data")); this.Imports.Add(new CodeNamespaceImport("System.Data.SqlClient")); this.Imports.Add(new CodeNamespaceImport("System.Threading")); // This is the class that provides a transacted interface into the data model. this.Types.Add(new ServerClass(serverSchema, tableSchema)); }
/// <summary> /// Generate the code from the inputs. /// </summary> /// <param name="inputFileName">The name of the input file.</param> /// <param name="inputFileContent">The contents of the input file.</param> /// <returns>A buffer containing the generated code.</returns> protected static byte[] GenerateCode(ServerSchema coreInterfaceSchema, TableSchema tableSchema) { // Create a namespace MarkThree.MiddleTier add it to the module. This namespace MarkThree.MiddleTier all the relavent code in it. CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); codeCompileUnit.Namespaces.Add(new ServerNamespace(coreInterfaceSchema, tableSchema)); // This will generate the source code and return it as an array of bytes. StringWriter stringWriter = new StringWriter(); ServerCompiler.CodeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, ServerCompiler.codeGeneratorOptions); return(System.Text.Encoding.UTF8.GetBytes(stringWriter.ToString())); }
/// <summary> /// Creates a CodeDOM class that provides a transacted interface to a data table. /// </summary> /// <param name="coreInterfaceSchema">The schema that defines the data model.</param> /// <param name="tableSchema">The schema that defines the table.</param> public ServerClass(ServerSchema serverSchema, TableSchema tableSchema) { // Initialize the object. this.coreInterfaceSchema = serverSchema; this.tableSchema = tableSchema; // /// <summary> // /// Provides transaction operations for the Department table. // /// </summary> // [System.ComponentModel.DesignerCategoryAttribute("code")] // [System.Diagnostics.DebuggerStepThrough()] // public class Department // { this.Comments.Add(new CodeCommentStatement("<summary>", true)); this.Comments.Add(new CodeCommentStatement(string.Format("Provides transaction operations for the {0} table.", tableSchema.Name), true)); this.Comments.Add(new CodeCommentStatement("</summary>", true)); this.CustomAttributes.Add(new CodeAttributeDeclaration("System.ComponentModel.DesignerCategoryAttribute", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression("code")) })); this.CustomAttributes.Add(new CodeAttributeDeclaration("System.Diagnostics.DebuggerStepThrough")); this.TypeAttributes = TypeAttributes.Public; this.IsClass = true; this.Name = tableSchema.Name; // // This is the name of the volatile resource manager used by this class. // private const string VolatileResource = "DataModel"; CodeMemberField volatileResourceField = new CodeMemberField(typeof(string), "VolatileResource"); volatileResourceField.Comments.Add(new CodeCommentStatement("This is the name of the volatile resource manager used by this class.")); volatileResourceField.Attributes = MemberAttributes.Private | MemberAttributes.Const; volatileResourceField.InitExpression = new CodePrimitiveExpression(this.coreInterfaceSchema.VolatileStoreName); this.Members.Add(volatileResourceField); // // This is the name of the durable resource manager used by this class. // private const string DurableResource = "UnitTest"; CodeMemberField durableResourceField = new CodeMemberField(typeof(string), "DurableResource"); durableResourceField.Comments.Add(new CodeCommentStatement("This is the name of the durable resource manager used by this class.")); durableResourceField.Attributes = MemberAttributes.Private | MemberAttributes.Const; durableResourceField.InitExpression = new CodePrimitiveExpression(this.coreInterfaceSchema.DurableStoreName); this.Members.Add(durableResourceField); // Add the member methods to the class. this.Members.Add(new Insert(tableSchema)); this.Members.Add(new Update(tableSchema)); this.Members.Add(new Delete(tableSchema)); //this.Members.Add(new Archive(coreInterfaceSchema, tableSchema)); }
/// <summary> /// Create a CodeDOM description of a method used to provide a transacted interface to a data model table. /// </summary> /// <param name="tableSchema"></param> public ServerMethod(TableSchema tableSchema) { // Initialize the object. this.tableSchema = tableSchema; this.serverSchema = tableSchema.DataModelSchema as ServerSchema; }
static int Main(string[] args) { try { // Defaults ServerCompiler.name = string.Empty; ServerCompiler.volatileStoreName = string.Empty; ServerCompiler.durableStoreName = string.Empty; ServerCompiler.references = new System.Collections.Generic.List <string>(); ServerCompiler.targetNamespace = "DefaultNamespace"; // The command line parser is driven by different states that are triggered by the flags read. Unless a flag has // been read, the command line parser assumes that it's reading the file name from the command line. argumentState = ArgumentState.InputFileName; // Parse the command line for arguments. foreach (string argument in args) { // Decode the current argument into a state change (or some other action). switch (argument) { case "-ds": argumentState = ArgumentState.DurableStore; continue; case "-i": argumentState = ArgumentState.InputFileName; continue; case "-n": argumentState = ArgumentState.Name; continue; case "-ns": argumentState = ArgumentState.TargetNamespace; continue; case "-out": argumentState = ArgumentState.OutputFileName; continue; case "-ref": argumentState = ArgumentState.Reference; continue; case "-t": argumentState = ArgumentState.TargetTable; continue; case "-vs": argumentState = ArgumentState.VolatileStore; continue; } // The parsing state will determine which variable is read next. switch (argumentState) { case ArgumentState.DurableStore: ServerCompiler.durableStoreName = argument; break; case ArgumentState.InputFileName: ServerCompiler.inputFileName = argument; break; case ArgumentState.Name: ServerCompiler.name = argument; break; case ArgumentState.OutputFileName: ServerCompiler.outputFileName = argument; break; case ArgumentState.TargetNamespace: ServerCompiler.targetNamespace = argument; break; case ArgumentState.TargetTable: ServerCompiler.targetTableName = argument; break; case ArgumentState.Reference: ServerCompiler.references.Add(argument); break; case ArgumentState.VolatileStore: ServerCompiler.volatileStoreName = argument; break; } // The default state is to look for the input file name on the command line. argumentState = ArgumentState.InputFileName; } // Throw a usage message back at the user if no file name was given. if (inputFileName == null || outputFileName == null || name == string.Empty || volatileStoreName == string.Empty || durableStoreName == string.Empty) { throw new Exception("Usage: Generator -i <input file name> -t <target table name>"); } // Read the schema into a string. This emulates the way that the IDE would normally call a code generator. Create // the MiddleTierSchema (like a Schema, but with extra helping functions and relations for this type of code // generation). StreamReader streamReader = new StreamReader(inputFileName); ServerSchema serverSchema = new ServerSchema(streamReader.ReadToEnd()); TableSchema tableSchema = serverSchema.Tables.Find(ServerCompiler.targetTableName); // Install the parameters into the schema. serverSchema.Name = ServerCompiler.name; serverSchema.TargetNamespace = ServerCompiler.targetNamespace; serverSchema.References = ServerCompiler.references; serverSchema.VolatileStoreName = ServerCompiler.volatileStoreName; serverSchema.DurableStoreName = ServerCompiler.durableStoreName; // This will generate a buffer of source code from the intput table schema. byte[] buffer = GenerateCode(serverSchema, tableSchema); // Write the buffer to the specified UTF8 output file. StreamWriter streamWriter = new StreamWriter(outputFileName); streamWriter.Write(Encoding.UTF8.GetString(buffer)); streamWriter.Close(); } catch (Exception exception) { // Write the exceptions to the console. Console.WriteLine(exception.Message); } // This indicates the table was compiled successfully. return(0); }