public static CSharpClassDefinition GetRepositoryClassDefinition(this ProjectFeature <DapperProjectSettings> projectFeature) { var classDefinition = new CSharpClassDefinition(); classDefinition.Namespaces.Add("System"); classDefinition.Namespaces.Add("System.Collections.Generic"); classDefinition.Namespaces.Add("System.Data"); classDefinition.Namespaces.Add("System.Data.SqlClient"); classDefinition.Namespaces.Add("System.Linq"); classDefinition.Namespaces.Add("System.Text"); classDefinition.Namespaces.Add("System.Threading.Tasks"); classDefinition.Namespaces.Add("Dapper"); classDefinition.Namespaces.Add("Microsoft.Extensions.Options"); foreach (var table in projectFeature.Project.Database.Tables) { if (table.HasDefaultSchema()) { classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetEntityLayerNamespace()); } else { classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetEntityLayerNamespace(table.Schema)); } classDefinition.Namespaces.AddUnique(projectFeature.GetDapperProject().GetDataLayerContractsNamespace()); } classDefinition.Namespace = projectFeature.GetDapperProject().GetDataLayerRepositoriesNamespace(); classDefinition.Name = projectFeature.GetClassRepositoryName(); classDefinition.BaseClass = "Repository"; classDefinition.Implements.Add(projectFeature.GetInterfaceRepositoryName()); classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition("IOptions<AppSettings>", "appSettings")) { Invocation = "base(appSettings)" }); var dbos = projectFeature.DbObjects.Select(dbo => dbo.FullName).ToList(); var tables = projectFeature.Project.Database.Tables.Where(t => dbos.Contains(t.FullName)).ToList(); var views = projectFeature.Project.Database.Views.Where(v => dbos.Contains(v.FullName)).ToList(); foreach (var table in tables) { classDefinition.Methods.Add(GetGetAllMethod(projectFeature, table)); if (table.PrimaryKey != null) { classDefinition.Methods.Add(GetGetMethod(projectFeature, table)); } classDefinition.Methods.Add(GetAddMethod(projectFeature, table)); if (table.PrimaryKey != null) { classDefinition.Methods.Add(GetUpdateMethod(projectFeature, table)); classDefinition.Methods.Add(GetRemoveMethod(projectFeature, table)); } foreach (var unique in table.Uniques) { classDefinition.Methods.Add(GetByUniqueMethod(projectFeature, table, unique)); } } foreach (var view in views) { classDefinition.Methods.Add(GetGetAllMethod(projectFeature, view)); } return(classDefinition); }
public static RepositoryClassDefinition GetRepositoryClassDefinition(this ProjectFeature <EntityFrameworkCoreProjectSettings> projectFeature) { var project = projectFeature.GetEntityFrameworkCoreProject(); var definition = new RepositoryClassDefinition { Namespaces = { "System", "System.Collections.Generic", "System.Data.SqlClient", "System.Linq", "System.Threading.Tasks", "Microsoft.EntityFrameworkCore" }, Namespace = project.GetDataLayerRepositoriesNamespace(), AccessModifier = AccessModifier.Public, Name = projectFeature.GetClassRepositoryName(), BaseClass = "Repository", Implements = { projectFeature.GetInterfaceRepositoryName() }, Constructors = { new ClassConstructorDefinition(AccessModifier.Public, new ParameterDefinition(project.GetDbContextName(project.Database), "dbContext")) { Invocation = "base(dbContext)" } } }; foreach (var table in project.Database.Tables) { definition.Namespaces .AddUnique(projectFeature.Project.Database.HasDefaultSchema(table) ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema)); definition.Namespaces.AddUnique(project.GetDataLayerContractsNamespace()); } foreach (var table in projectFeature.GetTables()) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(table); if (projectSelection.Settings.EntitiesWithDataContracts) { definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace()); } foreach (var foreignKey in table.ForeignKeys) { if (string.IsNullOrEmpty(foreignKey.Child)) { var child = projectFeature.Project.Database.FindTable(foreignKey.Child); if (child != null) { definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace()); } } } if (table.ForeignKeys.Count == 0) { definition.Methods.Add(GetGetAllMethodWithoutForeigns(projectFeature, projectSelection, table)); } else { definition.GetGetAllMethod(projectFeature, projectSelection, table); } if (table.PrimaryKey != null) { definition.Methods.Add(GetGetMethod(projectFeature, projectSelection, table)); } foreach (var unique in table.Uniques) { definition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique)); } if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } foreach (var view in projectFeature.GetViews()) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(view); if (projectSelection.Settings.EntitiesWithDataContracts) { definition.Namespaces.AddUnique(project.GetDataLayerDataContractsNamespace()); } definition.Methods.Add(GetGetAllMethod(projectFeature, view)); if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } foreach (var tableFunction in projectFeature.GetTableFunctions()) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(tableFunction); definition.Methods.Add(GetGetAllMethod(projectFeature, tableFunction)); if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } foreach (var storedProcedure in projectFeature.GetStoredProcedures()) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(storedProcedure); definition.Methods.Add(GetGetAllMethod(projectFeature, storedProcedure)); if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } return(definition); }
public static RepositoryClassDefinition GetRepositoryClassDefinition(this ProjectFeature <EntityFrameworkCoreProjectSettings> projectFeature) { var efCoreProject = projectFeature.GetEntityFrameworkCoreProject(); var definition = new RepositoryClassDefinition { Namespaces = { "System", "System.Linq", "System.Threading.Tasks", "Microsoft.EntityFrameworkCore" }, Namespace = efCoreProject.GetDataLayerRepositoriesNamespace(), Name = projectFeature.GetClassRepositoryName(), BaseClass = "Repository", Implements = { projectFeature.GetInterfaceRepositoryName() }, Constructors = { new ClassConstructorDefinition(new ParameterDefinition(efCoreProject.GetDbContextName(efCoreProject.Database), "dbContext")) { Invocation = "base(dbContext)" } } }; foreach (var table in efCoreProject.Database.Tables) { definition.Namespaces .AddUnique(projectFeature.Project.Database.HasDefaultSchema(table) ? efCoreProject.GetEntityLayerNamespace() : efCoreProject.GetEntityLayerNamespace(table.Schema)); definition.Namespaces.AddUnique(efCoreProject.GetDataLayerContractsNamespace()); } var tables = projectFeature .Project .Database .Tables .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName)) .ToList(); foreach (var table in tables) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(table); if (projectSelection.Settings.EntitiesWithDataContracts) { definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace()); } foreach (var foreignKey in table.ForeignKeys) { if (string.IsNullOrEmpty(foreignKey.Child)) { var child = projectFeature.Project.Database.FindTable(foreignKey.Child); if (child != null) { definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace()); } } } if (table.ForeignKeys.Count == 0) { definition.GetGetAllMethodWithoutForeigns(projectFeature, projectSelection, table); } else { definition.GetGetAllMethod(projectFeature, projectSelection, table); } if (table.PrimaryKey != null) { definition.Methods.Add(GetGetMethod(projectFeature, projectSelection, table)); } foreach (var unique in table.Uniques) { definition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique)); } if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } var views = projectFeature .Project .Database .Views .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName)) .ToList(); foreach (var view in views) { var projectSelection = projectFeature.GetEntityFrameworkCoreProject().GetSelection(view); if (projectSelection.Settings.EntitiesWithDataContracts) { definition.Namespaces.AddUnique(efCoreProject.GetDataLayerDataContractsNamespace()); } definition.GetGetAllMethod(projectFeature, view); if (projectSelection.Settings.SimplifyDataTypes) { definition.SimplifyDataTypes(); } } return(definition); }
public static CSharpClassDefinition GetRepositoryClassDefinition(this ProjectFeature projectFeature) { var entityFrameworkCoreProject = projectFeature.GetEntityFrameworkCoreProject(); var classDefinition = new CSharpClassDefinition(); classDefinition.Namespaces.Add("System"); classDefinition.Namespaces.Add("System.Linq"); classDefinition.Namespaces.Add("System.Threading.Tasks"); classDefinition.Namespaces.Add("Microsoft.EntityFrameworkCore"); foreach (var table in entityFrameworkCoreProject.Database.Tables) { classDefinition.Namespaces.AddUnique(table.HasDefaultSchema() ? entityFrameworkCoreProject.GetEntityLayerNamespace() : entityFrameworkCoreProject.GetEntityLayerNamespace(table.Schema)); classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerContractsNamespace()); } classDefinition.Namespace = entityFrameworkCoreProject.GetDataLayerRepositoriesNamespace(); classDefinition.Name = projectFeature.GetClassRepositoryName(); classDefinition.BaseClass = "Repository"; classDefinition.Implements.Add(projectFeature.GetInterfaceRepositoryName()); classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(projectFeature.Project.Database.GetDbContextName(), "dbContext")) { Invocation = "base(dbContext)" }); var tables = projectFeature .Project .Database .Tables .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName)) .ToList(); foreach (var table in tables) { if (entityFrameworkCoreProject.Settings.EntitiesWithDataContracts.Contains(table.FullName)) { classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace()); } foreach (var foreignKey in table.ForeignKeys) { if (string.IsNullOrEmpty(foreignKey.Child)) { var child = projectFeature.Project.Database.FindTableBySchemaAndName(foreignKey.Child); if (child != null) { classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace()); } } } classDefinition.GetGetAllMethod(projectFeature, table); if (table.PrimaryKey != null) { classDefinition.Methods.Add(GetGetMethod(projectFeature, table)); } foreach (var unique in table.Uniques) { classDefinition.Methods.Add(GetGetByUniqueMethods(projectFeature, table, unique)); } classDefinition.Methods.Add(GetAddMethod(projectFeature, table)); if (table.PrimaryKey != null) { classDefinition.Methods.Add(GetUpdateMethod(projectFeature, table)); classDefinition.Methods.Add(GetRemoveMethod(projectFeature, table)); } } var views = projectFeature .Project .Database .Views .Where(item => projectFeature.DbObjects.Select(dbo => dbo.FullName).Contains(item.FullName)) .ToList(); foreach (var view in views) { if (entityFrameworkCoreProject.Settings.EntitiesWithDataContracts.Contains(view.FullName)) { classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetDataLayerDataContractsNamespace()); } classDefinition.GetGetAllMethod(projectFeature, view); } return(classDefinition); }