-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pluralization Hook to Reverse Engineer
Introduces a new IPluralizer service that is used to singularize entity type names and pluralize DbSet names. The default implementation is a no-op, so this is just a hook where folks can easily plug in their own pluralizer. I left this separate from the existing CandidateNamingService, which is responsible for cleaning up the table name into a C# friendly identifier. Here is what it looks like for a developer to hook in their own pluralizer. Our code generation coverage is lacking, so I tested this against Northwind and verified by round tripping data. ```c# public class MyDesignTimeServices : IDesignTimeServices { public void ConfigureDesignTimeServices(IServiceCollection services) { services.AddSingleton<IPluralizer, MyPluralizer>(); } } public class MyPluralizer : IPluralizer { public string Pluralize(string name) { return Inflector.Inflector.Pluralize(name) ?? name; } public string Singularize(string name) { return Inflector.Inflector.Singularize(name) ?? name; } } ``` Resolves #3060
- Loading branch information
1 parent
ac1d6ba
commit 4dab5b6
Showing
15 changed files
with
271 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Microsoft.EntityFrameworkCore.Relational.Design/Infrastructure/IPluralizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Infrastructure | ||
{ | ||
/// <summary> | ||
/// Converts identifiers to the plural and singular equivalents. | ||
/// </summary> | ||
public interface IPluralizer | ||
{ | ||
/// <summary> | ||
/// Gets the plural version of the given identifier. Returns the same | ||
/// identifier if it is already pluralized. | ||
/// </summary> | ||
/// <param name="identifier"> The identifier to be pluralized. </param> | ||
/// <returns> The pluralized identifier. </returns> | ||
string Pluralize([CanBeNull] string identifier); | ||
|
||
/// <summary> | ||
/// Gets the singular version of the given identifier. Returns the same | ||
/// identifier if it is already singularized. | ||
/// </summary> | ||
/// <param name="identifier"> The identifier to be singularized. </param> | ||
/// <returns> The singularized identifier. </returns> | ||
string Singularize([CanBeNull] string identifier); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Microsoft.EntityFrameworkCore.Relational.Design/Internal/NullPluralizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Internal | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public class NullPluralizer : IPluralizer | ||
{ | ||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual string Pluralize(string identifier) | ||
{ | ||
return identifier; | ||
} | ||
|
||
/// <summary> | ||
/// This API supports the Entity Framework Core infrastructure and is not intended to be used | ||
/// directly from your code. This API may change or be removed in future releases. | ||
/// </summary> | ||
public virtual string Singularize(string identifier) | ||
{ | ||
return identifier; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rosoft.EntityFrameworkCore.Relational.Design/Metadata/ScaffoldingEntityTypeAnnotations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Scaffolding.Metadata | ||
{ | ||
/// <summary> | ||
/// Provides strongly typed access to scaffolding related annotations on an | ||
/// <see cref="IEntityType"/> instance. Instances of this class are typically obtained via the | ||
/// <see cref="ScaffoldingMetadataExtensions.Scaffolding(IEntityType)" /> extension method and it is not designed | ||
/// to be directly constructed in your application code. | ||
/// </summary> | ||
public class ScaffoldingEntityTypeAnnotations : RelationalEntityTypeAnnotations | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ScaffoldingEntityTypeAnnotations"/> class. | ||
/// Instances of this class are typically obtained via the | ||
/// <see cref="ScaffoldingMetadataExtensions.Scaffolding(IEntityType)" /> extension method and it is not designed | ||
/// to be directly constructed in your application code. | ||
/// </summary> | ||
/// <param name="entity"> The entity type to access annotation on. </param> | ||
public ScaffoldingEntityTypeAnnotations([NotNull] IEntityType entity) | ||
: base(entity, ScaffoldingFullAnnotationNames.Instance) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets or set the name of the <see cref="DbSet{TEntity}"/> property for this entity type. | ||
/// </summary> | ||
public virtual string DbSetName | ||
{ | ||
get { return (string)Annotations.GetAnnotation(ScaffoldingFullAnnotationNames.Instance.DbSetName, null); } | ||
[param: CanBeNull] | ||
set | ||
{ | ||
Annotations.SetAnnotation( | ||
ScaffoldingFullAnnotationNames.Instance.DbSetName, | ||
null, | ||
Check.NullButNotEmpty(value, nameof(value))); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/Microsoft.EntityFrameworkCore.Relational.Design.Tests/NullPluralizerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Xunit; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Relational.Design | ||
{ | ||
public class NullPluralizerTest | ||
{ | ||
[Fact] | ||
public void Returns_same_name() | ||
{ | ||
var pluralizer = new NullPluralizer(); | ||
var name = "blogs"; | ||
Assert.Equal(name, pluralizer.Pluralize(name)); | ||
Assert.Equal(name, pluralizer.Singularize(name)); | ||
} | ||
|
||
[Fact] | ||
public void Returns_same_name_when_null() | ||
{ | ||
var pluralizer = new NullPluralizer(); | ||
Assert.Equal(null, pluralizer.Pluralize(null)); | ||
Assert.Equal(null, pluralizer.Singularize(null)); | ||
} | ||
} | ||
} |
Oops, something went wrong.