Skip to content

Commit 820017c

Browse files
authored
Merge pull request #362 from vinod-vetrivel/file-scoped-namespace
Option to create file-scoped namespaces
2 parents 3440cec + f520ae1 commit 820017c

File tree

8 files changed

+112
-37
lines changed

8 files changed

+112
-37
lines changed

src/EntityFrameworkCore.Generator.Core/Options/ClassOptionsBase.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
22

33
namespace EntityFrameworkCore.Generator.Options
44
{
@@ -50,5 +50,14 @@ public string Directory
5050
/// </value>
5151
[DefaultValue(false)]
5252
public bool Document { get; set; }
53+
54+
/// <summary>
55+
/// Gets or sets a value indicating whether to use file-scoped namespace.
56+
/// </summary>
57+
/// <value>
58+
/// <c>true</c> to use file-coped namespace; otherwise, <c>false</c>.
59+
/// </value>
60+
[DefaultValue(false)]
61+
public bool FileScopedNamespace { get; set; }
5362
}
54-
}
63+
}

src/EntityFrameworkCore.Generator.Core/Templates/DataContextTemplate.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,25 @@ public override string WriteCode()
2323
CodeBuilder.AppendLine("using Microsoft.EntityFrameworkCore.Metadata;");
2424
CodeBuilder.AppendLine();
2525

26-
CodeBuilder.AppendLine($"namespace {_entityContext.ContextNamespace}");
27-
CodeBuilder.AppendLine("{");
26+
CodeBuilder.Append($"namespace {_entityContext.ContextNamespace}");
2827

29-
using (CodeBuilder.Indent())
28+
if (Options.Data.Context.FileScopedNamespace)
3029
{
30+
CodeBuilder.AppendLine(";");
3131
GenerateClass();
3232
}
33+
else
34+
{
35+
CodeBuilder.AppendLine();
36+
CodeBuilder.AppendLine("{");
3337

34-
CodeBuilder.AppendLine("}");
38+
using (CodeBuilder.Indent())
39+
{
40+
GenerateClass();
41+
}
42+
43+
CodeBuilder.AppendLine("}");
44+
}
3545

3646
return CodeBuilder.ToString();
3747
}

src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using System.DirectoryServices.ActiveDirectory;
21
using System.Linq;
3-
42
using EntityFrameworkCore.Generator.Extensions;
53
using EntityFrameworkCore.Generator.Metadata.Generation;
64
using EntityFrameworkCore.Generator.Options;
75

8-
using Microsoft.Extensions.Options;
9-
106
namespace EntityFrameworkCore.Generator.Templates
117
{
128
public class EntityClassTemplate : CodeTemplateBase
@@ -26,15 +22,25 @@ public override string WriteCode()
2622
CodeBuilder.AppendLine("using System.Collections.Generic;");
2723
CodeBuilder.AppendLine();
2824

29-
CodeBuilder.AppendLine($"namespace {_entity.EntityNamespace}");
30-
CodeBuilder.AppendLine("{");
25+
CodeBuilder.Append($"namespace {_entity.EntityNamespace}");
3126

32-
using (CodeBuilder.Indent())
27+
if (Options.Data.Context.FileScopedNamespace)
3328
{
29+
CodeBuilder.AppendLine(";");
3430
GenerateClass();
3531
}
32+
else
33+
{
34+
CodeBuilder.AppendLine();
35+
CodeBuilder.AppendLine("{");
3636

37-
CodeBuilder.AppendLine("}");
37+
using (CodeBuilder.Indent())
38+
{
39+
GenerateClass();
40+
}
41+
42+
CodeBuilder.AppendLine("}");
43+
}
3844

3945
return CodeBuilder.ToString();
4046
}

src/EntityFrameworkCore.Generator.Core/Templates/MapperClassTemplate.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using EntityFrameworkCore.Generator.Extensions;
33
using EntityFrameworkCore.Generator.Metadata.Generation;
44
using EntityFrameworkCore.Generator.Options;
@@ -33,15 +33,25 @@ public override string WriteCode()
3333

3434
CodeBuilder.AppendLine();
3535

36-
CodeBuilder.AppendLine($"namespace {_entity.MapperNamespace}");
37-
CodeBuilder.AppendLine("{");
36+
CodeBuilder.Append($"namespace {_entity.MapperNamespace}");
3837

39-
using (CodeBuilder.Indent())
38+
if (Options.Data.Context.FileScopedNamespace)
4039
{
40+
CodeBuilder.AppendLine(";");
4141
GenerateClass();
4242
}
43+
else
44+
{
45+
CodeBuilder.AppendLine();
46+
CodeBuilder.AppendLine("{");
4347

44-
CodeBuilder.AppendLine("}");
48+
using (CodeBuilder.Indent())
49+
{
50+
GenerateClass();
51+
}
52+
53+
CodeBuilder.AppendLine("}");
54+
}
4555

