Skip to content

Commit

Permalink
Dbcontextname (#330)
Browse files Browse the repository at this point in the history
* start of adding flexibility for naming dbcontext

* update dbcontext prep based on merge

* fix name generation
  • Loading branch information
dpvreony committed Dec 27, 2022
1 parent 185c1b1 commit 14f54af
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 19 deletions.
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
@@ -0,0 +1,22 @@
using System;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
using Dhgms.Nucleotide.Generators.PropertyInfo;

namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
{
public sealed class EntityFrameworkDbContextGenerationModel : IClassName
{
public EntityGenerationModel[] DbSetEntities
{
get;
set;
}

public string ClassName
{
get;
set;
}
}
}
Expand Up @@ -10,7 +10,7 @@ namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
/// <summary>
/// Code Generator for Entity Framework DB Context
/// </summary>
public abstract class EntityFrameworkDbContextGenerator : BaseGenerator<EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, IEntityGenerationModel>
public abstract class EntityFrameworkDbContextGenerator : BaseGenerator<EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, EntityFrameworkDbContextGenerationModel>
{
///<inheritdoc />
protected override string GetNamespace()
Expand Down
Expand Up @@ -17,12 +17,12 @@ namespace Dhgms.Nucleotide.Generators.Features.EntityFramework
/// <summary>
/// Code Generator for Entity Framework DB Context
/// </summary>
public sealed class EntityFrameworkDbContextGeneratorProcessor : BaseGeneratorProcessor<IEntityGenerationModel>
public sealed class EntityFrameworkDbContextGeneratorProcessor : BaseGeneratorProcessor<EntityFrameworkDbContextGenerationModel>
{
///<inheritdoc />
public override NamespaceDeclarationSyntax GenerateObjects(
NamespaceDeclarationSyntax namespaceDeclaration,
INucleotideGenerationModel<IEntityGenerationModel> nucleotideGenerationModel)
INucleotideGenerationModel<EntityFrameworkDbContextGenerationModel> nucleotideGenerationModel)
{
var generationModelEntityGenerationModel = nucleotideGenerationModel.EntityGenerationModel;

Expand All @@ -35,7 +35,12 @@ public sealed class EntityFrameworkDbContextGeneratorProcessor : BaseGeneratorPr
var classDeclarations = new List<MemberDeclarationSyntax>();

var suffix = GetClassSuffix();
classDeclarations.Add(GetClassDeclarationSyntax(generationModelEntityGenerationModel, suffix));

foreach (var entityFrameworkDbContextGenerationModel in generationModelEntityGenerationModel)
{
classDeclarations.Add(GetClassDeclarationSyntax(entityFrameworkDbContextGenerationModel, suffix));
}


var usings = GetUsingDirectives();
namespaceDeclaration = namespaceDeclaration.AddUsings(usings);
Expand All @@ -45,7 +50,7 @@ public sealed class EntityFrameworkDbContextGeneratorProcessor : BaseGeneratorPr
return namespaceDeclaration;
}

private MemberDeclarationSyntax[] GetMembers(string className, string entityName, IEntityGenerationModel[] generationModelEntityGenerationModel)
private MemberDeclarationSyntax[] GetMembers(string className, EntityGenerationModel[] generationModelEntityGenerationModel)
{
var result = new List<MemberDeclarationSyntax>();

Expand Down Expand Up @@ -123,7 +128,7 @@ private ConstructorDeclarationSyntax GetConstructorWithDbOptions(string classNam
/// Gets the method declarations to be generated
/// </summary>
/// <returns></returns>
private MemberDeclarationSyntax[] GetMethodDeclarations(IEntityGenerationModel[] generationModelEntityGenerationModel)
private MemberDeclarationSyntax[] GetMethodDeclarations(EntityGenerationModel[] generationModelEntityGenerationModel)
{
var results = new[]
{
Expand All @@ -137,13 +142,13 @@ private MemberDeclarationSyntax[] GetMethodDeclarations(IEntityGenerationModel[]
/// Gets the property declarations to be generated
/// </summary>
/// <returns></returns>
private PropertyDeclarationSyntax[] GetPropertyDeclarations(IEntityGenerationModel[] generationModelEntityGenerationModel)
private PropertyDeclarationSyntax[] GetPropertyDeclarations(EntityGenerationModel[] generationModelEntityGenerationModel)
{
return generationModelEntityGenerationModel?.Select(GetPropertyDeclaration).ToArray();
}

private PropertyDeclarationSyntax GetPropertyDeclaration(
IEntityGenerationModel generationModelEntityGenerationModel)
EntityGenerationModel generationModelEntityGenerationModel)
{
var setType = SyntaxFactory.ParseTypeName($"Set<EfModels.{generationModelEntityGenerationModel.ClassName}EfModel>");
var setCreation = SyntaxFactory.InvocationExpression(setType);
Expand Down Expand Up @@ -190,12 +195,14 @@ private PropertyDeclarationSyntax[] GetPropertyDeclarations(IEntityGenerationMod
// return result.ToArray();
//}

private MemberDeclarationSyntax GetClassDeclarationSyntax(IEntityGenerationModel[] generationModelEntityGenerationModel, string suffix)
private MemberDeclarationSyntax GetClassDeclarationSyntax(EntityFrameworkDbContextGenerationModel generationModelEntityGenerationModel, string suffix)
{
// TODO : our model needs a parent name.
var entityName = "Test";
var entityName = generationModelEntityGenerationModel.ClassName;
var className = $"{entityName}{suffix}";
var members = GetMembers(className, entityName, generationModelEntityGenerationModel);
var members = GetMembers(
className,
generationModelEntityGenerationModel.DbSetEntities);

// SyntaxFactory.Token(SyntaxKind.SealedKeyword)

Expand Down Expand Up @@ -331,7 +338,7 @@ protected override PropertyDeclarationSyntax GetReadOnlyPropertyDeclaration(Prop
return null;
}

private MemberDeclarationSyntax GetOnModelCreatingMethodDeclaration(IEntityGenerationModel[] generationModelEntityGenerationModel)
private MemberDeclarationSyntax GetOnModelCreatingMethodDeclaration(EntityGenerationModel[] generationModelEntityGenerationModel)
{
var methodName = $"OnModelCreating";

Expand All @@ -347,7 +354,7 @@ private MemberDeclarationSyntax GetOnModelCreatingMethodDeclaration(IEntityGener
return declaration;
}

private StatementSyntax GetApplyConfigurationInvocationDeclaration(IEntityGenerationModel entityGenerationModel)
private StatementSyntax GetApplyConfigurationInvocationDeclaration(EntityGenerationModel entityGenerationModel)
{
var invokeExpression = RoslynGenerationHelpers.GetMethodOnVariableInvocationSyntax("modelBuilder",
"ApplyConfiguration",
Expand Down
18 changes: 18 additions & 0 deletions src/Dhgms.Nucleotide.ModelTests/ModelGenerationDetails.cs
Expand Up @@ -90,6 +90,24 @@ public class UserEntityGenerationModel : EntityGenerationModel
};
}

public class EntityFrameworkGenerationModelDetails : INucleotideGenerationModel<EntityFrameworkDbContextGenerationModel>
{
public EntityFrameworkDbContextGenerationModel[] EntityGenerationModel =>
new EntityFrameworkDbContextGenerationModel[]
{
new EntityFrameworkDbContextGenerationModel
{
ClassName = "SomeProduct",
DbSetEntities = new EntityGenerationModel[]
{
new UserEntityGenerationModel(),
}
}
};

public string RootNamespace => "Dhgms.Nucleotide.GenerationTests";
}

public class ModelGenerationDetails : INucleotideGenerationModel<IEntityGenerationModel>
{
public IEntityGenerationModel[] EntityGenerationModel => new IEntityGenerationModel[]
Expand Down
Expand Up @@ -11,6 +11,6 @@ namespace Dhgms.Nucleotide.ModelTests
[Generator]
public sealed class TestEntityFrameworkDbContextGenerator : EntityFrameworkDbContextGenerator
{
protected override INucleotideGenerationModel<IEntityGenerationModel> NucleotideGenerationModel => new ModelGenerationDetails();
protected override INucleotideGenerationModel<EntityFrameworkDbContextGenerationModel> NucleotideGenerationModel => new EntityFrameworkGenerationModelDetails();
}
}
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp6.0</TargetFramework>
<IsPackable>false</IsPackable>
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
Expand Down
Expand Up @@ -15,7 +15,7 @@ namespace Dhgms.Nucleotide.UnitTests.Generators
[ExcludeFromCodeCoverage]
public static class EntityFrameworkDbContextGeneratorTests
{
public sealed class ConstructorMethod : BaseGeneratorTests.BaseConstructorMethod<EntityFrameworkDbContextGenerator, EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, IEntityGenerationModel>
public sealed class ConstructorMethod : BaseGeneratorTests.BaseConstructorMethod<EntityFrameworkDbContextGenerator, EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, EntityFrameworkDbContextGenerationModel>
{
public ConstructorMethod(ITestOutputHelper output) : base(output)
{
Expand All @@ -27,7 +27,7 @@ public ConstructorMethod(ITestOutputHelper output) : base(output)
}
}

public sealed class GenerateAsyncMethod : BaseGeneratorTests.BaseGenerateAsyncMethod<EntityFrameworkDbContextGenerator, EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, IEntityGenerationModel>
public sealed class GenerateAsyncMethod : BaseGeneratorTests.BaseGenerateAsyncMethod<EntityFrameworkDbContextGenerator, EntityFrameworkDbContextFeatureFlags, EntityFrameworkDbContextGeneratorProcessor, EntityFrameworkDbContextGenerationModel>
{
public GenerateAsyncMethod(ITestOutputHelper output) : base(output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Dhgms.Nucleotide/Dhgms.Nucleotide.csproj
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
Expand Down

0 comments on commit 14f54af

Please sign in to comment.