Archive

Archive for September, 2009

Welcome to the Machine – Introducing PropertyTree

September 21, 2009 1 comment

In this first post I will introduce you to PropertyTree, a data structure that I developed to handle the problem of adding custom fields to a record in a database and that has also proven very useful for client/server communication, validation and filling a whole host of other needs.

The Problem

It all started when I began developing a Business Process Modeling framework to replace the old Workflow framework that is the platform for all applications developed by the company I currently work for.

One of the features of the current framework is the ability to store arbitrary bits of information for a given Activity Instance in a SQL Server database. To do this, for each Activity Instance record in the activity_instance table, one or more data items are stored in a separate activity_instance_items table. This proves to be a problem when you need to retrieve the data items because you have to pivot the data to produce a package with all the required data bits.

The other problem is that the data is stored as a string in a varchar(8000) so on retrieval the programmer needs to cast the string into the original data type and the only way of knowing the appropriate data type is to inspect the data records directly or treat them all as string, something that happens quite a lot.

The Solution

As I designed the new framework I figured that a better way would be to store all the data items of an Activity Instance directly in it’s record in a XML field. That would keep all data items together with there respective instance in the same table. Also, since we are using XML, now we have the ability to store items hierarchically along with meta-data to describe each item.

And So PropertyTree Was Born

A PropertyTree allows you to store data items kind of like the old PropertyBag of early Visual Basic (pre .Net) or the Session-State Store in ASP.Net.

Data items are stored as a collection of nodes from a root node and each node itself can have a collection of nodes allowing for a tree like structure to be formed.

Each node has meta-data to indicate, among other things, the data type of it’s stored data, formatting information and validation rules. You can also add your own meta-data items through custom attributes.

A PropertyTree also exposes events for changes in values of properties and attributes as well as when validating value assignments and for handling validation errors.

Features

This is a list of features that are exposed by the PropertyTree:

  • Hierarchical collection of data items (PropertyNodes).
  • PropertyNodes contain strongly-typed values.
  • PropertyNodes can have meta-data in the form of attributes.
  • Validation information can be defined for a PropertyNode like for example:
    • Minimum value
    • Maximum value
    • Minimum and maximum length
    • A regular expression mask of allowed characters
    • A list of allowed values
  • These validation rules can be applied to a single PropertyNode or in Types that can be reused on any number of nodes.
  • An output format for a PropertyNode can be defined.
  • Events are raised for:
    • After a change in PropertyNode value (PropertyChanged event)
    • After a change in an attribute of a PropertyNode (PropertyAttributeChanged event)
    • When non-existing PropertyNode is referenced (PropertyNotFound event)
    • A hook to allow custom validation to be performed (ValidateValue event)
    • When a value assigned to a PropertyNode has caused a validation error (ValidationException event)
  • A PropertyTree can be used on a client of a web app using a JavaScript implementation that has also been created.

     

What’s Next?

In future posts I’ll dive deeper into each of these features starting with how to create an instance of a PropertyTree and add nodes with validation.

Digg This
Categories: PropertyTree Tags:

Careful with That Axe, Eugene – Using ToString() with a .Net Decimal Type

September 11, 2009 Leave a comment

A Problem With Decimals

Yesterday I ran into a problem with the ToString() function on a .Net Decimal type.

Actually the problem was mine but it revealed something I didn’t know about Decimal types that actually IS in the documentation but no one really reads the documentation, do they? Well they do when they run into problems.

Try the following in a .Net console application (C# or VB.Net or whatever you prefer):

    decimal dec1 = 10.0M;
    decimal dec2 = Decimal.Parse("10.00");
    Console.Write("dec1 == dec2 : ");
    Console.WriteLine(dec1 == dec2);
    Console.Write("dec1.ToString() == dec2.ToString() : ");
    Console.WriteLine(dec1.ToString() == dec2.ToString());

The output is the following:

dec1 == dec2 : True

dec1.ToString() == dec2.ToString() : False

Now, both dec1 and dec2 contain a decimal 10 value so mathematically they are equal, but the output of ToString() for each of them is:

dec1.ToString() : 10.0

dec2.ToString() : 10.00

So you see the strings are not identical.

The Reason Behind It

The reason that the two ToString() calls are not equal is because Decimal preserves the position of the decimal point so when we parse the string “10.00” both zeros to the right of the decimal point are kept.

This is all detailed in the MSDN documentation on the Decimal structure :

The scaling factor also preserves any trailing zeroes in a Decimal number. Trailing zeroes do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeroes can be revealed by the ToString method if an appropriate format string is applied.

I stumbled upon this while working on the Serialize/De-serialize functions of PropertyTree a data structure that I will post about soon (really).

Technorati Tags: ,
Categories: .Net, Programming Tags:
Follow

Get every new post delivered to your Inbox.