You have a good start, but, as John said, he is currently not type safe; the converter does not have error checking to ensure that the decimal value it receives is Celsius.
So, to take this further, I would begin to introduce types of structures that take a numerical value and apply it to a unit of measure. In enterprise architecture templates (the so-called gang of four design templates), this is called the "Money" template after its most common use to indicate the amount of a currency type. The template is executed for any numerical sum that requires a unit of measure in order to make sense.
Example:
public enum TemperatureScale { Celsius, Fahrenheit, Kelvin } public struct Temperature { decimal Degrees {get; private set;} TemperatureScale Scale {get; private set;} public Temperature(decimal degrees, TemperatureScale scale) { Degrees = degrees; Scale = scale; } public Temperature(Temperature toCopy) { Degrees = toCopy.Degrees; Scale = toCopy.Scale; } }
Now you have a simple type that you can use to ensure that your transformations take a temperature that has the correct scale and return a result that is known to be on a different scale.
Your Funcs will need an extra line to verify that the input matches the output; you can continue to use lambdas, or you can do it one more step with a simple strategy template:
public interface ITemperatureConverter { public Temperature Convert(Temperature input); } public class FahrenheitToCelsius:ITemperatureConverter { public Temperature Convert(Temperature input) { if (input.Scale != TemperatureScale.Fahrenheit) throw new ArgumentException("Input scale is not Fahrenheit"); return new Temperature(input.Degrees * 5m / 9m - 32, TemperatureScale.Celsius); } }
Since these types of conversions are two-way, you might consider setting up the interface for processing in both directions using the "ConvertBack" method or a similar method that will take the temperature in Celsius and convert to degrees Fahrenheit. This reduces your class. Then, instead of class instances, your dictionary values can be pointers to methods on converter instances. This complicates the setup of the main StrategyConverter choice somewhat, but reduces the number of conversion strategy classes that you must define.
Also note that error checking is performed at run time when you are actually trying to do the conversion, requiring that this code be thoroughly tested in all cases of use to ensure that it is correct. To avoid this, you can get a base temperature class to create CelsiusTemperature and FahrenheitTemperature structures that simply define their scale as a constant value. Then the ITemperatureConverter could be made common to two types: temperature, giving you a compile-time check when you specify the transformation that you think. A temperature converter can also be made to dynamically search for ITemperatureConverters, determine the types that they will convert between them, and automatically configure the converter dictionary, so you don’t have to worry about adding new ones. This is due to an increase in the number of classes based on temperature; you will need four domain classes (base and three derived classes) instead of one. It will also slow down the creation of the TemperatureConverter class, as the code for smoothly creating the converter dictionary will use quite a bit of reflection.
You can also change the enumerations so that the units become "marker classes"; empty classes that have no meaning except that they belong to this class and come from other classes. Then you can define a complete hierarchy of UnitOfMeasure classes that represent different units of measure, and can be used as arguments and constraints of a general type; An ITemperatureConverter can be common to two types, both of which are limited to TemperatureScale classes, and the CelsiusFahrenheitConverter implementation closes the common interface for the CelsiusDegrees and FahrenheitDegrees types derived from TemperatureScale. This allows you to set units of measure on their own as conversion restrictions, which in turn allows conversions between unit types (certain units of certain materials have known conversions, 1 British imperial water pin weighs 1.25 pounds).
All these are design decisions that will simplify one type of changes in this design, but at a certain price (either to do something even more difficult than to do or reduce the performance of the algorithm). It's up to you to decide what is really “easy” for you, in the context of the general application and coding environment in which you work.
EDIT: The use you want from your edit is extremely simple for temperature. However, if you want the universal UnitConverter to work with any UnitofMeasure, you no longer want Enums to display your units of measure because Enums cannot have their own inheritance hierarchy (they are derived directly from System.Enum).
You can specify that the default constructor can accept any Enum, but then you need to make sure that Enum is one of the types that is the unit of measure, otherwise you could pass the DialogResult value and the converter will freak out at runtime.
Instead, if you want one UnitConverter that can convert to any UnitOfMeasure given by lambdas for other units, I would specify units as "marker classes"; small stateless "tokens" that make sense only in that they are their own type and come from their parents:
//The only functionality any UnitOfMeasure needs is to be semantically equatable //with any other reference to the same type. public abstract class UnitOfMeasure:IEquatable<UnitOfMeasure> { public override bool Equals(UnitOfMeasure other) { return this.ReferenceEquals(other) || this.GetType().Name == other.GetType().Name; } public override bool Equals(Object other) { return other is UnitOfMeasure && this.Equals(other as UnitOfMeasure); } public override operator ==(Object other) {return this.Equals(other);} public override operator !=(Object other) {return this.Equals(other) == false;} } public abstract class Temperature:UnitOfMeasure { public static CelsiusTemperature Celsius {get{return new CelsiusTemperature();}} public static FahrenheitTemperature Fahrenheit {get{return new CelsiusTemperature();}} public static KelvinTemperature Kelvin {get{return new CelsiusTemperature();}} } public class CelsiusTemperature:Temperature{} public class FahrenheitTemperature :Temperature{} public class KelvinTemperature :Temperature{} ... public class UnitConverter { public UnitOfMeasure BaseUnit {get; private set;} public UnitConverter(UnitOfMeasure baseUnit) {BaseUnit = baseUnit;} private readonly Dictionary<UnitOfMeasure, Func<decimal, decimal>> converters = new Dictionary<UnitOfMeasure, Func<decimal, decimal>>(); public void AddConverter(UnitOfMeasure measure, Func<decimal, decimal> conversion) { converters.Add(measure, conversion); } public void Convert(UnitOfMeasure measure, decimal input) { return converters[measure](input); } }
You can put an error check (check that the input block has the specified conversion, make sure that the conversion to be added is intended for the UOM with the same parent as the base type, etc.) as you see fit. You can also get a UnitConverter to create a TemperatureConverter, allowing you to add static compile-time checks and avoid the run-time checks that UnitConverter will have to use.