Best practices: C# Extension methods namespace and promoting extension methods

后端 未结 10 1014
南方客
南方客 2021-02-02 06:43

I know there exists already a post, describing nearly the same, but I think mine is a bit different.

What I would like to know is how you organize your extension methods

相关标签:
10条回答
  • 2021-02-02 07:18

    We put everything into the same Namespace and Class, however we use partial classes to keep them organized.

    For example:

    ExtensionMethods-String.cs

    ExtensionMethods-DataObject.cs

    ExtensionMethods-Debug.cs

    ...etc all have partial classes...

    0 讨论(0)
  • 2021-02-02 07:23

    I'm dumb, lazy and minimalistic, so I put them at the same namespace as the type they extend. In this way there is no need for extra using statements, documentation or emailing about them (Winston).

    0 讨论(0)
  • 2021-02-02 07:24

    I like the way ReSharper solves this problem.

    ReSharper discovers any available extension methods, even without the corresponding usings. In case the using is not present, Intellisense also shows the namespace where the extension resides, making clear where the extension comes from and indicating that selecting it will add the using. (Example below.)

    Naturally, only namespaces reachable by the current project, i.e. directly or indirectly referenced, are included.

    Here is an example of what Intellisense might show if there are two extension methods. The first one comes from a namespace that we have already included. The second comes from a namespace that we have not (yet) included.

    • AddMvc
    • AddEntityFrameworkSqlServer (Microsoft.Extensions.DependencyInjection)
    0 讨论(0)
  • 2021-02-02 07:26

    The problem here is not the naming of the namespace, it's the lack of documentation and education of your developers.

    Put them in whatever namespace makes sense, write a wiki article documenting all your extension methods, then send an email to your developers with a link to the wiki article.

    0 讨论(0)
提交回复
热议问题