4656
return CodeBuilder.ToString();
4757
}

src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Globalization;
1+
using System.Globalization;
22
using System.Linq;
33
using EntityFrameworkCore.Generator.Extensions;
44
using EntityFrameworkCore.Generator.Metadata.Generation;
@@ -27,15 +27,25 @@ public override string WriteCode()
2727
CodeBuilder.AppendLine("using Microsoft.EntityFrameworkCore;");
2828
CodeBuilder.AppendLine();
2929

30-
CodeBuilder.AppendLine($"namespace {_entity.MappingNamespace}");
31-
CodeBuilder.AppendLine("{");
30+
CodeBuilder.Append($"namespace {_entity.MappingNamespace}");
3231

33-
using (CodeBuilder.Indent())
32+
if (Options.Data.Context.FileScopedNamespace)
3433
{
34+
CodeBuilder.AppendLine(";");
3535
GenerateClass();
3636
}
37+
else
38+
{
39+
CodeBuilder.AppendLine();
40+
CodeBuilder.AppendLine("{");
3741

38-
CodeBuilder.AppendLine("}");
42+
using (CodeBuilder.Indent())
43+
{
44+
GenerateClass();
45+
}
46+
47+
CodeBuilder.AppendLine("}");
48+
}
3949

4050
return CodeBuilder.ToString();
4151
}

src/EntityFrameworkCore.Generator.Core/Templates/ModelClassTemplate.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,25 @@ public override string WriteCode()
2121
CodeBuilder.AppendLine("using System.Collections.Generic;");
2222
CodeBuilder.AppendLine();
2323

24-
CodeBuilder.AppendLine($"namespace {_model.ModelNamespace}");
25-
CodeBuilder.AppendLine("{");
24+
CodeBuilder.Append($"namespace {_model.ModelNamespace}");
2625

27-
using (CodeBuilder.Indent())
26+
if (Options.Data.Context.FileScopedNamespace)
2827
{
28+
CodeBuilder.AppendLine(";");
2929
GenerateClass();
3030
}
31+
else
32+
{
33+
CodeBuilder.AppendLine();
34+
CodeBuilder.AppendLine("{");
3135

32-
CodeBuilder.AppendLine("}");
36+
using (CodeBuilder.Indent())
37+
{
38+
GenerateClass();
39+
}
40+
41+
CodeBuilder.AppendLine("}");
42+
}
3343

3444
return CodeBuilder.ToString();
3545
}

src/EntityFrameworkCore.Generator.Core/Templates/QueryExtensionTemplate.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,25 @@ public override string WriteCode()
2828

2929
var extensionNamespace = Options.Data.Query.Namespace;
3030

31-
CodeBuilder.AppendLine($"namespace {extensionNamespace}");
32-
CodeBuilder.AppendLine("{");
31+
CodeBuilder.Append($"namespace {extensionNamespace}");
3332

34-
using (CodeBuilder.Indent())
33+
if (Options.Data.Context.FileScopedNamespace)
3534
{
35+
CodeBuilder.AppendLine(";");
3636
GenerateClass();
3737
}
38+
else
39+
{
40+
CodeBuilder.AppendLine();
41+
CodeBuilder.AppendLine("{");
3842

39-
CodeBuilder.AppendLine("}");
43+
using (CodeBuilder.Indent())
44+
{
45+
GenerateClass();
46+
}
47+
48+
CodeBuilder.AppendLine("}");
49+
}
4050

4151
return CodeBuilder.ToString();
4252
}

src/EntityFrameworkCore.Generator.Core/Templates/ValidatorClassTemplate.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using EntityFrameworkCore.Generator.Extensions;
1+
using EntityFrameworkCore.Generator.Extensions;
22
using EntityFrameworkCore.Generator.Metadata.Generation;
33
using EntityFrameworkCore.Generator.Options;
44

@@ -25,15 +25,25 @@ public override string WriteCode()
2525

2626
CodeBuilder.AppendLine();
2727

28-
CodeBuilder.AppendLine($"namespace {_model.ValidatorNamespace}");
29-
CodeBuilder.AppendLine("{");
28+
CodeBuilder.Append($"namespace {_model.ValidatorNamespace}");
3029

31-
using (CodeBuilder.Indent())
30+
if (Options.Data.Context.FileScopedNamespace)
3231
{
32+
CodeBuilder.AppendLine(";");
3333
GenerateClass();
3434
}
35+
else
36+
{
37+
CodeBuilder.AppendLine();
38+
CodeBuilder.AppendLine("{");
3539

36-
CodeBuilder.AppendLine("}");
40+
using (CodeBuilder.Indent())
41+
{
42+
GenerateClass();
43+
}
44+
45+
CodeBuilder.AppendLine("}");
46+
}
3747

3848
return CodeBuilder.ToString();
3949
}

0 commit comments

Comments
 (0)