views:

1178

answers:

6

Hi,

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 in terms of assigning the namespace. Currently - for the extension methods in our framework - I use the following namespace pattern

  • MyCompany.Web.Utils

and inside I have the extension method classes. This is fine for me with the disadvantage that the extenders are not immediately visible to our software developers. Consider the case where I have a StringExtender class which provides a quite handy extension method "In" that extends the String object. Having the extension method withing the above mentioned namespace, our programmers won't see the extension method unless they explicitly include its namespace. Instead, if I would put the extension method in the System namespace, everyone would immediately see it, but I've read that this is bad practice.

So my question is how you do promote your extension methods s.t. they are used by your developers.

+9  A: 

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! :)

JP Alioto
(+1) I agree, we use a namingconvention where we add .extensions at the end.
Johan Leino
+1. Good point about extension method overload.
RichardOD
That's basically what I'm doing already. So I guess it is the best way to continue like that. Thx
Juri
+1 about extension overload but at least you have a common namespace, so discovery of whats available is very easy.
Ralph Willgoss
+1  A: 

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

RichardOD
yes, the name is pascal cased...didn't pay attention on that when posting it here :)
Juri
+6  A: 

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.

Winston Smith
Yes that was my other option. Lack of communication is often an issue in software development teams. We are already in the process of setting up a wiki, but it is often quite difficult, being under time pressure and not getting supported on this kind of activities by the management. I guess people know what I'm talking about.
Juri
I feel your pain. Sometimes I find it's better to go ahead and do these things, get people using them and THEN tell the management :)
Winston Smith
Same point as my answer, but +1 for mentioning using a wiki.
RichardOD
+1 for the use of wiki and developer education.
Ralph Willgoss
+1  A: 

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.

Dan Diplo
+2  A: 

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.

ShuggyCoUk
"Be wary of the desire to throw all extensions into one namespace unless they really are widely useful together."We have a quite limited number of extension methods and are also trying to keep them small. So this shouldn't pose a problem.
Juri
+1  A: 

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

dushouke