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

 ̄綄美尐妖づ 提交于 2019-12-02 20:03:52

We put them all in their own namespace Company.Common.Extensions. That way, if you have any of our extension methods, you have them all. Plus, at least at my shop, we don't have to worry about our developers not knowing about extension methods. I have the opposite worry, extension method overload! :)

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.

This is not a namespace problem it is a communication problem.

If these methods are useful you need to communicate this to the developers and, conversely, act on the feedback from them (with appropriate levels of judgement).

Placing anything into the System namespace is a recipe for disaster and confusion later. The only times you ever want to do this is to 'back port' functionality into older frameworks and then you probably shouldn't do it yourself but should use something like LinqBridge to do it.

Be wary of the desire to throw all extensions into one namespace unless they really are widely useful together. Some developers may find the wood lost for the trees if they are bombarded with everything and the kitchen sink via intellisense.

Keeping the namespace the company name is sensible in general to avoid confusion.

@Juri- If you think about it this is the same problem as developers knowing that class X exists in the .NET framework. Communication is key that all team members use the right classes, be they extension methods or some other helper.

As JP has stated, I often see extension methods in some kind of subfolder called Extensions. Hopefully when you state you use my.company.web.utils the namespace is actually Pascal cased?

Even if you put them in a good place there is no 100% guarantee that other developers will use them.

Presuming you use Visual Studio, one way would be to create a custom Class template (or modify the default one) so that whenever a developer creates a new class file it automatically has a using statement with your namespace(s). See Customize Visual Studio 2005 Templates for Coding Productivity.

Yes,i think put the Extension methods in own company namespce is best practices. put it in System namespace is a lazy operation

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).

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...

You can achieve what you want by putting extension methods in the global namespace. That's what I do and they're then available without needing any using statements.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!