public void TestCamelCase() { Assert.True(NamingConvention.GetCamelCase("FOO") == "foo"); Assert.True(NamingConvention.GetCamelCase("Bar") == "bar"); Assert.True(NamingConvention.GetCamelCase("zaz") == "zaz"); Assert.True(NamingConvention.GetCamelCase("FooBarZaz") == "fooBarZaz"); Assert.True(NamingConvention.GetCamelCase("foo bar zaz") == "fooBarZaz"); Assert.True(NamingConvention.GetCamelCase("foo_bar_zaz") == "fooBarZaz"); }
public static DataContractClassDefinition GetDataContractClassDefinition(this EntityFrameworkCoreProject project, ITable table, bool isDomainDrivenDesign) { var definition = new DataContractClassDefinition { Namespaces = { "System" }, Namespace = isDomainDrivenDesign ? project.GetDomainQueryModelsNamespace() : project.GetDataLayerDataContractsNamespace(), AccessModifier = AccessModifier.Public, Name = project.GetDataContractName(table), DbObject = table }; foreach (var column in table.Columns) { definition.Properties.Add(new PropertyDefinition(AccessModifier.Public, project.Database.ResolveDatabaseType(column), project.GetPropertyName(table, column)) { IsAutomatic = true }); } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = project.Database.FindTable(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyAlias = NamingConvention.GetCamelCase(project.GetEntityName(foreignTable)); foreach (var column in foreignTable?.GetColumnsWithNoPrimaryKey()) { var col = (Column)column; var propertyName = project.GetPropertyName(foreignTable, col); var target = string.Format("{0}{1}", project.GetEntityName(foreignTable), propertyName); if (definition.Properties.Count(item => item.Name == propertyName) == 0) { definition.Properties.Add(new PropertyDefinition(AccessModifier.Public, project.Database.ResolveDatabaseType(col), target) { IsAutomatic = true }); } } } definition.SimplifyDataTypes(); return(definition); }
public void TestCamelCase() { Assert.True("foo" == NamingConvention.GetCamelCase("FOO")); Assert.True("bar" == NamingConvention.GetCamelCase("Bar")); Assert.True("zaz" == NamingConvention.GetCamelCase("zaz")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("FooBarZaz")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("foo bar zaz")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("FOO BAR ZAZ")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("foo_bar_zaz")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("FOO-BAR-ZAZ")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("foo-bar-zaz")); Assert.True("fooBarZaz" == NamingConvention.GetCamelCase("FOO-BAR-ZAZ")); }
private static void ScaffoldDataContracts(EntityFrameworkCoreProject project) { foreach (var table in project.Database.Tables) { if (!project.Settings.EntitiesWithDataContracts.Contains(table.FullName)) { continue; } var classDefinition = new CSharpClassDefinition { Namespaces = new List <string> { "System" }, Namespace = project.GetDataLayerDataContractsNamespace(), Name = table.GetDataContractName() }; foreach (var column in table.Columns) { classDefinition.Properties.Add(new PropertyDefinition(column.GetClrType(), column.GetPropertyName())); } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = project.Database.FindTableByFullName(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyAlias = NamingConvention.GetCamelCase(foreignTable.GetEntityName()); foreach (var column in foreignTable?.GetColumnsWithOutPrimaryKey()) { var target = string.Format("{0}{1}", foreignTable.GetEntityName(), column.GetPropertyName()); if (classDefinition.Properties.Where(item => item.Name == column.GetPropertyName()).Count() == 0) { classDefinition.Properties.Add(new PropertyDefinition(column.GetClrType(), target)); } } } CSharpCodeBuilder.CreateFiles(project.OutputDirectory, project.GetDataLayerDataContractsDirectory(), project.Settings.ForceOverwrite, classDefinition); } }
public static DataContractClassDefinition GetDataContractClassDefinition(this EntityFrameworkCoreProject project, ITable table) { var definition = new DataContractClassDefinition { Namespaces = { "System" }, Namespace = project.GetDataLayerDataContractsNamespace(), Name = project.GetDataContractName(table) }; foreach (var column in table.Columns) { definition.Properties.Add(new PropertyDefinition(project.Database.ResolveDatabaseType(column), column.GetPropertyName())); } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = project.Database.FindTable(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyAlias = NamingConvention.GetCamelCase(project.GetEntityName(foreignTable)); foreach (var column in foreignTable?.GetColumnsWithNoPrimaryKey()) { var target = string.Format("{0}{1}", project.GetEntityName(foreignTable), column.GetPropertyName()); if (definition.Properties.Count(item => item.Name == column.GetPropertyName()) == 0) { definition.Properties.Add(new PropertyDefinition(project.Database.ResolveDatabaseType(column), target)); } } } return(definition); }
private static void GetGetAllMethod(this CSharpClassDefinition classDefinition, ProjectFeature projectFeature, ITable table) { var entityFrameworkCoreProject = projectFeature.GetEntityFrameworkCoreProject(); var returnType = string.Empty; var lines = new List <ILine>(); if (entityFrameworkCoreProject.Settings.EntitiesWithDataContracts.Contains(table.FullName)) { var entityAlias = NamingConvention.GetCamelCase(table.GetEntityName()); returnType = table.GetDataContractName(); var dataContractPropertiesSets = new[] { new { IsForeign = false, Type = string.Empty, Nullable = false, ObjectSource = string.Empty, PropertySource = string.Empty, Target = string.Empty } }.ToList(); foreach (var column in table.Columns) { var propertyName = column.GetPropertyName(); dataContractPropertiesSets.Add(new { IsForeign = false, Type = column.Type, Nullable = column.Nullable, ObjectSource = entityAlias, PropertySource = propertyName, Target = propertyName }); } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = projectFeature.Project.Database.FindTableByFullName(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyAlias = NamingConvention.GetCamelCase(foreignTable.GetEntityName()); foreach (var column in foreignTable?.GetColumnsWithOutPrimaryKey()) { if (dataContractPropertiesSets.Where(item => string.Format("{0}.{1}", item.ObjectSource, item.PropertySource) == string.Format("{0}.{1}", entityAlias, column.GetPropertyName())).Count() == 0) { var target = string.Format("{0}{1}", foreignTable.GetEntityName(), column.GetPropertyName()); dataContractPropertiesSets.Add(new { IsForeign = true, Type = column.Type, Nullable = column.Nullable, ObjectSource = foreignKeyAlias, PropertySource = column.GetPropertyName(), Target = target }); } } } lines.Add(new CommentLine(" Get query from DbSet")); lines.Add(new CodeLine("var query = from {0} in DbContext.Set<{1}>()", entityAlias, table.GetEntityName())); foreach (var foreignKey in table.ForeignKeys) { var foreignTable = projectFeature.Project.Database.FindTableByFullName(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyEntityName = foreignTable.GetEntityName(); var foreignKeyAlias = NamingConvention.GetCamelCase(foreignTable.GetEntityName()); if (foreignTable.HasDefaultSchema()) { classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetEntityLayerNamespace()); } else { classDefinition.Namespaces.AddUnique(entityFrameworkCoreProject.GetEntityLayerNamespace(foreignTable.Schema)); } if (foreignKey.Key.Count == 0) { lines.Add(new PreprocessorDirectiveLine(1, " There isn't definition for key in foreign key '{0}' in your current database", foreignKey.References)); } else if (foreignKey.Key.Count == 1) { if (foreignTable == null) { lines.Add(LineHelper.Warning(" There isn't definition for '{0}' in your current database", foreignKey.References)); } else { var column = table.Columns.FirstOrDefault(item => item.Name == foreignKey.Key[0]); var x = NamingExtensions.namingConvention.GetPropertyName(foreignKey.Key[0]); var y = NamingExtensions.namingConvention.GetPropertyName(foreignTable.PrimaryKey.Key[0]); if (column.Nullable) { lines.Add(new CodeLine(1, "join {0}Join in DbContext.Set<{1}>() on {2}.{3} equals {0}Join.{4} into {0}Temp", foreignKeyAlias, foreignKeyEntityName, entityAlias, x, y)); lines.Add(new CodeLine(2, "from {0} in {0}Temp.Where(relation => relation.{2} == {1}.{3}).DefaultIfEmpty()", foreignKeyAlias, entityAlias, x, y)); } else { lines.Add(new CodeLine(1, "join {0} in DbContext.Set<{1}>() on {2}.{3} equals {0}.{4}", foreignKeyAlias, foreignKeyEntityName, entityAlias, x, y)); } } } else { lines.Add(LineHelper.Warning(" Add logic for foreign key with multiple key")); } } lines.Add(new CodeLine(1, "select new {0}", returnType)); lines.Add(new CodeLine(1, "{")); for (var i = 0; i < dataContractPropertiesSets.Count; i++) { var property = dataContractPropertiesSets[i]; if (string.IsNullOrEmpty(property.ObjectSource) && string.IsNullOrEmpty(property.Target)) { continue; } if (property.IsForeign) { if (property.Type == "binary" || property.Type == "image" || property.Type == "varbinary") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Byte[]) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "bit") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Boolean?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type.Contains("char") || property.Type.Contains("text")) { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? string.Empty : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type.Contains("date")) { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(DateTime?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "tinyint") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Byte?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "smallint") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Int16?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "bigint") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Int64?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "int") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Int32?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "decimal" || property.Type == "money" || property.Type == "smallmoney") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Decimal?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "float") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Double?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "real") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Single?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else if (property.Type == "uniqueidentifier") { if (property.Nullable) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Guid?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } lines.Add(new CodeLine(1, "};")); lines.Add(new CodeLine()); } else { returnType = table.GetSingularName(); lines.Add(new CommentLine(" Get query from DbSet")); if (entityFrameworkCoreProject.Settings.DeclareDbSetPropertiesInDbContext) { lines.Add(new CodeLine("var query = DbContext.{0}.AsQueryable();", table.GetPluralName())); } else { lines.Add(new CodeLine("var query = DbContext.Set<{0}>().AsQueryable();", table.GetSingularName())); } lines.Add(new CodeLine()); } var parameters = new List <ParameterDefinition> { }; if (table.ForeignKeys.Count == 0) { lines.Add(new CodeLine("return query;")); } else { for (var i = 0; i < table.ForeignKeys.Count; i++) { var foreignKey = table.ForeignKeys[i]; if (foreignKey.Key.Count == 1) { var column = table.Columns.First(item => item.Name == foreignKey.Key[0]); var parameterName = NamingExtensions.namingConvention.GetParameterName(column.Name); parameters.Add(new ParameterDefinition(column.GetClrType(), parameterName, "null")); if (column.IsString()) { lines.Add(new CodeLine("if (!string.IsNullOrEmpty({0}))", NamingExtensions.namingConvention.GetParameterName(column.Name))); lines.Add(new CodeLine("{")); lines.Add(new CommentLine(1, " Filter by: '{0}'", column.Name)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine("}")); lines.Add(new CodeLine()); } else { lines.Add(new CodeLine("if ({0}.HasValue)", NamingExtensions.namingConvention.GetParameterName(column.Name))); lines.Add(new CodeLine("{")); lines.Add(new CommentLine(1, " Filter by: '{0}'", column.Name)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine("}")); lines.Add(new CodeLine()); } } else { lines.Add(LineHelper.Warning("Add logic for foreign key with multiple key")); } } lines.Add(new CodeLine("return query;")); } classDefinition.Methods.Add(new MethodDefinition(string.Format("IQueryable<{0}>", returnType), table.GetGetAllRepositoryMethodName(), parameters.ToArray()) { Lines = lines }); }
public string GetFieldName(string value) => string.Format("m_{0}", NamingConvention.GetCamelCase(ValidName(value)));
public static string GetSqlServerParameterName(this Parameter param) => string.Format("@{0}", NamingConvention.GetCamelCase(param.Name));
private static void GetGetAllMethod(this CSharpClassDefinition definition, ProjectFeature <EntityFrameworkCoreProjectSettings> projectFeature, ProjectSelection <EntityFrameworkCoreProjectSettings> projectSelection, ITable table) { var efCoreProject = projectFeature.GetEntityFrameworkCoreProject(); var returnType = string.Empty; var lines = new List <ILine>(); if (projectSelection.Settings.EntitiesWithDataContracts) { returnType = efCoreProject.GetDataContractName(table); var dataContractPropertiesSets = new[] { new { IsForeign = false, Type = string.Empty, Nullable = false, ObjectSource = string.Empty, PropertySource = string.Empty, Target = string.Empty } }.ToList(); var entityAlias = NamingConvention.GetCamelCase(efCoreProject.GetEntityName(table)); foreach (var column in table.Columns) { var propertyName = column.GetPropertyName(); dataContractPropertiesSets.Add(new { IsForeign = false, column.Type, column.Nullable, ObjectSource = entityAlias, PropertySource = propertyName, Target = propertyName }); } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = projectFeature.Project.Database.FindTable(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyAlias = NamingConvention.GetCamelCase(efCoreProject.GetEntityName(foreignTable)); foreach (var column in foreignTable?.GetColumnsWithNoPrimaryKey()) { if (dataContractPropertiesSets.Where(item => string.Format("{0}.{1}", item.ObjectSource, item.PropertySource) == string.Format("{0}.{1}", entityAlias, column.GetPropertyName())).Count() == 0) { var target = string.Format("{0}{1}", efCoreProject.GetEntityName(foreignTable), column.GetPropertyName()); dataContractPropertiesSets.Add(new { IsForeign = true, column.Type, column.Nullable, ObjectSource = foreignKeyAlias, PropertySource = column.GetPropertyName(), Target = target }); } } } lines.Add(new CommentLine(" Get query from DbSet")); lines.Add(new CodeLine("var query = from {0} in DbContext.Set<{1}>()", entityAlias, efCoreProject.GetEntityName(table))); foreach (var foreignKey in table.ForeignKeys) { var foreignTable = projectFeature.Project.Database.FindTable(foreignKey.References); if (foreignTable == null) { continue; } var foreignKeyEntityName = efCoreProject.GetDbSetPropertyName(foreignTable); var foreignKeyAlias = NamingConvention.GetCamelCase(efCoreProject.GetEntityName(foreignTable)); if (projectFeature.Project.Database.HasDefaultSchema(foreignTable)) { definition.Namespaces.AddUnique(efCoreProject.GetEntityLayerNamespace()); } else { definition.Namespaces.AddUnique(efCoreProject.GetEntityLayerNamespace(foreignTable.Schema)); } if (foreignKey.Key.Count == 0) { lines.Add(new PreprocessorDirectiveLine(1, " There isn't definition for key in foreign key '{0}' in your current database", foreignKey.References)); } else if (foreignKey.Key.Count == 1) { if (foreignTable == null) { lines.Add(LineHelper.Warning(" There isn't definition for '{0}' in your current database", foreignKey.References)); } else { var column = table.Columns.FirstOrDefault(item => item.Name == foreignKey.Key.First()); var x = efCoreProject.CodeNamingConvention.GetPropertyName(foreignKey.Key.First()); var y = efCoreProject.CodeNamingConvention.GetPropertyName(foreignTable.PrimaryKey.Key.First()); if (column.Nullable) { lines.Add(new CodeLine(1, "join {0}Join in DbContext.{1} on {2}.{3} equals {0}Join.{4} into {0}Temp", foreignKeyAlias, foreignKeyEntityName, entityAlias, x, y)); lines.Add(new CodeLine(2, "from {0} in {0}Temp.DefaultIfEmpty()", foreignKeyAlias, entityAlias, x, y)); } else { lines.Add(new CodeLine(1, "join {0} in DbContext.{1} on {2}.{3} equals {0}.{4}", foreignKeyAlias, foreignKeyEntityName, entityAlias, x, y)); } } } else { lines.Add(LineHelper.Warning(" Add logic for foreign key with multiple key")); } } lines.Add(new CodeLine(1, "select new {0}", returnType)); lines.Add(new CodeLine(1, "{")); for (var i = 0; i < dataContractPropertiesSets.Count; i++) { var property = dataContractPropertiesSets[i]; if (string.IsNullOrEmpty(property.ObjectSource) && string.IsNullOrEmpty(property.Target)) { continue; } if (property.IsForeign) { var dbType = projectFeature.Project.Database.ResolveType(property.Type); if (dbType == null) { throw new ObjectRelationMappingException(string.Format("There isn't mapping for '{0}' type", property.Type)); } var clrType = dbType.GetClrType(); if (clrType.FullName == typeof(byte[]).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(byte[]) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(bool).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(bool?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(string).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? string.Empty : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(DateTime).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(DateTime?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(TimeSpan).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(TimeSpan?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(byte).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(byte?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(short).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(short?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(int).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(int?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(long).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(long?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(decimal).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(decimal?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(double).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(double?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(float).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(float?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else if (clrType.FullName == typeof(Guid).FullName) { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(Guid?) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } else { lines.Add(new CodeLine(2, "{0} = {1} == null ? default(object) : {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } else { lines.Add(new CodeLine(2, "{0} = {1}.{2},", property.Target, property.ObjectSource, property.PropertySource)); } } lines.Add(new CodeLine(1, "};")); lines.Add(new CodeLine()); } else { returnType = efCoreProject.GetEntityName(table); lines.Add(new CommentLine(" Get query from DbSet")); lines.Add(new CodeLine("var query = DbContext.{0}.AsQueryable();", efCoreProject.GetDbSetPropertyName(table))); lines.Add(new CodeLine()); } var parameters = new List <ParameterDefinition>(); if (table.ForeignKeys.Count == 0) { lines.Add(new CodeLine("return query;")); } else { for (var i = 0; i < table.ForeignKeys.Count; i++) { var foreignKey = table.ForeignKeys[i]; if (foreignKey.Key.Count == 1) { var column = table.Columns.First(item => item.Name == foreignKey.Key.First()); var parameterName = efCoreProject.GetParameterName(column); parameters.Add(new ParameterDefinition(projectFeature.Project.Database.ResolveDatabaseType(column), parameterName, "null")); if (projectFeature.Project.Database.ColumnIsDateTime(column)) { lines.Add(new CommentLine(" Filter by: '{0}'", column.Name)); lines.Add(new CodeLine("if ({0}.HasValue)", parameterName)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine()); } else if (projectFeature.Project.Database.ColumnIsNumber(column)) { lines.Add(new CommentLine(" Filter by: '{0}'", column.Name)); lines.Add(new CodeLine("if ({0}.HasValue)", parameterName)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine()); } else if (projectFeature.Project.Database.ColumnIsString(column)) { lines.Add(new CommentLine(" Filter by: '{0}'", column.Name)); lines.Add(new CodeLine("if (!string.IsNullOrEmpty({0}))", parameterName)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine()); } else { lines.Add(new CommentLine(" Filter by: '{0}'", column.Name)); lines.Add(new CodeLine("if ({0} != null)", parameterName)); lines.Add(new CodeLine(1, "query = query.Where(item => item.{0} == {1});", column.GetPropertyName(), parameterName)); lines.Add(new CodeLine()); } } else { lines.Add(LineHelper.Warning("Add logic for foreign key with multiple key")); } } lines.Add(new CodeLine("return query;")); } definition.Methods.Add(new MethodDefinition(string.Format("IQueryable<{0}>", returnType), efCoreProject.GetGetAllRepositoryMethodName(table), parameters.ToArray()) { Lines = lines }); }
public string GetParameterName(string value) => NamingConvention.GetCamelCase(ValidName(value));
public static string GetParameterName(this Column column) => string.Format("@{0}", NamingConvention.GetCamelCase(column.Name));
public string GetPropertyName(string value) => NamingConvention.GetCamelCase(ValidName(value));
public string GetMethodName(string value) => NamingConvention.GetCamelCase(ValidName(value));
public string GetConstantName(string value) => string.Format("{0}", NamingConvention.GetCamelCase(ValidName(value)));
/// <summary> /// Gets the name for parameter according to current naming convention /// </summary> /// <param name="name">Name</param> /// <returns>A string as parameter's name</returns> public virtual string GetParameterName(string name) => string.Format("@{0}", NamingConvention.GetCamelCase(name));
private static MethodDefinition GetGetAllMethod(ProjectFeature <DapperProjectSettings> projectFeature, TableFunction tableFunction) { var lines = new List <ILine>(); lines.Add(new CommentLine(" Create connection instance")); lines.Add(new CodeLine("using (var connection = new SqlConnection(ConnectionString))")); lines.Add(new CodeLine("{")); var selection = projectFeature.GetDapperProject().GetSelection(tableFunction); if (selection.Settings.UseStringBuilderForQueries) { lines.Add(new CommentLine(1, " Create string builder for query")); lines.Add(new CodeLine(1, "var query = new StringBuilder();")); lines.Add(new CodeLine()); lines.Add(new CommentLine(1, " Create sql statement")); lines.Add(new CodeLine(1, "query.Append(\" select \");")); for (var i = 0; i < tableFunction.Columns.Count; i++) { var column = tableFunction.Columns[i]; lines.Add(new CodeLine(1, "query.Append(\" {0}{1} \");", tableFunction.GetColumnName(column), i < tableFunction.Columns.Count - 1 ? "," : string.Empty)); } var selectParameters = tableFunction.Parameters.Count == 0 ? string.Empty : string.Join(", ", tableFunction.Parameters.Select(item => item.Name)); lines.Add(new CodeLine(1, "query.Append(\" from \");")); lines.Add(new CodeLine(1, "query.Append(\" {0}({1}) \");", tableFunction.GetFullName(), selectParameters)); lines.Add(new CodeLine()); } else { lines.Add(new CommentLine(1, " Create sql statement")); lines.Add(new CodeLine(1, "var query = @\" select ")); for (var i = 0; i < tableFunction.Columns.Count; i++) { var column = tableFunction.Columns[i]; lines.Add(new CodeLine(1, " {0}{1} ", tableFunction.GetColumnName(column), i < tableFunction.Columns.Count - 1 ? "," : string.Empty)); } var selectParameters = tableFunction.Parameters.Count == 0 ? string.Empty : string.Join(", ", tableFunction.Parameters.Select(item => item.Name)); lines.Add(new CodeLine(1, " from ")); lines.Add(new CodeLine(1, " {0}({1}) ", tableFunction.GetFullName(), selectParameters)); lines.Add(new CodeLine(1, " \";")); lines.Add(new CodeLine()); } if (tableFunction.Parameters.Count > 0) { lines.Add(new CommentLine(1, " Create parameters collection")); lines.Add(new CodeLine(1, "var parameters = new DynamicParameters();")); lines.Add(new CodeLine()); lines.Add(new CommentLine(1, " Add parameters to collection")); foreach (var parameter in tableFunction.Parameters) { lines.Add(new CodeLine(1, "parameters.Add(\"{0}\", {1});", parameter.Name, NamingConvention.GetCamelCase(parameter.Name.Replace("@", "")))); } lines.Add(new CodeLine()); } lines.Add(new CommentLine(1, " Retrieve result from database and convert to typed list")); if (tableFunction.Parameters.Count == 0) { lines.Add(new CodeLine(1, "return await connection.QueryAsync<{0}>(query.ToString());", tableFunction.GetEntityName())); } else { lines.Add(new CodeLine(1, "return await connection.QueryAsync<{0}>(query.ToString(), parameters);", tableFunction.GetEntityName())); } lines.Add(new CodeLine("}")); var parameters = new List <ParameterDefinition>(); foreach (var parameter in tableFunction.Parameters) { parameters.Add(new ParameterDefinition(projectFeature.Project.Database.ResolveType(parameter.Type).ClrAliasType, NamingConvention.GetCamelCase(parameter.Name.Replace("@", "")))); } return(new MethodDefinition(string.Format("Task<IEnumerable<{0}>>", tableFunction.GetEntityName()), tableFunction.GetGetAllRepositoryMethodName(), parameters.ToArray()) { IsAsync = true, Lines = lines }); }
public static string GetParameterName(this string name) => string.Format("@{0}", NamingConvention.GetCamelCase(name));
public string GetFieldName(string value) => ValidName(NamingConvention.GetCamelCase(value));