efcore / EFCore.NamingConventions

Entity Framework Core plugin to apply naming conventions to table and column names (e.g. snake_case)
Apache License 2.0
738 stars 74 forks source link

Name Preprocessor propose #157

Closed dukecheng closed 2 years ago

dukecheng commented 2 years ago

I usually like to add a suffix for Persist Object class naming, example for UserEntity, and expected table name User, Class Name: UserEntity => TableName: User

So is there have a name preprocessor for this let me to remove the suffix when convert the name, If doesn't have yet, I can commit a pr to support the NamePreprocessor, do you have any suggest?

roji commented 2 years ago

@DukeCheng what's the rule here? When is which suffix supposed to be removed?

dukecheng commented 2 years ago

I want to let the developer customize the rule, Example for:

public class CustomizeNamePreprocessor : INamePreprocessor
{
    public const string TableNameSuffix = "Entity";
    public const string ViewNameSuffix = "ViewEntity";
    public string TableName(string name)
    {
        return name.EndsWith(TableNameSuffix) ? name.Substring(0, name.Length - TableNameSuffix.Length) : name;
    }

    public string ViewName(string name)
    {
        return name.EndsWith(ViewNameSuffix) ? name.Substring(0, name.Length - ViewNameSuffix.Length) : name;
    }
}

I suggest to add a NamingConventionOptions to configure it, and pass it into the Use extensions class

public class NamingConventionOptions
{
    public INamePreprocessor NamePreprocessor { get; set; } = new NullNamePreprocessor();
}
public static DbContextOptionsBuilder UseSnakeCaseNamingConvention(
    [NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null, Action<NamingConventionOptions> options = null)
{
    Check.NotNull(optionsBuilder, nameof(optionsBuilder));

    NamingConventionOptions conventionOptions = new NamingConventionOptions();
    options?.Invoke(conventionOptions);

    var extension = (optionsBuilder.Options.FindExtension<NamingConventionsOptionsExtension>()
            ?? new NamingConventionsOptionsExtension())
        .WithSnakeCaseNamingConvention(conventionOptions);

    ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

    return optionsBuilder;
}
roji commented 2 years ago

@DukeCheng that sounds like a dup of #42

dukecheng commented 2 years ago

Thanks for your quickly response,

Yes, looks there are same, Is anybody work on this for now and how is the progress?

roji commented 2 years ago

Duplicate of #42

roji commented 2 years ago

Nobody's working on that for now. I'm heads-down working on EF Core 7.0 and related urgent things, typically shortly before or after the release I do a work cycle on this plugin (at the very least to make it compatible with the new EF Core release). I might be able to do that as part of that.

dukecheng commented 2 years ago

Sounds good, I am looking forward to it