Skip to content

Commit

Permalink
feature: generate index logic for entity type configuration (#499)
Browse files Browse the repository at this point in the history
* add rowversion generation to entity type config

* initial logic for generating indexes

* Update EntityGenerationModel.cs

* adjust index record to just use names

* adjust index gen to be a lambda expression
  • Loading branch information
dpvreony authored Sep 18, 2024
1 parent c4dd307 commit 516a420
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Dhgms.Nucleotide.Generators.Features.Database;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
Expand Down Expand Up @@ -183,6 +184,11 @@ private MethodDeclarationSyntax GetConfigureMethodDeclaration(EntityFrameworkMod
}
}

if (entityGenerationModel.GenerateRowVersionColumn)
{
GenerateConfigurePropertyColumnInvocation("RowVersion", subMethodParams, body);
}

if (entityGenerationModel.ChildEntityRelationships != null)
{
foreach (var referencedByEntityGenerationModel in entityGenerationModel.ChildEntityRelationships)
Expand All @@ -203,6 +209,7 @@ private MethodDeclarationSyntax GetConfigureMethodDeclaration(EntityFrameworkMod
}
}


var parameters = GetParams(new []{ $"Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<EfModels.{entityName}EfModel> builder"});

var returnType = SyntaxFactory.ParseTypeName("void");
Expand Down Expand Up @@ -343,10 +350,7 @@ private MethodDeclarationSyntax GetTableMappingMethodDeclaration(
{
var methodName = $"ConfigureTable";

var body = new List<StatementSyntax>();

//var tableInvocation = GetEfTableInvocation(entityGenerationModel);
//body.Add(tableInvocation);
var body = GetTableMappingBodyDeclaration(entityGenerationModel);

var parameters = GetParams(new[] { $"Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<EfModels.{entityName}EfModel> builder" });

Expand Down Expand Up @@ -500,9 +504,13 @@ private ExpressionSyntax CheckHasMaximumValueMethodDeclaration(ExpressionSyntax
return fluentApiInvocation;
}

private StatementSyntax GetEfTableInvocation(IEntityGenerationModel entityGenerationModel)
private List<StatementSyntax> GetTableMappingBodyDeclaration(IEntityGenerationModel entityGenerationModel)
{
throw new NotImplementedException();
var body = new List<StatementSyntax>();
GenerateIndexDeclarations(entityGenerationModel, body);

return body;

/*
var fluentApiInvocation = RoslynGenerationHelpers.GetMethodOnVariableInvocationExpression(
"builder",
Expand All @@ -522,6 +530,47 @@ private StatementSyntax GetEfTableInvocation(IEntityGenerationModel entityGenera
*/
}

private static void GenerateIndexDeclarations(IEntityGenerationModel entityGenerationModel, List<StatementSyntax> body)
{
var indexes = entityGenerationModel.Indexes;
if (indexes == null)
{
return;
}

foreach (var index in indexes)
{
var statement = GetIndexDeclarationExpressionStatementSyntax(index);
body.Add(statement);
}
}

private static ExpressionStatementSyntax GetIndexDeclarationExpressionStatementSyntax(IndexGenerationModel index)
{
var args = index.Names.Select(n => $"t.{n}");

var flattedPropertyNames = string.Join(", ", args);

var fluentApiInvocation = RoslynGenerationHelpers.GetMethodOnVariableInvocationExpression(
"builder",
"HasIndex",
[ $"t => new {{ {flattedPropertyNames} }}" ],
false);


if (index.IsUnique)
{
fluentApiInvocation = RoslynGenerationHelpers.GetFluentApiChainedInvocationExpression(
fluentApiInvocation,
"IsUnique",
[]);

}

var statement = SyntaxFactory.ExpressionStatement(fluentApiInvocation);
return statement;
}

private ExpressionSyntax CheckHasComputedColumnSqlMethodDeclaration(ExpressionSyntax fluentApiInvocation, string sqlComputedColumn)
{
if (sqlComputedColumn == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class EntityGenerationModel : IEntityGenerationModel

public InterfaceGenerationModel[] InterfaceGenerationModels { get; init; }

public IndexGenerationModel[] Indexes { get; init; }

/// <summary>
/// Gets the name of the information class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IEntityGenerationModel : IObjectGenerationModel

InterfaceGenerationModel[] InterfaceGenerationModels { get; }

IndexGenerationModel[] Indexes { get; }

/// <summary>
/// Gets the class remarks.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Dhgms.Nucleotide.Generators.PropertyInfo;
using System;
using System.Collections.Generic;
using System.Text;

namespace Dhgms.Nucleotide.Generators.Models
{
public sealed record IndexGenerationModel(string[] Names, bool IsUnique);
}
71 changes: 54 additions & 17 deletions src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public class EntityFrameworkGenerationModelDetails : INucleotideGenerationModel<
ClassName = "SomeProduct",
DbSetEntities =
[
SampleEntityFrameworkModelGenerationModel.UserEntityFrameworkModelEntityGenerationModel
SampleEntityFrameworkModelGenerationModel.AddressEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.GenderEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.PersonEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.PersonAddressEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.SalutationEntityFrameworkModelEntityGenerationModel
]
}
];
Expand Down Expand Up @@ -53,8 +57,8 @@ public class ModelGenerationDetails : INucleotideGenerationModel<IEntityGenerati
SampleEntityFrameworkModelGenerationModel.AddressEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.GenderEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.PersonEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.SalutationEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.UserEntityFrameworkModelEntityGenerationModel
SampleEntityFrameworkModelGenerationModel.PersonAddressEntityFrameworkModelEntityGenerationModel,
SampleEntityFrameworkModelGenerationModel.SalutationEntityFrameworkModelEntityGenerationModel
];

public string RootNamespace => "Dhgms.Nucleotide.GenerationTests";
Expand All @@ -72,8 +76,10 @@ public class ReferencedByEntityGenerationModelGenerationDetails : INucleotideGen
{
public ReferencedByEntityGenerationModel[] EntityGenerationModel =>
[
SampleEntityFrameworkModelGenerationModel.AddressEntityRelationship,
SampleEntityFrameworkModelGenerationModel.GenderEntityRelationship,
SampleEntityFrameworkModelGenerationModel.PersonEntityRelationship,
SampleEntityFrameworkModelGenerationModel.PersonAddressEntityRelationship,
SampleEntityFrameworkModelGenerationModel.SalutationEntityRelationship,
];

Expand All @@ -85,6 +91,14 @@ public static class SampleEntityFrameworkModelGenerationModel
public static string RootNamespace => "Dhgms.Nucleotide.GenerationTests";
public static string DatabaseRootNamespace => $"{RootNamespace}.Database";

public static ReferencedByEntityGenerationModel AddressEntityRelationship => new(
DatabaseRootNamespace,
"Address",
"Address",
"Address",
"Addresses",
"int");

public static ReferencedByEntityGenerationModel SalutationEntityRelationship => new (
DatabaseRootNamespace,
"Salutation",
Expand All @@ -109,6 +123,14 @@ public static class SampleEntityFrameworkModelGenerationModel
"Persons",
"int");

public static ReferencedByEntityGenerationModel PersonAddressEntityRelationship => new(
DatabaseRootNamespace,
"PersonAddress",
"PersonAddress",
"PersonAddress",
"PersonAddresses",
"int");

public static EntityFrameworkModelEntityGenerationModel SalutationEntityFrameworkModelEntityGenerationModel => new()
{
ClassName = "Salutation",
Expand All @@ -132,8 +154,12 @@ public static class SampleEntityFrameworkModelGenerationModel
ParentEntityRelationships = new List<ReferencedByEntityGenerationModel>
{
SalutationEntityRelationship,
GenderEntityRelationship,
GenderEntityRelationship
},
ChildEntityRelationships =
[
PersonAddressEntityRelationship
]
};

public static EntityFrameworkModelEntityGenerationModel GenderEntityFrameworkModelEntityGenerationModel => new()
Expand All @@ -151,25 +177,36 @@ public static class SampleEntityFrameworkModelGenerationModel
]
};

