Skip to content

Commit

Permalink
feature: logic to allow overriding base dbcontext (#434)
Browse files Browse the repository at this point in the history
* logic to allow overriding base dbcontext

* *BREAKING* detail how the ef generator needs to inherit the old model

* factor in some changes on the efmodel gen

* simplify creation of gen models for ef

* update default for created, modified, rowversion columns
  • Loading branch information
dpvreony committed Feb 2, 2024
1 parent d38290c commit 5af3d81
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 177 deletions.
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Whipstaff.AspNetCore" Version="7.1.50" />
<PackageReference Include="Whipstaff.Core" Version="7.1.50" />
<PackageReference Include="Whipstaff.EntityFramework" Version="7.1.50" />
Expand Down
@@ -1,7 +1,6 @@
using System;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
using Dhgms.Nucleotide.Generators.PropertyInfo;

namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
{
Expand All @@ -18,5 +17,11 @@ public string ClassName
get;
set;
}

public string OverrideBaseDbContextType
{
get;
set;
}
}
}
Expand Up @@ -226,7 +226,7 @@ private MemberDeclarationSyntax GetClassDeclarationSyntax(EntityFrameworkDbConte
// declaration = declaration.AddAttributeLists(attributeListSyntax);
//}

var baseClass = GetBaseClass(entityName);
var baseClass = GetBaseClass(generationModelEntityGenerationModel.OverrideBaseDbContextType);
if (!string.IsNullOrWhiteSpace(baseClass))
{
var b = SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(baseClass));
Expand Down Expand Up @@ -288,9 +288,22 @@ private IEnumerable<string> GetImplementedInterfaces(string entityName)
return null;
}

private string GetBaseClass(string entityName)
private string GetBaseClass(string overrideBaseDbContextType)
{
return "Microsoft.EntityFrameworkCore.DbContext";
const string baseDbContextType = "Microsoft.EntityFrameworkCore.DbContext";
if (overrideBaseDbContextType != null)
{
/*
if (!overrideBaseDbContextType.IsSubclassOf(Type.GetType(baseDbContextType)))
{
throw new ArgumentException($"The {nameof(overrideBaseDbContextType)} type \"{overrideBaseDbContextType.FullName}\" does not inherit from {baseDbContextType}");
};
*/

return overrideBaseDbContextType;
}

return baseDbContextType;
}

private UsingDirectiveSyntax[] GetUsingDirectives()
Expand Down
Expand Up @@ -4,21 +4,20 @@

using System.Collections.Generic;
using Dhgms.Nucleotide.Generators.Features.Database;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.PropertyInfo;
using Dhgms.Nucleotide.Generators.Models;

namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
{
public sealed class EntityFrameworkModelEntityGenerationModel : IClassName
public sealed class EntityFrameworkModelEntityGenerationModel : EntityGenerationModel
{
public string ClassName { get; set; }
public string ClassPluralName { get; init; }

public string ClassPluralName { get; set; }
public IList<ReferencedByEntityGenerationModel> ParentEntityRelationships { get; init; }

public IList<PropertyInfoBase> Properties { get; set; }
public IList<ReferencedByEntityGenerationModel> ChildEntityRelationships { get; init; }

public IList<ReferencedByEntityGenerationModel> ParentEntityRelationships { get; set; }
public GenerateCreatedAndModifiedColumns GenerateCreatedAndModifiedColumns { get; init; } = GenerateCreatedAndModifiedColumns.CreatedAndModified;

public IList<ReferencedByEntityGenerationModel> ChildEntityRelationships { get; set; }
public bool GenerateRowVersionColumn { get; init; } = true;
}
}
Expand Up @@ -27,24 +27,33 @@ protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclaration
{
var inheritDocSyntaxTrivia = RoslynGenerationHelpers.GetInheritDocSyntaxTrivia().ToArray();
var datetimeOffSetType = SyntaxFactory.ParseTypeName("System.DateTimeOffset");
var ulongType = SyntaxFactory.ParseTypeName("ulong");

yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
datetimeOffSetType,
$"Created",
inheritDocSyntaxTrivia);
if (entityGenerationModel.GenerateCreatedAndModifiedColumns is GenerateCreatedAndModifiedColumns.CreatedOnly or GenerateCreatedAndModifiedColumns.CreatedAndModified)
{
yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
datetimeOffSetType,
$"Created",
inheritDocSyntaxTrivia);
}

yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
datetimeOffSetType,
$"Modified",
inheritDocSyntaxTrivia);
if (entityGenerationModel.GenerateCreatedAndModifiedColumns == GenerateCreatedAndModifiedColumns.CreatedAndModified)
{
yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
datetimeOffSetType,
$"Modified",
inheritDocSyntaxTrivia);
}

yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
ulongType,
$"RowVersion",
inheritDocSyntaxTrivia);
if (entityGenerationModel.GenerateRowVersionColumn)
{
var ulongType = SyntaxFactory.ParseTypeName("ulong");
yield return RoslynGenerationHelpers.GetPropertyDeclarationSyntax(
ulongType,
$"RowVersion",
inheritDocSyntaxTrivia);
}

if (entityGenerationModel?.ParentEntityRelationships != null)
if (entityGenerationModel.ParentEntityRelationships != null)
{
foreach (var referencedByEntityGenerationModel in entityGenerationModel.ParentEntityRelationships)
{
Expand All @@ -64,7 +73,7 @@ protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclaration
}
}

