Skip to content

Commit

Permalink
add attribute support to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Feb 26, 2024
1 parent 303c09f commit ceaa6a5
Show file tree
Hide file tree
Showing 41 changed files with 156 additions and 240 deletions.
2 changes: 1 addition & 1 deletion sample/Tracker/Tracker.Core/Data/Mapping/StatusMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion sample/Tracker/Tracker.Core/Data/Mapping/UserMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class AuditExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Audit"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Audit?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Audit> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class PriorityExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Priority"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Priority?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Priority> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
2 changes: 2 additions & 0 deletions sample/Tracker/Tracker.Core/Data/Queries/RoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class RoleExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Role"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Role?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Role> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -65,6 +66,7 @@ public static partial class RoleExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="name">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Role"/> or null if not found.</returns>
public static async System.Threading.Tasks.Task<Tracker.Core.Data.Entities.Role?> GetByNameAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Role> queryable, string name, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class StatusExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Status"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Status?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Status> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class TaskExtendedExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="taskId">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.TaskExtended"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.TaskExtended?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.TaskExtended> queryable, Guid taskId, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
1 change: 1 addition & 0 deletions sample/Tracker/Tracker.Core/Data/Queries/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static partial class TaskExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Task"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Task?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Task> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static partial class TenantExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.Tenant"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.Tenant?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.Tenant> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
2 changes: 2 additions & 0 deletions sample/Tracker/Tracker.Core/Data/Queries/UserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static partial class UserExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="emailAddress">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.User"/> or null if not found.</returns>
public static async System.Threading.Tasks.Task<Tracker.Core.Data.Entities.User?> GetByEmailAddressAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.User> queryable, string emailAddress, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -62,6 +63,7 @@ public static partial class UserExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.User"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.User?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.User> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static partial class UserLoginExtensions
/// </summary>
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="id">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.UserLogin"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.UserLogin?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.UserLogin> queryable, Guid id, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static partial class UserRoleExtensions
/// <param name="queryable">An <see cref="T:System.Linq.IQueryable`1" /> to filter.</param>
/// <param name="userId">The value to filter by.</param>
/// <param name="roleId">The value to filter by.</param>
/// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>An instance of <see cref="T:Tracker.Core.Data.Entities.UserRole"/> or null if not found.</returns>
public static async System.Threading.Tasks.ValueTask<Tracker.Core.Data.Entities.UserRole?> GetByKeyAsync(this System.Linq.IQueryable<Tracker.Core.Data.Entities.UserRole> queryable, Guid userId, Guid roleId, System.Threading.CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using FluentValidation;

using Tracker.Core.Domain.Models;

// ReSharper disable once CheckNamespace
namespace Tracker.Core.Domain.Validation;

/// <summary>
/// Validator class for <see cref="AuditCreateModel"/> .
/// </summary>
[RegisterSingleton<IValidator<AuditCreateModel>>]
public partial class AuditCreateModelValidator
: AbstractValidator<AuditCreateModel>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using FluentValidation;
using Tracker.Core.Domain.Models;

// ReSharper disable once CheckNamespace
namespace Tracker.Core.Domain.Validation;

/// <summary>
/// Validator class for <see cref="AuditUpdateModel"/> .
/// </summary>
[RegisterSingleton<IValidator<AuditUpdateModel>>]
public partial class AuditUpdateModelValidator
: AbstractValidator<AuditUpdateModel>
{
Expand Down
3 changes: 2 additions & 1 deletion sample/Tracker/Tracker.Core/Tracker.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="Injectio" Version="3.1.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
</ItemGroup>

<ItemGroup>
<Folder Include="Data\Mapping\" />
<Using Include="Injectio.Attributes" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions sample/Tracker/Tracker.Core/generation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ model:
baseClass: 'AbstractValidator<{Model.Name}>'
namespace: '{Project.Namespace}.Domain.Validation'
directory: '{Project.Directory}\Domain\{Entity.Name}\Validation'
attributes: '[RegisterSingleton<IValidator<{Model.Name}>>]'
document: true
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="Oracle.EntityFrameworkCore" Version="8.21.121" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="15.1.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EntityFrameworkCore.Generator.Metadata.Generation;
namespace EntityFrameworkCore.Generator.Metadata.Generation;

public class Model : ModelBase, IOptionVariable
{
Expand All @@ -17,6 +17,8 @@ public Model()

public string ModelBaseClass { get; set; }

public string ModelAttributes { get; set; }


public string ValidatorNamespace { get; set; }

Expand All @@ -27,7 +29,6 @@ public Model()

public PropertyCollection Properties { get; set; }


void IOptionVariable.Set(VariableDictionary variableDictionary)
{
variableDictionary.Set(VariableConstants.ModelName, ModelClass);
Expand All @@ -37,4 +38,4 @@ void IOptionVariable.Remove(VariableDictionary variableDictionary)
{
variableDictionary.Remove(VariableConstants.ModelName);
}
}
}
5 changes: 2 additions & 3 deletions src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -537,7 +535,8 @@ private void CreateModel<TOption>(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)
Expand Down
92 changes: 36 additions & 56 deletions src/EntityFrameworkCore.Generator.Core/OptionMapper.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit ceaa6a5

Please sign in to comment.