From 3a964bf4dad2cb5bdfb0a9a2275fc764c2801ed9 Mon Sep 17 00:00:00 2001 From: Arnaud Boussaer Date: Fri, 22 Mar 2024 10:38:57 +0100 Subject: [PATCH] #545 Add option for additional using statements in the entity --- src/EntityFrameworkCore.Generator.Core/OptionMapper.cs | 1 + .../Options/EntityClassOptions.cs | 5 +++++ .../Serialization/EntityClass.cs | 5 +++++ .../Templates/EntityClassTemplate.cs | 8 ++++++++ .../Options/full.yaml | 1 + 5 files changed, 20 insertions(+) diff --git a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs index ad837307..75c999ee 100644 --- a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs +++ b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs @@ -154,6 +154,7 @@ private static void MapEntity(EntityClassOptions option, EntityClass entity) option.EntityNaming = entity.EntityNaming; option.RelationshipNaming = entity.RelationshipNaming; option.PrefixWithSchemaName = entity.PrefixWithSchemaName; + option.AdditionalUsings = entity.AdditionalUsings; MapSelection(option.Renaming, entity.Renaming); } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs index 3878ce7a..647bd9d8 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs @@ -79,4 +79,9 @@ public string BaseClass /// The renaming expressions. /// public SelectionOptions Renaming { get; } + + /// + /// Additional using statements, ;-separated - example: MyLib;MyLib.Data;MyLib.Domain;MyLib.Domain.Interfaces + /// + public string AdditionalUsings { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs index 44df5285..d44ff2d9 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs @@ -70,4 +70,9 @@ public EntityClass() /// The renaming expressions. /// public SelectionModel Renaming { get; set; } + + /// + /// Additional using statements, ;-separated - example: MyLib;MyLib.Data;MyLib.Domain;MyLib.Domain.Interfaces + /// + public string AdditionalUsings { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs index 8bea59bf..f840edfc 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs @@ -21,6 +21,14 @@ public override string WriteCode() CodeBuilder.AppendLine("using System;"); CodeBuilder.AppendLine("using System.Collections.Generic;"); + if (!Options.Data.Entity.AdditionalUsings.IsNullOrWhiteSpace()) + { + var splittedUsingEntries = Options.Data.Entity.AdditionalUsings.Split(';').Distinct() + .Where(s => !s.IsNullOrWhiteSpace()); + + foreach (var usingEntry in splittedUsingEntries) + CodeBuilder.AppendLine($"using {usingEntry};"); + } CodeBuilder.AppendLine(); CodeBuilder.Append($"namespace {_entity.EntityNamespace}"); diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml index 5d253569..836403e0 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml +++ b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml @@ -34,6 +34,7 @@ data: relationshipNaming: Plural document: false prefixWithSchemaName: false + additionalUsings: '' renaming: entities: - ^(sp|tbl|udf|vw)_