Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name Preprocessor propose #157

Closed
dukecheng opened this issue Sep 15, 2022 · 7 comments
Closed

Name Preprocessor propose #157

dukecheng opened this issue Sep 15, 2022 · 7 comments

Comments

@dukecheng
Copy link

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
Copy link
Member

roji commented Sep 15, 2022

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

@dukecheng
Copy link
Author

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
Copy link
Member

roji commented Sep 15, 2022

@dukecheng that sounds like a dup of #42

@dukecheng
Copy link
Author

Thanks for your quickly response,

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

@roji
Copy link
Member

roji commented Sep 15, 2022

Duplicate of #42

@roji roji marked this as a duplicate of #42 Sep 15, 2022
@roji
Copy link
Member

roji commented Sep 15, 2022

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.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2022
@dukecheng
Copy link
Author

Sounds good, I am looking forward to it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants