static void Main(string[] args) { Console.WriteLine("Reading Generation Info File \n"); FileIniDataParser parser = new FileIniDataParser(); IniData genData = parser.LoadFile("generation.ini"); Console.WriteLine("Generation Info File Read \n"); foreach (SectionData genSection in genData.Sections) { Console.WriteLine(String.Format("Analyzing {0} \n", genSection.SectionName)); Console.WriteLine(String.Format("=============================================== \n", genSection.SectionName)); GenerationInfo info = new GenerationInfo(); info.CompanyName = genSection.Keys["companyname"].Replace(";", ""); info.DataNameSpace = genSection.Keys["datanamespace"].Replace(";", ""); info.FolderPath = genSection.Keys["path"].Replace(";", ""); info.ServerName = genSection.Keys["server"].Replace(";", ""); info.DataBase = genSection.Keys["database"].Replace(";", ""); MGenerator.Tools.Generator.GenerateFromDatabase(info); Console.WriteLine(String.Format("=============================================== \n", genSection.SectionName)); Console.WriteLine(String.Format("done \n", genSection.SectionName)); Console.WriteLine(String.Format("=============================================== \n", genSection.SectionName)); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
/// <summary> /// Generates Data Access, Mapping and Transfer Code from the Database /// </summary> /// <param name="FolderPath"></param> /// <param name="ServerName"></param> /// <param name="DatabaseName"></param> public static void GenerateFromDatabase(GenerationInfo GenInfo) { DirectoryInfo d = new DirectoryInfo(GenInfo.FolderPath); Microsoft.SqlServer.Management.Common.ServerConnection svrCon = new Microsoft.SqlServer.Management.Common.ServerConnection(GenInfo.ServerName); Server svr = new Server(svrCon); String DbScriptsPath = GenInfo.FolderPath + @"\Procedures"; foreach (Microsoft.SqlServer.Management.Smo.Database datab in svr.Databases) { if (datab.IsAccessible) { if (!(DatabasesToIgnore().Contains(datab.Name))) { if (datab.Name.ToLower() == GenInfo.DataBase.ToLower()) { Console.Write("Generating Code for Database: " + datab.Name + "\n"); String RepositoryPath = GenInfo.FolderPath + @"\" + datab.Name; // The Current Repository String DataLayerNamespace = String.Format("{0}.{1}.{2}", GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name); #region [ Clean / Create Directory ] if (Directory.Exists(RepositoryPath)) { // Directory Exists clean it out DirectoryInfo dRepository = new DirectoryInfo(RepositoryPath); foreach (FileInfo f in dRepository.GetFiles("*.*", SearchOption.AllDirectories)) { f.Delete(); } } else { Directory.CreateDirectory(RepositoryPath); } #endregion #region [ Tables ] String RepositoryPathProcedures = RepositoryPath + @"\Procedures"; if (Directory.Exists(RepositoryPathProcedures)) { Directory.Delete(RepositoryPathProcedures); } Directory.CreateDirectory(RepositoryPathProcedures); Int32 GenerationStep = 0; DALGenerator dg = new DALGenerator(); POCOGenerator pg = new POCOGenerator(); foreach (Table t in datab.Tables) { GenerationStep = 0; try { if (t.Name == "Loan") { "$".IsNormalized(); } if (!(t.IsSystemObject)) { // Check if the table has a primary key, if not then dont generate anything. Table muist be normalized if (ValidateSqlTable(t)) { #region [ Generation Root ] String cpFilename = RepositoryPathProcedures + @"\\" + "cp_" + t.Name + ".sql"; String sspFileName = RepositoryPathProcedures + @"\\" + "ssp_" + t.Name + ".sql"; String EntityFileName = RepositoryPath + @"\\" + t.Name + ".cs"; String DALFileName = RepositoryPath + @"\\" + t.Name + "DAL.cs"; String DOMFileName = RepositoryPath + @"\\" + t.Name + "DomainObject.cs"; GenerationStep = 1; // [0] Stored Procedures MGenerator.Tools.SqlGeneration.CrudProcedureGenerator gen = new SqlGeneration.CrudProcedureGenerator(datab, t); gen.Generate(RepositoryPathProcedures, MGenerator.Tools.SqlGeneration.ProcedureGenerationType.Alter); GenerationStep = 2; ObjectiveSearchProcedureGenerator ospgen = new ObjectiveSearchProcedureGenerator(datab, t); ospgen.Generate(RepositoryPathProcedures); GenerationStep = 3; // [1] Data Access Classes WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, dg.BuildDalClass(String.Format("{0}.{1}", GenInfo.CompanyName, GenInfo.DataNameSpace), datab.Name, t)); GenerationStep = 4; // [2] Entity / Structure Class WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, pg.BuildPoc(t)); GenerationStep = 5; // [3] Service Layer DomainServiceGraph svcgen = new DomainServiceGraph(); WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, svcgen.BuildDomainObject(String.Format("{0}.{1}", GenInfo.CompanyName, GenInfo.DataNameSpace), datab.Name, t)); GenerationStep = 6; // [4] API ApiGraph api = new ApiGraph(); string api_name = api.WriteApiStation(GenInfo, datab, t,RepositoryPath); #endregion Console.Write(t.Name + "....COMPLETE" + "\n"); } } } catch (Exception x) { Console.Write(t.Name + "....ERROR" + "\n"); // File.WriteAllText(RepositoryPath + t.Name + ".txt", x.Message + "\n Generation Step:" + GenerationStep.ToString() ); continue; } } #endregion #region [ Views ] foreach (View view in datab.Views) { try { if (view.Name == "vwLOLicense") { String fd = ""; } if (!(view.IsSystemObject)) { if (ValidateSqlView(view)) { ObjectiveSearchProcedureGenerator ospgen = new ObjectiveSearchProcedureGenerator(datab, view); DomainServiceGraph svcgen = new DomainServiceGraph(); ospgen.Generate(RepositoryPathProcedures); WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, dg.BuildDalClass(String.Format("{0}.{1}", GenInfo.CompanyName, GenInfo.DataNameSpace), datab.Name, view)); WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, pg.BuildPoco(view)); WriteCsharpFile(GenInfo.CompanyName, GenInfo.DataNameSpace, datab.Name, RepositoryPath, svcgen.BuildDomainObject(String.Format("{0}.{1}", GenInfo.CompanyName, GenInfo.DataNameSpace), datab.Name, view)); Console.Write(view.Name + "....COMPLETE" + "\n"); } } } catch (Exception x) { Console.Write(view.Name + "....ERROR" + "\n"); continue; } } #endregion } } } } }