tags:

views:

356

answers:

3

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?

A: 

Where are you getting the unit of measurement from? I can see the enum, but I don't see any reference to it elsewhere in the API.

Assuming you do get it from somewhere, the easiest solution may well be a switch statement. It's a bit ugly, but:

  • It'll work
  • It's easy to understand
  • It'll be fast

I'm still not entirely convinced I understand everything about the question though - particularly as you haven't explained what the other objects will be used for after casting.

Jon Skeet
I added a follow up answer to your questions....
Michael Kniskern
+3  A: 

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:

public class CastableObject {

    private UnitOfMeasurement eUnit; // Assign this somehow

    public static implicit operator int(CastableObject obj) 
    {
        if (obj.eUnit != UnitOfMeasurement.Numeric)
        {
            throw new InvalidCastException("Mismatched unit of measurement");
        }
        // return the numeric value
    }

    // Create other cast operators for the other unit types
}
JSBangs
I can not find the ClassCastException exception type in the .NET framework. What is the C# equialvant of the ClassCastException?
Michael Kniskern
Actually, I found it.....InvalidCastException
Michael Kniskern
I used this solution for casting the object to the appropriate type...Thanks for your input.
Michael Kniskern
Sorry about that... ClassCastException is the Java name. I have updated the original answer.
JSBangs
A: 

The Unit of Measurement enum is contained within the approval routing service. To elaborate more on the Rule class, it is used as a base class for all the other types of rules. It is loosely based on the flow engine of the NxBRE business rule engine NxBRE Home page.

Here is an example of the LessThanRule class (I modified the original question to reflect the correct functionality):

public class LessThanRule : Rule
{
    private bool m_Result = false;
    private object m_ObjectToCompare = null;
    private object m_ObjectToEvaluate = null;

    public bool Result
    {
        get { return this.m_Result; }
    }

    public object ObjectToCompare
    {
        get { return this.m_ObjectToCompare; }
        set { this.m_ObjectToCompare = value; }
    }

    public object ObjectToEvaluate
    {
        get { return this.m_ObjectToEvaluate; }
        set { this.m_ObjectToEvaluate = value; }
    }

    public override void ExecuteRule()
    {
        if (((IComparable)this.m_ObjectToEvaluate).CompareTo(this.m_ObjectToCompare) < 0)
            this.m_Result = true;
    }
}

Hope this Helps....

Michael Kniskern