public static EntityFrameworkModelEntityGenerationModel UserEntityFrameworkModelEntityGenerationModel => new()
public static EntityFrameworkModelEntityGenerationModel AddressEntityFrameworkModelEntityGenerationModel => new()
{
ClassName = "User",
ClassPluralName = "Users",
KeyType = KeyType.Int32,
BaseTypeEntityGenerationModel = null,
InterfaceGenerationModels = null,
ClassRemarks = "Represents a User",
Properties = [
new ClrStringPropertyInfo(CollectionType.None, "Username", "Username for the user", false, 3, 255, false, false, null),
new ClrStringPropertyInfo(CollectionType.None, "PasswordHash", "Hash of the user password", true, 0, 1024, false, false, null)
ClassName = "Address",
ClassPluralName = "Addresses",
ChildEntityRelationships =
[
PersonAddressEntityRelationship
]
};

public static EntityFrameworkModelEntityGenerationModel AddressEntityFrameworkModelEntityGenerationModel => new()
public static EntityFrameworkModelEntityGenerationModel PersonAddressEntityFrameworkModelEntityGenerationModel => new()
{
KeyType = KeyType.Int32,
ClassName = "Address",
ClassPluralName = "Addresses",
ClassName = "PersonAddress",
ClassPluralName = "PersonAddresses",
ParentEntityRelationships =
[
PersonEntityRelationship,
AddressEntityRelationship
],
Indexes =
[
new IndexGenerationModel(
[
PersonEntityRelationship.SingularPropertyName,
AddressEntityRelationship.SingularPropertyName
],
true)
]
};

public static EntityFrameworkModelEntityGenerationModel SomeRoleEntityFrameworkModelEntityGenerationModel => new()
Expand Down Expand Up @@ -203,10 +240,10 @@ public static class SampleEntityFrameworkModelGenerationModel
SalutationEntityFrameworkModelEntityGenerationModel,
PersonEntityFrameworkModelEntityGenerationModel,
GenderEntityFrameworkModelEntityGenerationModel,
UserEntityFrameworkModelEntityGenerationModel,
AddressEntityFrameworkModelEntityGenerationModel,
SomeRoleEntityFrameworkModelEntityGenerationModel,
SomeUserEntityFrameworkModelEntityGenerationModel,
PersonAddressEntityFrameworkModelEntityGenerationModel
];
}

Expand Down

0 comments on commit 516a420

Please sign in to comment.