17
votes

What naming conventions are you using for namespaces and sponsor classes? (i.e. the classes that hold extension method definitions)

Is there a standard/recommended .NET Framework naming convention? (the "Framework Design Guidelines, 2nd Edition" book only gives guidance on what namespaces not to use).

3

3 Answers

13
votes

I haven't seen any official recommendations, but I've been organizing my extension classes like [NameSpace].[ClassName]Extensions:

ProjectName.Web.Util.ControlExtensions
ProjectName.Data.Util.CollectionExtensions
7
votes

For the Namespace - I would focus on the standard framework guidelines for namespace names. Put the extension methods into a namespace where they will typically be used/associated meaningfully, and avoid having an extra namespace just for this.

For the sponsor class - in this case, it's fairly unimportant. I would try to pick a class name that is meaningful, but there does not seem to be a fixed guideline.

The important thing here, though, is that the sponsor class is never really directly used/seen by the user of your extension methods. As long as the namespace has been included, the extension method is found correctly. I personally use something very similar to jrummell for my extension methods, but Microsoft does not follow this in the Framework (a good example of this is the Enumerable class).

0
votes

For the file name and class name us the Extensions suffix. Example, if you want to create an extension method for the name Foo call it FooExtensions and place it in a file name called FooExtensions.cs.

If you have many extension classes place them into a Extensions directory.

I've seen Microsoft use the same namespace as the class they wish to extend. This way they're readily available and easily discoverable without having to import them into scope by through using statements.