I am currently developing an approval routing WCF service that will allow an user to create ‘rules’ which determine how an request is routed. The route is determined by comparing the ‘ObjectToEvaluate’ property of the Request class against the ‘ObjectToEvaluate’ property of the ‘Rule’ class. The ‘UnitOfMeasurement’ enum determines how to cast the ‘ObjectToEvaluate’ property for each class.
public enum UnitOfMeasurement { Currency = 1, Numeric = 2, Special = 3, Text = 4, } public class Request { public object ObjectToEvaluate { get; set; } } public class Rule { public object ObjectToEvaluate { get; set; } public virtual void ExecuteRule() { //logic to see if it passes the rule condition } }
What would be the best way to implement the method to cast the ‘ObjectToEvaluate’ property using the ‘UnitOfMeasurement’ enum?
Use an implicit type operator that checks the value of the enum. That way callers can transparently assign the objects to the types you want to represent them. Eg: