public void ScaffoldingProductViewModelClass() { // Arrange var definition = new CSharpClassDefinition { Namespaces = { "System", "System.ComponentModel" }, Namespace = "DesignPatterns", AccessModifier = AccessModifier.Public, IsPartial = true, Name = "Product", Implements = { "INotifyPropertyChanged" }, Events = { new EventDefinition("PropertyChangedEventHandler", "PropertyChanged") { AccessModifier = AccessModifier.Public } } }; definition.AddViewModelProperty("int?", "ProductID"); definition.AddViewModelProperty("string", "ProductName"); definition.AddViewModelProperty("int?", "SupplierID"); definition.AddViewModelProperty("int?", "CategoryID"); definition.AddViewModelProperty("string", "QuantityPerUnit"); definition.AddViewModelProperty("decimal?", "UnitPrice"); definition.AddViewModelProperty("short?", "UnitsInStock"); definition.AddViewModelProperty("short?", "UnitsOnOrder"); definition.AddViewModelProperty("short?", "ReorderLevel"); definition.AddViewModelProperty("bool?", "Discontinued"); // Act CSharpCodeBuilder.CreateFiles(@"C:\Temp\CatFactory.NetCore\DesignPatterns", string.Empty, true, definition); }
public void TestCsharpViewModelClassWithExtensionMethodsGeneration() { // Arrange var definition = new CSharpClassDefinition(); definition.Namespaces.Add("System"); definition.Namespaces.Add("System.ComponentModel"); definition.Namespace = "ViewModels"; definition.Name = "ProductViewModel"; definition.Implements.Add("INotifyPropertyChanged"); definition.Events.Add(new EventDefinition("PropertyChangedEventHandler", "PropertyChanged")); definition.AddViewModelProperty("Int32?", "ProductId"); definition.AddViewModelProperty("String", "ProductName"); definition.AddViewModelProperty("Int32?", "ProductCategoryID"); definition.AddViewModelProperty("Decimal?", "UnitPrice"); definition.AddViewModelProperty("String", "ProductDescription"); // Act CSharpCodeBuilder.CreateFiles("C:\\Temp\\CatFactory.DotNetCore", string.Empty, true, definition); }
public static CSharpClassDefinition GetEntityClassDefinition(this EntityFrameworkCoreProject project, ITable table) { var classDefinition = new CSharpClassDefinition(); classDefinition.Namespaces.Add("System"); if (project.Settings.UseDataAnnotations) { classDefinition.Namespaces.Add("System.ComponentModel.DataAnnotations"); classDefinition.Namespaces.Add("System.ComponentModel.DataAnnotations.Schema"); } if (project.Settings.EnableDataBindings) { classDefinition.Namespaces.Add("System.ComponentModel"); classDefinition.Implements.Add("INotifyPropertyChanged"); classDefinition.Events.Add(new EventDefinition("PropertyChangedEventHandler", "PropertyChanged")); } classDefinition.Namespace = table.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema); classDefinition.Name = table.GetSingularName(); classDefinition.IsPartial = true; classDefinition.Constructors.Add(new ClassConstructorDefinition()); var columns = table.Columns; if (table.PrimaryKey?.Key.Count == 1) { var column = table.PrimaryKey.GetColumns(table).First(); classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(column.GetClrType(), column.GetParameterName())) { Lines = new List <ILine> { new CodeLine("{0} = {1};", column.GetPropertyName(), column.GetParameterName()) } }); } if (!string.IsNullOrEmpty(table.Description)) { classDefinition.Documentation.Summary = table.Description; } foreach (var column in columns) { if (project.Settings.EnableDataBindings) { classDefinition.AddViewModelProperty(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName()); } else { if (project.Settings.BackingFields.Contains(table.GetFullColumnName(column))) { classDefinition.AddPropertyWithField(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName()); } else if (project.Settings.UseAutomaticPropertiesForEntities) { classDefinition.Properties.Add(new PropertyDefinition(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName())); } else { classDefinition.AddPropertyWithField(column.GetClrType(), column.HasSameNameEnclosingType(table) ? column.GetNameForEnclosing() : column.GetPropertyName()); } } } if (project.Settings.AuditEntity == null) { classDefinition.Implements.Add("IEntity"); } else { var count = 0; foreach (var column in columns) { if (project.Settings.AuditEntity.Names.Contains(column.Name)) { count += 1; } } if (count == project.Settings.AuditEntity.Names.Length) { classDefinition.Implements.Add(project.Settings.EntityInterfaceName); } else { classDefinition.Implements.Add("IEntity"); } } foreach (var foreignKey in table.ForeignKeys) { var foreignTable = project.Database.FindTableByFullName(foreignKey.References); if (foreignTable == null) { continue; } classDefinition.Namespaces.AddUnique(foreignTable.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(foreignTable.Schema)); classDefinition.Namespace = table.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema); classDefinition.Properties.Add(foreignKey.GetParentNavigationProperty(project, foreignTable)); } foreach (var child in project.Database.Tables) { if (table.FullName == child.FullName) { continue; } foreach (var foreignKey in child.ForeignKeys) { if (foreignKey.References.EndsWith(table.FullName)) { classDefinition.Namespaces.AddUnique(project.Settings.NavigationPropertyEnumerableNamespace); classDefinition.Namespaces.AddUnique(child.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(child.Schema)); var navigationProperty = project.GetChildNavigationProperty(child, foreignKey); if (classDefinition.Properties.FirstOrDefault(item => item.Name == navigationProperty.Name) == null) { classDefinition.Properties.Add(navigationProperty); } } } } if (project.Settings.SimplifyDataTypes) { classDefinition.SimplifyDataTypes(); } return(classDefinition); }
public static CSharpClassDefinition CreateEntity(this DapperProject project, ITable table) { var classDefinition = new CSharpClassDefinition(); classDefinition.Namespaces.Add("System"); var selection = project.GetSelection(table); if (selection.Settings.EnableDataBindings) { classDefinition.Namespaces.Add("System.ComponentModel"); classDefinition.Implements.Add("INotifyPropertyChanged"); classDefinition.Events.Add(new EventDefinition("PropertyChangedEventHandler", "PropertyChanged")); } classDefinition.Namespace = table.HasDefaultSchema() ? project.GetEntityLayerNamespace() : project.GetEntityLayerNamespace(table.Schema); classDefinition.Name = table.GetEntityName(); classDefinition.Constructors.Add(new ClassConstructorDefinition()); if (table.PrimaryKey != null && table.PrimaryKey.Key.Count == 1) { var column = table.GetColumnsFromConstraint(table.PrimaryKey).First(); classDefinition.Constructors.Add(new ClassConstructorDefinition(new ParameterDefinition(project.Database.ResolveType(column), column.GetParameterName())) { Lines = new List <ILine>() { new CodeLine("{0} = {1};", column.GetPropertyName(), column.GetParameterName()) } }); } if (!string.IsNullOrEmpty(table.Description)) { classDefinition.Documentation.Summary = table.Description; } foreach (var column in table.Columns) { if (selection.Settings.EnableDataBindings) { classDefinition.AddViewModelProperty(project.Database.ResolveType(column), column.GetPropertyName()); } else { if (selection.Settings.UseAutomaticPropertiesForEntities) { classDefinition.Properties.Add(new PropertyDefinition(project.Database.ResolveType(column), column.GetPropertyName())); } else { classDefinition.AddPropertyWithField(project.Database.ResolveType(column), column.GetPropertyName()); } } } classDefinition.Implements.Add("IEntity"); if (selection.Settings.SimplifyDataTypes) { classDefinition.SimplifyDataTypes(); } return(classDefinition); }