From ceaa6a5a67a2ad3aa8505e9cf81be76ed0c24c0a Mon Sep 17 00:00:00 2001 From: Paul Welter Date: Sun, 25 Feb 2024 20:39:05 -0600 Subject: [PATCH] add attribute support to templates --- .../Tracker.Core/Data/Mapping/StatusMap.cs | 2 +- .../Tracker.Core/Data/Mapping/UserMap.cs | 2 +- .../Data/Queries/AuditExtensions.cs | 1 + .../Data/Queries/PriorityExtensions.cs | 1 + .../Data/Queries/RoleExtensions.cs | 2 + .../Data/Queries/StatusExtensions.cs | 1 + .../Data/Queries/TaskExtendedExtensions.cs | 1 + .../Data/Queries/TaskExtensions.cs | 1 + .../Data/Queries/TenantExtensions.cs | 1 + .../Data/Queries/UserExtensions.cs | 2 + .../Data/Queries/UserLoginExtensions.cs | 1 + .../Data/Queries/UserRoleExtensions.cs | 1 + .../Validation/AuditCreateModelValidator.cs | 3 +- .../Validation/AuditUpdateModelValidator.cs | 2 +- .../Tracker/Tracker.Core/Tracker.Core.csproj | 3 +- sample/Tracker/Tracker.Core/generation.yml | 1 + .../EntityFrameworkCore.Generator.Core.csproj | 2 +- .../Metadata/Generation/Model.cs | 7 +- .../ModelGenerator.cs | 5 +- .../OptionMapper.cs | 92 ++++++++----------- .../Options/ClassOptionsBase.cs | 36 ++++++++ .../Options/ContextClassOptions.cs | 28 +----- .../Options/MapperClassOptions.cs | 28 +----- .../Options/MappingClassOptions.cs | 1 + .../Options/ModelOptionsBase.cs | 25 ----- .../Options/QueryExtensionOptions.cs | 5 +- .../Options/ValidatorClassOptions.cs | 28 +----- .../Serialization/ClassBase.cs | 24 +++++ .../Serialization/ContextClass.cs | 10 +- .../Serialization/MapperClass.cs | 16 ---- .../Serialization/MappingClass.cs | 1 + .../Serialization/ModelBase.cs | 16 ---- .../Serialization/QueryExtension.cs | 1 + .../Serialization/ValidatorClass.cs | 16 ---- .../Templates/MapperClassTemplate.cs | 5 +- .../Templates/ModelClassTemplate.cs | 5 +- .../Templates/ValidatorClassTemplate.cs | 5 +- .../EntityFrameworkCore.Generator.csproj | 2 +- .../InitializeCommand.cs | 2 +- ...yFrameworkCore.Generator.Core.Tests.csproj | 6 +- .../OptionsTests.cs | 5 +- 41 files changed, 156 insertions(+), 240 deletions(-) diff --git a/sample/Tracker/Tracker.Core/Data/Mapping/StatusMap.cs b/sample/Tracker/Tracker.Core/Data/Mapping/StatusMap.cs index ddb48bbf..c2866578 100644 --- a/sample/Tracker/Tracker.Core/Data/Mapping/StatusMap.cs +++ b/sample/Tracker/Tracker.Core/Data/Mapping/StatusMap.cs @@ -45,7 +45,7 @@ public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityType .IsRequired() .HasColumnName("DisplayOrder") .HasColumnType("int") - .HasDefaultValueSql("((0))"); + .HasDefaultValue(0); builder.Property(t => t.IsActive) .IsRequired() diff --git a/sample/Tracker/Tracker.Core/Data/Mapping/UserMap.cs b/sample/Tracker/Tracker.Core/Data/Mapping/UserMap.cs index 1c9b8570..8f98d86d 100644 --- a/sample/Tracker/Tracker.Core/Data/Mapping/UserMap.cs +++ b/sample/Tracker/Tracker.Core/Data/Mapping/UserMap.cs @@ -64,7 +64,7 @@ public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityType .IsRequired() .HasColumnName("AccessFailedCount") .HasColumnType("int") - .HasDefaultValueSql("((0))"); + .HasDefaultValue(0); builder.Property(t => t.LockoutEnabled) .IsRequired() diff --git a/sample/Tracker/Tracker.Core/Data/Queries/AuditExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/AuditExtensions.cs index 0884218a..6c50abaa 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/AuditExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/AuditExtensions.cs @@ -34,6 +34,7 @@ public static partial class AuditExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/PriorityExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/PriorityExtensions.cs index 7d62688b..a719675c 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/PriorityExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/PriorityExtensions.cs @@ -34,6 +34,7 @@ public static partial class PriorityExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/RoleExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/RoleExtensions.cs index 66da7615..2270c843 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/RoleExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/RoleExtensions.cs @@ -34,6 +34,7 @@ public static partial class RoleExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { @@ -65,6 +66,7 @@ public static partial class RoleExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.Task GetByNameAsync(this System.Linq.IQueryable queryable, string name, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/StatusExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/StatusExtensions.cs index cf0748c0..263089ed 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/StatusExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/StatusExtensions.cs @@ -34,6 +34,7 @@ public static partial class StatusExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/TaskExtendedExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/TaskExtendedExtensions.cs index 7dbf19cf..0f236de1 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/TaskExtendedExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/TaskExtendedExtensions.cs @@ -34,6 +34,7 @@ public static partial class TaskExtendedExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid taskId, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/TaskExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/TaskExtensions.cs index 97cd8606..d80ad829 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/TaskExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/TaskExtensions.cs @@ -48,6 +48,7 @@ public static partial class TaskExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/TenantExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/TenantExtensions.cs index ed24d5d3..c7ab4711 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/TenantExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/TenantExtensions.cs @@ -34,6 +34,7 @@ public static partial class TenantExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/UserExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/UserExtensions.cs index da61d5de..1839677a 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/UserExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/UserExtensions.cs @@ -31,6 +31,7 @@ public static partial class UserExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.Task GetByEmailAddressAsync(this System.Linq.IQueryable queryable, string emailAddress, System.Threading.CancellationToken cancellationToken = default) { @@ -62,6 +63,7 @@ public static partial class UserExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/UserLoginExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/UserLoginExtensions.cs index 09ac89b8..7a843dab 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/UserLoginExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/UserLoginExtensions.cs @@ -48,6 +48,7 @@ public static partial class UserLoginExtensions /// /// An to filter. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid id, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Data/Queries/UserRoleExtensions.cs b/sample/Tracker/Tracker.Core/Data/Queries/UserRoleExtensions.cs index 2cac61b9..4787a2ad 100644 --- a/sample/Tracker/Tracker.Core/Data/Queries/UserRoleExtensions.cs +++ b/sample/Tracker/Tracker.Core/Data/Queries/UserRoleExtensions.cs @@ -64,6 +64,7 @@ public static partial class UserRoleExtensions /// An to filter. /// The value to filter by. /// The value to filter by. + /// A to observe while waiting for the task to complete. /// An instance of or null if not found. public static async System.Threading.Tasks.ValueTask GetByKeyAsync(this System.Linq.IQueryable queryable, Guid userId, Guid roleId, System.Threading.CancellationToken cancellationToken = default) { diff --git a/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditCreateModelValidator.cs b/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditCreateModelValidator.cs index 00fde6ea..9a719eeb 100644 --- a/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditCreateModelValidator.cs +++ b/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditCreateModelValidator.cs @@ -1,13 +1,14 @@ using System; using FluentValidation; + using Tracker.Core.Domain.Models; -// ReSharper disable once CheckNamespace namespace Tracker.Core.Domain.Validation; /// /// Validator class for . /// +[RegisterSingleton>] public partial class AuditCreateModelValidator : AbstractValidator { diff --git a/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditUpdateModelValidator.cs b/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditUpdateModelValidator.cs index b2443969..fe87f22d 100644 --- a/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditUpdateModelValidator.cs +++ b/sample/Tracker/Tracker.Core/Domain/Audit/Validation/AuditUpdateModelValidator.cs @@ -2,12 +2,12 @@ using FluentValidation; using Tracker.Core.Domain.Models; -// ReSharper disable once CheckNamespace namespace Tracker.Core.Domain.Validation; /// /// Validator class for . /// +[RegisterSingleton>] public partial class AuditUpdateModelValidator : AbstractValidator { diff --git a/sample/Tracker/Tracker.Core/Tracker.Core.csproj b/sample/Tracker/Tracker.Core/Tracker.Core.csproj index f85be1c2..4d75eac8 100644 --- a/sample/Tracker/Tracker.Core/Tracker.Core.csproj +++ b/sample/Tracker/Tracker.Core/Tracker.Core.csproj @@ -9,11 +9,12 @@ + - + diff --git a/sample/Tracker/Tracker.Core/generation.yml b/sample/Tracker/Tracker.Core/generation.yml index 97ec516e..82f0bd98 100644 --- a/sample/Tracker/Tracker.Core/generation.yml +++ b/sample/Tracker/Tracker.Core/generation.yml @@ -79,4 +79,5 @@ model: baseClass: 'AbstractValidator<{Model.Name}>' namespace: '{Project.Namespace}.Domain.Validation' directory: '{Project.Directory}\Domain\{Entity.Name}\Validation' + attributes: '[RegisterSingleton>]' document: true diff --git a/src/EntityFrameworkCore.Generator.Core/EntityFrameworkCore.Generator.Core.csproj b/src/EntityFrameworkCore.Generator.Core/EntityFrameworkCore.Generator.Core.csproj index 5325d7e3..ff5dafee 100644 --- a/src/EntityFrameworkCore.Generator.Core/EntityFrameworkCore.Generator.Core.csproj +++ b/src/EntityFrameworkCore.Generator.Core/EntityFrameworkCore.Generator.Core.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/EntityFrameworkCore.Generator.Core/Metadata/Generation/Model.cs b/src/EntityFrameworkCore.Generator.Core/Metadata/Generation/Model.cs index 595f9af7..69fd45d4 100644 --- a/src/EntityFrameworkCore.Generator.Core/Metadata/Generation/Model.cs +++ b/src/EntityFrameworkCore.Generator.Core/Metadata/Generation/Model.cs @@ -1,4 +1,4 @@ -namespace EntityFrameworkCore.Generator.Metadata.Generation; +namespace EntityFrameworkCore.Generator.Metadata.Generation; public class Model : ModelBase, IOptionVariable { @@ -17,6 +17,8 @@ public Model() public string ModelBaseClass { get; set; } + public string ModelAttributes { get; set; } + public string ValidatorNamespace { get; set; } @@ -27,7 +29,6 @@ public Model() public PropertyCollection Properties { get; set; } - void IOptionVariable.Set(VariableDictionary variableDictionary) { variableDictionary.Set(VariableConstants.ModelName, ModelClass); @@ -37,4 +38,4 @@ void IOptionVariable.Remove(VariableDictionary variableDictionary) { variableDictionary.Remove(VariableConstants.ModelName); } -} \ No newline at end of file +} diff --git a/src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs b/src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs index bb435262..b84416e9 100644 --- a/src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs +++ b/src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs @@ -16,8 +16,6 @@ using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.Logging; -using static Npgsql.Replication.PgOutput.Messages.RelationMessage; - using Model = EntityFrameworkCore.Generator.Metadata.Generation.Model; using Property = EntityFrameworkCore.Generator.Metadata.Generation.Property; using PropertyCollection = EntityFrameworkCore.Generator.Metadata.Generation.PropertyCollection; @@ -537,7 +535,8 @@ private void CreateModel(Entity entity, TOption options, ModelType mode ModelType = modelType, ModelBaseClass = options.BaseClass, ModelNamespace = modelNamespace, - ModelClass = modelClass + ModelClass = modelClass, + ModelAttributes = options.Attributes, }; foreach (var property in entity.Properties) diff --git a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs index f2c0587c..ad837307 100644 --- a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs +++ b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs @@ -1,9 +1,14 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using EntityFrameworkCore.Generator.Options; using EntityFrameworkCore.Generator.Serialization; +using Microsoft.CodeAnalysis.Options; + +using static Microsoft.EntityFrameworkCore.DbLoggerCategory; + namespace EntityFrameworkCore.Generator; public static class OptionMapper @@ -58,68 +63,54 @@ private static void MapModel(ModelOptions option, ViewModel model) MapValidator(option.Validator, model.Validator); } + private static void MapClassBase(ClassOptionsBase option, ClassBase classBase) + { + option.Namespace = classBase.Namespace; + option.Directory = classBase.Directory; + option.Document = classBase.Document; + option.Name = classBase.Name; + option.BaseClass = classBase.BaseClass; + option.Attributes = classBase.Attributes; + } + + private static void MapModelBase(ModelOptionsBase option, ModelBase modelBase) + { + MapClassBase(option, modelBase); + + option.Generate = modelBase.Generate; + + MapSelection(option.Include, modelBase.Include); + MapSelection(option.Exclude, modelBase.Exclude); + + } + private static void MapValidator(ValidatorClassOptions option, ValidatorClass validator) { - option.Namespace = validator.Namespace; - option.Directory = validator.Directory; - option.Document = validator.Document; + MapClassBase(option, validator); option.Generate = validator.Generate; - option.Name = validator.Name; - option.BaseClass = validator.BaseClass; } private static void MapMapper(MapperClassOptions option, MapperClass mapper) { - option.Namespace = mapper.Namespace; - option.Directory = mapper.Directory; - option.Document = mapper.Document; + MapClassBase(option, mapper); option.Generate = mapper.Generate; - option.Name = mapper.Name; - option.BaseClass = mapper.BaseClass; } private static void MapUpdate(UpdateModelOptions option, UpdateModel update) { - option.Namespace = update.Namespace; - option.Directory = update.Directory; - option.Document = update.Document; - - option.Generate = update.Generate; - option.Name = update.Name; - option.BaseClass = update.BaseClass; - - MapSelection(option.Include, update.Include); - MapSelection(option.Exclude, update.Exclude); + MapModelBase(option, update); } private static void MapCreate(CreateModelOptions option, CreateModel create) { - option.Namespace = create.Namespace; - option.Directory = create.Directory; - option.Document = create.Document; - - option.Generate = create.Generate; - option.Name = create.Name; - option.BaseClass = create.BaseClass; - - MapSelection(option.Include, create.Include); - MapSelection(option.Exclude, create.Exclude); + MapModelBase(option, create); } private static void MapRead(ReadModelOptions option, ReadModel read) { - option.Namespace = read.Namespace; - option.Directory = read.Directory; - option.Document = read.Document; - - option.Generate = read.Generate; - option.Name = read.Name; - option.BaseClass = read.BaseClass; - - MapSelection(option.Include, read.Include); - MapSelection(option.Exclude, read.Exclude); + MapModelBase(option, read); } private static void MapShared(SharedModelOptions option, SharedModel shared) @@ -141,9 +132,7 @@ private static void MapData(DataOptions option, DataModel data) private static void MapQuery(QueryExtensionOptions option, QueryExtension query) { - option.Namespace = query.Namespace; - option.Directory = query.Directory; - option.Document = query.Document; + MapClassBase(option, query); option.Generate = query.Generate; option.IndexPrefix = query.IndexPrefix; @@ -152,21 +141,16 @@ private static void MapQuery(QueryExtensionOptions option, QueryExtension query) private static void MapMapping(MappingClassOptions option, MappingClass mapping) { - option.Namespace = mapping.Namespace; - option.Directory = mapping.Directory; - option.Document = mapping.Document; + MapClassBase(option, mapping); + option.Temporal = mapping.Temporal; option.RowVersion = mapping.RowVersion; } private static void MapEntity(EntityClassOptions option, EntityClass entity) { - option.Namespace = entity.Namespace; - option.Directory = entity.Directory; - option.Document = entity.Document; + MapClassBase(option, entity); - option.Name = entity.Name; - option.BaseClass = entity.BaseClass; option.EntityNaming = entity.EntityNaming; option.RelationshipNaming = entity.RelationshipNaming; option.PrefixWithSchemaName = entity.PrefixWithSchemaName; @@ -194,12 +178,8 @@ private static void MapSelection(SelectionOptions option, SelectionModel selecti private static void MapContext(ContextClassOptions option, ContextClass context) { - option.Namespace = context.Namespace; - option.Directory = context.Directory; - option.Document = context.Document; + MapClassBase(option, context); - option.Name = context.Name; - option.BaseClass = context.BaseClass; option.PropertyNaming = context.PropertyNaming; } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/ClassOptionsBase.cs b/src/EntityFrameworkCore.Generator.Core/Options/ClassOptionsBase.cs index 2c34ba57..6810b594 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/ClassOptionsBase.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/ClassOptionsBase.cs @@ -51,4 +51,40 @@ public string Directory /// [DefaultValue(false)] public bool Document { get; set; } + + /// + /// Gets or sets the class name template. + /// + /// + /// The class name template. + /// + public string Name + { + get => GetProperty(); + set => SetProperty(value); + } + + /// + /// Gets or sets the base class to inherit from. + /// + /// + /// The base class. + /// + public string BaseClass + { + get => GetProperty(); + set => SetProperty(value); + } + + /// + /// Gets or sets the attributes to add to the class + /// + /// + /// The attributes to add to the class + /// + public string Attributes + { + get => GetProperty(); + set => SetProperty(value); + } } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/ContextClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/ContextClassOptions.cs index 3b5e7627..03e52185 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/ContextClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/ContextClassOptions.cs @@ -1,4 +1,4 @@ -using System.ComponentModel; +using System.ComponentModel; using Microsoft.EntityFrameworkCore; @@ -24,30 +24,6 @@ public ContextClassOptions(VariableDictionary variables, string prefix) PropertyNaming = ContextNaming.Plural; } - /// - /// Gets or sets the class name for the generated file. - /// - /// - /// The class name for the file. - /// - public string Name - { - get => GetProperty(); - set => SetProperty(value); - } - - /// - /// Gets or sets the base class to inherit from. Default is . - /// - /// - /// The base class. - /// - public string BaseClass - { - get => GetProperty(); - set => SetProperty(value); - } - /// /// Gets or sets the property naming strategy for entity data set property. /// @@ -56,4 +32,4 @@ public string BaseClass /// [DefaultValue(ContextNaming.Plural)] public ContextNaming PropertyNaming { get; set; } -} \ No newline at end of file +} diff --git a/src/EntityFrameworkCore.Generator.Core/Options/MapperClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/MapperClassOptions.cs index cbe10871..b4b450d2 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/MapperClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/MapperClassOptions.cs @@ -1,4 +1,4 @@ -namespace EntityFrameworkCore.Generator.Options; +namespace EntityFrameworkCore.Generator.Options; /// /// View model mapper class options @@ -27,28 +27,4 @@ public MapperClassOptions(VariableDictionary variables, string prefix) /// true to generate; otherwise, false. /// public bool Generate { get; set; } - - /// - /// Gets or sets the mapper class name template. - /// - /// - /// The mapper class name template. - /// - public string Name - { - get => GetProperty(); - set => SetProperty(value); - } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass - { - get => GetProperty(); - set => SetProperty(value); - } -} \ No newline at end of file +} diff --git a/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs index ae47b405..c5ad6fc1 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs @@ -16,6 +16,7 @@ public MappingClassOptions(VariableDictionary variables, string prefix) { Namespace = "{Project.Namespace}.Data.Mapping"; Directory = @"{Project.Directory}\Data\Mapping"; + Name = "{Entity.Name}Map"; } /// diff --git a/src/EntityFrameworkCore.Generator.Core/Options/ModelOptionsBase.cs b/src/EntityFrameworkCore.Generator.Core/Options/ModelOptionsBase.cs index 476dad70..fbf726fd 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/ModelOptionsBase.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/ModelOptionsBase.cs @@ -30,31 +30,6 @@ protected ModelOptionsBase(VariableDictionary variables, string prefix) /// public bool Generate { get; set; } - /// - /// Gets or sets the class name template. - /// - /// - /// The class name template. - /// - public string Name - { - get => GetProperty(); - set => SetProperty(value); - } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass - { - get => GetProperty(); - set => SetProperty(value); - } - - /// /// Gets or sets the include selection options. /// diff --git a/src/EntityFrameworkCore.Generator.Core/Options/QueryExtensionOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/QueryExtensionOptions.cs index df4eddb5..005f9ff7 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/QueryExtensionOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/QueryExtensionOptions.cs @@ -1,4 +1,4 @@ -namespace EntityFrameworkCore.Generator.Options; +namespace EntityFrameworkCore.Generator.Options; /// /// Query extensions options @@ -14,6 +14,7 @@ public QueryExtensionOptions(VariableDictionary variables, string prefix) { Namespace = "{Project.Namespace}.Data.Queries"; Directory = @"{Project.Directory}\Data\Queries"; + Name = "{Entity.Name}Extensions"; Generate = false; IndexPrefix = "By"; @@ -51,4 +52,4 @@ public string UniquePrefix get => GetProperty(); set => SetProperty(value); } -} \ No newline at end of file +} diff --git a/src/EntityFrameworkCore.Generator.Core/Options/ValidatorClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/ValidatorClassOptions.cs index f03981a0..6f22ab08 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/ValidatorClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/ValidatorClassOptions.cs @@ -1,4 +1,4 @@ -namespace EntityFrameworkCore.Generator.Options; +namespace EntityFrameworkCore.Generator.Options; /// /// Validator class options @@ -26,28 +26,4 @@ public ValidatorClassOptions(VariableDictionary variables, string prefix) /// true to generate; otherwise, false. /// public bool Generate { get; set; } - - /// - /// Gets or sets the validator class name template. - /// - /// - /// The validator class name template. - /// - public string Name - { - get => GetProperty(); - set => SetProperty(value); - } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass - { - get => GetProperty(); - set => SetProperty(value); - } -} \ No newline at end of file +} diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/ClassBase.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/ClassBase.cs index a8372c50..e63dc8ae 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/ClassBase.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/ClassBase.cs @@ -31,4 +31,28 @@ public abstract class ClassBase /// [DefaultValue(false)] public bool Document { get; set; } + + /// + /// Gets or sets the class name template. + /// + /// + /// The class name template. + /// + public string Name { get; set; } + + /// + /// Gets or sets the base class to inherit from. + /// + /// + /// The base class. + /// + public string BaseClass { get; set; } + + /// + /// Gets or sets the attributes to add to the class + /// + /// + /// The attributes to add to the class + /// + public string Attributes { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/ContextClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/ContextClass.cs index 64e5a41b..eb43e549 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/ContextClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/ContextClass.cs @@ -25,14 +25,6 @@ public ContextClass() PropertyNaming = ContextNaming.Plural; } - /// - /// Gets or sets the class name for the generated file. - /// - /// - /// The class name for the file. - /// - public string Name { get; set; } - /// /// Gets or sets the base class to inherit from. Default is . /// @@ -40,7 +32,7 @@ public ContextClass() /// The base class. /// [DefaultValue("DbContext")] - public string BaseClass { get; set; } + public new string BaseClass { get; set; } /// /// Gets or sets the property naming strategy for entity data set property. diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/MapperClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/MapperClass.cs index 76585eda..0d762a15 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/MapperClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/MapperClass.cs @@ -25,20 +25,4 @@ public MapperClass() /// true to generate; otherwise, false. /// public bool Generate { get; set; } - - /// - /// Gets or sets the mapper class name template. - /// - /// - /// The mapper class name template. - /// - public string Name { get; set; } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs index 6665ee6b..b5c4706a 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs @@ -16,6 +16,7 @@ public MappingClass() { Namespace = "{Project.Namespace}.Data.Mapping"; Directory = @"{Project.Directory}\Data\Mapping"; + Name = "{Entity.Name}Map"; } /// diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/ModelBase.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/ModelBase.cs index c7682d2e..3ae41b74 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/ModelBase.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/ModelBase.cs @@ -25,22 +25,6 @@ protected ModelBase() /// public bool Generate { get; set; } - /// - /// Gets or sets the class name template. - /// - /// - /// The class name template. - /// - public string Name { get; set; } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass { get; set; } - /// /// Gets or sets the include selection options. /// diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/QueryExtension.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/QueryExtension.cs index 10992471..c091810b 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/QueryExtension.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/QueryExtension.cs @@ -12,6 +12,7 @@ public QueryExtension() { Namespace = "{Project.Namespace}.Data.Queries"; Directory = @"{Project.Directory}\Data\Queries"; + Name = "{Entity.Name}Extensions"; Generate = false; IndexPrefix = "By"; diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/ValidatorClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/ValidatorClass.cs index cd549add..5ea40a79 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/ValidatorClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/ValidatorClass.cs @@ -24,20 +24,4 @@ public ValidatorClass() /// true to generate; otherwise, false. /// public bool Generate { get; set; } - - /// - /// Gets or sets the validator class name template. - /// - /// - /// The validator class name template. - /// - public string Name { get; set; } - - /// - /// Gets or sets the base class to inherit from. - /// - /// - /// The base class. - /// - public string BaseClass { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/MapperClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/MapperClassTemplate.cs index e274848c..e44433ba 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/MapperClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/MapperClassTemplate.cs @@ -69,7 +69,10 @@ private void GenerateClass() CodeBuilder.AppendLine($"/// Mapper class for entity ."); CodeBuilder.AppendLine("/// "); } - + if (Options.Model.Mapper.Attributes.HasValue()) + { + CodeBuilder.AppendLine(Options.Model.Mapper.Attributes); + } CodeBuilder.AppendLine($"public partial class {mapperClass}"); if (_entity.MapperBaseClass.HasValue()) diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs index cfd84468..2091fbde 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs @@ -56,7 +56,10 @@ private void GenerateClass() CodeBuilder.AppendLine("/// View Model class"); CodeBuilder.AppendLine("/// "); } - + if (_model.ModelAttributes.HasValue()) + { + CodeBuilder.AppendLine(_model.ModelAttributes); + } CodeBuilder.AppendLine($"public partial class {modelClass}"); if (_model.ModelBaseClass.HasValue()) diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs index 2c0aa813..218595d9 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs @@ -60,7 +60,10 @@ private void GenerateClass() CodeBuilder.AppendLine($"/// Validator class for ."); CodeBuilder.AppendLine("/// "); } - + if (Options.Model.Validator.Attributes.HasValue()) + { + CodeBuilder.AppendLine(Options.Model.Validator.Attributes); + } CodeBuilder.AppendLine($"public partial class {validatorClass}"); if (_model.ValidatorBaseClass.HasValue()) diff --git a/src/EntityFrameworkCore.Generator/EntityFrameworkCore.Generator.csproj b/src/EntityFrameworkCore.Generator/EntityFrameworkCore.Generator.csproj index d53549cc..337985c5 100644 --- a/src/EntityFrameworkCore.Generator/EntityFrameworkCore.Generator.csproj +++ b/src/EntityFrameworkCore.Generator/EntityFrameworkCore.Generator.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/EntityFrameworkCore.Generator/InitializeCommand.cs b/src/EntityFrameworkCore.Generator/InitializeCommand.cs index 9f359ac2..4546a687 100644 --- a/src/EntityFrameworkCore.Generator/InitializeCommand.cs +++ b/src/EntityFrameworkCore.Generator/InitializeCommand.cs @@ -108,7 +108,7 @@ private Serialization.GeneratorModel CreateOptionsFile(string optionsFile) options.Model.Update.Generate = true; options.Model.Validator.Generate = true; options.Model.Mapper.Generate = true; - + Logger.LogInformation($"Creating options file: {optionsFile}"); return options; diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/EntityFrameworkCore.Generator.Core.Tests.csproj b/test/EntityFrameworkCore.Generator.Core.Tests/EntityFrameworkCore.Generator.Core.Tests.csproj index 7be16962..24ed62cc 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/EntityFrameworkCore.Generator.Core.Tests.csproj +++ b/test/EntityFrameworkCore.Generator.Core.Tests/EntityFrameworkCore.Generator.Core.Tests.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -27,8 +27,8 @@ - - + + all runtime; build; native; contentfiles; analyzers diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/OptionsTests.cs b/test/EntityFrameworkCore.Generator.Core.Tests/OptionsTests.cs index b24efdd9..b0702c1b 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/OptionsTests.cs +++ b/test/EntityFrameworkCore.Generator.Core.Tests/OptionsTests.cs @@ -3,6 +3,7 @@ using System.Reflection; using EntityFrameworkCore.Generator.Options; +using EntityFrameworkCore.Generator.Serialization; using FluentAssertions; @@ -28,7 +29,7 @@ public OptionsTests(ITestOutputHelper output) [Fact] public void SaveDefault() { - var generatorOptions = new GeneratorOptions(); + var generatorOptions = new GeneratorModel(); // set user secret values generatorOptions.Database.UserSecretsId = Guid.NewGuid().ToString(); generatorOptions.Database.ConnectionName = "ConnectionStrings:Generator"; @@ -42,10 +43,10 @@ public void SaveDefault() generatorOptions.Model.Mapper.Generate = true; var serializer = new SerializerBuilder() + .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults) .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); - var yaml = serializer.Serialize(generatorOptions);