views:

177

answers:

3

I am creating a conversion class for frequency, and I wanted to allow my team to be able to add additional conversions when they are needed.

Frequency myFrequency = new Frequency(100, MHz);
double value = myFrequency.InKhz();

The source code for the class will not be include in future projects, so I will either have to have the class be a partial class or the additional conversions will need to be extensions. An example would be adding a conversion to GHz

myFrequency.InGHz()

Which is the best way to proceed with this?

Update: After reading Randolpho's answer I am going with the extension method approach. As time allows the extensions will get rolled into the base code, but I didn't want to make the other team members wait for updated assemblies and this allows them to move on to the next task a little faster.

+2  A: 

Partial classes will not work unless you have the original source. If you are denying the developers access to that source, their only alternative is to use extension methods.

That said, you probably just want to have a process to update the library with new additions; it looks like they'll be rare.

Randolpho
Thanks. I have never tried using partial classes in the way I described, I just assumed they would work.
Robin Robinson
+1  A: 

Extension methods all the way. It will not limit you, and partial methods can only be used within the assembly, while extension methods can be declared anywhere.

Nick Berardi
That's what I was thinking, but some of my coworkers them extension methods are "evil".
Robin Robinson
Extension methods aren't evil by a long shot, but that doesn't mean you should solve all your problems with them.
Randolpho
extension methods are just compiler/syntax magic to automatically map static methods to already created objects. extension methods shouldn't be over used for such things as to replace normal methods. But they should be used to extend the functionality of an object that you can no longer change.
Nick Berardi
A: 

If you want this to be extensible without compiling, you probably need to make a separate conversion class. This would allow you to specify the conversions at runtime and store them in some form of dictionary, but unfortunately will not have the same "built-in" langauge feeling as what you were typing.

Otherwise, extension methods are probably your best option - but they will require a recompile + new library anytime you want to add new conversions.

Reed Copsey