if (entityGenerationModel?.ChildEntityRelationships != null)
if (entityGenerationModel.ChildEntityRelationships != null)
{
foreach (var referencedByEntityGenerationModel in entityGenerationModel.ChildEntityRelationships)
{
Expand Down Expand Up @@ -157,15 +166,32 @@ protected override string[] GetClassLevelCommentRemarks(string entityName)
///<inheritdoc />
protected override string GetBaseClass(EntityFrameworkModelEntityGenerationModel entityGenerationModel)
{
if (entityGenerationModel.BaseTypeEntityGenerationModel != null)
{
return entityGenerationModel.BaseTypeEntityGenerationModel.FullyQualifiedClassName;
}

var entityName = entityGenerationModel.ClassName;
return $"Models.{entityName}Model";
}

///<inheritdoc />
protected override IEnumerable<string> GetImplementedInterfaces(EntityFrameworkModelEntityGenerationModel entityGenerationModel)
{
yield return $"global::Whipstaff.Core.Entities.ILongRowVersion";
yield return $"global::Whipstaff.Core.Entities.IModifiable";
if (entityGenerationModel.GenerateRowVersionColumn)
{
yield return $"global::Whipstaff.Core.Entities.ILongRowVersion";
}

switch (entityGenerationModel.GenerateCreatedAndModifiedColumns)
{
case GenerateCreatedAndModifiedColumns.CreatedOnly:
yield return $"global::Whipstaff.Core.Entities.ICreatable";
break;
case GenerateCreatedAndModifiedColumns.CreatedAndModified:
yield return $"global::Whipstaff.Core.Entities.IModifiable";
break;
}

if (entityGenerationModel?.ParentEntityRelationships != null)
{
Expand Down
@@ -0,0 +1,23 @@
namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
{
/// <summary>
/// Enum to dictate whether to generate created and modified columns on a DBSet.
/// </summary>
public enum GenerateCreatedAndModifiedColumns
{
/// <summary>
/// Don't generate either column.
/// </summary>
None,

/// <summary>
/// Generate only the created column.
/// </summary>
CreatedOnly,

/// <summary>
/// Generate both the created and modified columns.
/// </summary>
CreatedAndModified
}
}
Expand Up @@ -60,6 +60,11 @@ protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclaration
{
var idColumn = GetIdColumn(entityGenerationModel.KeyType);

if (idColumn == null)
{
return Array.Empty<PropertyDeclarationSyntax>();
}

return new[]
{
GetReadOnlyPropertyDeclaration(idColumn)
Expand Down Expand Up @@ -98,6 +103,9 @@ private static PropertyInfoBase GetIdColumn(KeyType keyType)
int.MaxValue,
true,
null);
case KeyType.Inherited:
// TODO: this need validation that the base class has a key
return null;
default:
throw new ArgumentOutOfRangeException(nameof(keyType));
}
Expand Down
Expand Up @@ -57,6 +57,8 @@ protected override string[] GetBaseInterfaces(IEntityGenerationModel entityGener
case KeyType.Int64:
result.Add("global::Whipstaff.Core.Entities.ILongId");
break;
case KeyType.Inherited:
break;
default:
throw new ArgumentOutOfRangeException(nameof(entityGenerationModel.KeyType));
}
Expand Down
@@ -0,0 +1,4 @@
namespace Dhgms.Nucleotide.Generators.Models
{
public sealed record BaseEntityTypeGenerationModel(string FullyQualifiedClassName);
}
14 changes: 7 additions & 7 deletions src/Dhgms.Nucleotide.Generators/Models/EntityGenerationModel.cs
Expand Up @@ -9,27 +9,27 @@ namespace Dhgms.Nucleotide.Generators.Models
/// <summary>
/// Represents the parameters for generating an data manager classes
/// </summary>
public abstract class EntityGenerationModel : IEntityGenerationModel
public class EntityGenerationModel : IEntityGenerationModel
{
/// <summary>
/// Gets the name of the information class.
/// </summary>
public abstract string ClassName { get; }
public string ClassName { get; init; }

public abstract KeyType KeyType { get; }
public KeyType KeyType { get; init; }

public abstract IEntityGenerationModel BaseTypeEntityGenerationModel { get; }
public BaseEntityTypeGenerationModel BaseTypeEntityGenerationModel { get; init; }

public abstract InterfaceGenerationModel[] InterfaceGenerationModels { get; }
public InterfaceGenerationModel[] InterfaceGenerationModels { get; init; }

/// <summary>
/// Gets the name of the information class.
/// </summary>
public abstract string ClassRemarks { get; }
public string ClassRemarks { get; init; }

/// <summary>
/// Gets the collection of properties for the class.
/// </summary>
public abstract PropertyInfoBase[] Properties { get; }
public PropertyInfoBase[] Properties { get; init; }
}
}
Expand Up @@ -21,7 +21,7 @@ public interface IEntityGenerationModel : IObjectGenerationModel
/// </summary>
PropertyInfoBase[] Properties { get; }

IEntityGenerationModel BaseTypeEntityGenerationModel { get; }
BaseEntityTypeGenerationModel BaseTypeEntityGenerationModel { get; }

InterfaceGenerationModel[] InterfaceGenerationModels { get; }

Expand Down
3 changes: 2 additions & 1 deletion src/Dhgms.Nucleotide.Generators/Models/KeyType.cs
Expand Up @@ -9,6 +9,7 @@ public enum KeyType
Unknown,
Guid,
Int32,
Int64
Int64,
Inherited
}
}

0 comments on commit 5af3d81

Please sign in to comment.