Example #1
0
        private void GenerateDataLayer_Click(object sender, EventArgs e)
        {
            StringBuilder        sb             = null;
            List <DatabaseField> DatabaseFields = GetAllDatabaseFields();

            if (DatabaseFields.Count > 0)
            {
                sb = new StringBuilder();

                sb.AppendLine("using System;");
                sb.AppendLine("using System.Text;");
                sb.AppendLine("using System.Linq;");
                sb.AppendLine("using System.Configuration;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Data;");
                sb.AppendLine("using System.Data.SqlClient;");
                sb.AppendLine("using System.Diagnostics.CodeAnalysis;");
                sb.AppendLine("using BB.DataLayer.Abstract;");
                sb.AppendLine();
                sb.AppendLine("namespace BB.DataLayer");
                sb.AppendLine("{");

                StringBuilder        AbstractClass = new StringBuilder();
                StringBuilder        RecordClass   = new StringBuilder();
                StringBuilder        MockClass     = new StringBuilder();
                string               CurrentTable  = string.Empty;
                List <DatabaseField> CurrentFields;
                bool IdExists;
                foreach (var Field in DatabaseFields)
                {
                    //are we onto a new table
                    if (CurrentTable != Field.TableName)
                    {
                        //if this is not the first table, then we need to finish off the last one
                        if (!string.IsNullOrEmpty(CurrentTable))
                        {
                            CurrentFields = DatabaseFields.Where(x => x.TableName == CurrentTable).ToList <DatabaseField>();
                            IdExists      = CurrentFields.Exists(x => x.IsIdentity == true);
                            FinishMockClass(MockClass, CurrentTable, IdExists);

                            //Add the overriding methods for the previous class(es)
                            FinishAbstractClass(CurrentTable, CurrentFields, AbstractClass);
                            FinishRecordClass(CurrentTable, CurrentFields, IdExists, RecordClass);

                            //append the abstract record and mocked class to the main stringbuilder
                            sb.AppendLine(AbstractClass.ToString());
                            sb.AppendLine(RecordClass.ToString());
                            sb.AppendLine(MockClass.ToString());

                            //Renew Class StringBuilders for the next table
                            AbstractClass = new StringBuilder();
                            RecordClass   = new StringBuilder();
                            MockClass     = new StringBuilder();
                        }
                        //Create Collection classes
                        string PluralTableName = GetPlural(Field.TableName);
                        GetParentCollectionClass(Field.TableName, PluralTableName, sb);
                        sb.AppendLine("");
                        GetCollectionClass(Field.TableName, PluralTableName, DatabaseFields.Where(x => x.TableName == Field.TableName).ToList <DatabaseField>(), sb);
                        sb.AppendLine("");
                        GetMockCollectionClass(Field.TableName, PluralTableName, DatabaseFields.Where(x => x.TableName == Field.TableName).ToList <DatabaseField>(), sb);
                        sb.AppendLine("");

                        //initialise the abstract, mock and record classes
                        GetClassHeader("ADL_" + Field.TableName, AbstractClass);
                        GetClassHeader("DL_" + Field.TableName, RecordClass, "ADL_" + Field.TableName);
                        GetClassHeader("MOCK_DL_" + Field.TableName, MockClass, "ADL_" + Field.TableName);
                        //add Error message constant to the mock class
                        MockClass.Append("\t\tpublic const string ERR_SAVE_FAILED = \"Could not save ");
                        MockClass.Append(Field.TableName);
                        MockClass.AppendLine(" record.\";");
                    }

                    //get the property code for the abstract class
                    GetPropertyText(Field, AbstractClass);

                    //get the current table
                    CurrentTable = Field.TableName;
                }

                CurrentFields = DatabaseFields.Where(x => x.TableName == CurrentTable).ToList <DatabaseField>();
                IdExists      = CurrentFields.Exists(x => x.IsIdentity == true);

                FinishMockClass(MockClass, CurrentTable, IdExists);

                //Add the overriding methods for the previous class(es)
                FinishAbstractClass(CurrentTable, CurrentFields, AbstractClass);
                FinishRecordClass(CurrentTable, CurrentFields, IdExists, RecordClass);

                //append the record and mocked class to the main stringbuilder
                sb.AppendLine(AbstractClass.ToString());
                sb.AppendLine();
                sb.AppendLine(RecordClass.ToString());
                sb.AppendLine();
                sb.AppendLine(MockClass.ToString());

                sb.AppendLine("}");

                string filename = Application.StartupPath + "\\..\\..\\..\\BB.DataLayer\\BBDataLayer.cs";
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }

                using (var fs = new FileStream(filename, FileMode.CreateNew))
                {
                    fs.Write(Encoding.ASCII.GetBytes(sb.ToString()), 0, sb.ToString().Length);
                    fs.Flush();
                }
            }
        }