Simple Rule Editor goes to BitBucket

About a year ago I wrote a blog post where I described a simple WYSIWIG rule editor that can be used to manage rules applied using WF rule engine. After that I received a few responses with comments and questions about using WF rule engine in .NET applications. Apparently this technique was considered useful in various projects, and I even presented it on a recent NNUG session in Oslo.

Few days ago I received a mail from one developer who complained that my rule enging wrapper was unable to handle Nullable<T> types. I never invested enough time to make the wrapper handle such issues, the whole thing was more like a demonstration of a concept. However, since it is now used by others, I decided to have more solid approach to it and moved the project to BitBucket where it’s source can be obtained from here. I also updated WF rule engine wrapper to correctly handle converstion to nullable types. This is a common problem. Strings can not be converted to a generic Nullable<T> type using Convert.ChangeType call. I had to modify RyleType.SetPropertyValue method that now looks like this:

public void SetPropertyValue(RuleObject ruleObject, int index, object value)
{
    object propertyValue;
    if (this.Properties[index].Type.IsEnum)
    {
        propertyValue = Enum.Parse(this.Properties[index].Type, value.ToString());
    }
    else if (this.Properties[index].Type.IsGenericType && this.Properties[index].Type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
    {
        System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(this.Properties[index].Type);
        propertyValue = System.Convert.ChangeType(value, nullableConverter.UnderlyingType);
    }
    else
    {
        propertyValue = Convert.ChangeType(value, this.Properties[index].Type);
    }
    this.Properties[index].PropertyInfo.SetValue(ruleObject.PropertyObjects[this.Properties[index]], propertyValue, null);
}

Note the block that handles generic types. It doesn’t seem that conversion from string can be handles easier.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s