Home > Exploring the CodeFluent Runtime > Exploring the CodeFluent Runtime: AutoObject

Exploring the CodeFluent Runtime: AutoObject


Following our “Exploring the CodeFluent Runtime“ post series, I thought I’d share with you another cool class available in the CodeFluent Runtime which is the AutoObject class.

The AutoObject class is a light class which implements INotifyPropertyChanged and IDataErrorInfo so you can easily and quickly create classes data-binding and validation friendly classes.

Here’s how it works: say you need a small technical class for UI purposes. You want it to support data-binding and to raise an event when a property is changed, plus since your working in the UI, you might at some point need to validate user input. Instead of implementing those interfaces myself, I can derive from the AutoObject class which will provide those features for me.

Data-Binding (INotifyPropertyChanged):

Across .NET platforms (e.g. Windows Forms, WPF) a lot of .NET controls provided by Microsoft or third parties (e.g. Infragistics) use this interface to update the UI to reflect changes in the model. Here’s how to use the AutoObject to benefit from those features:

public class CustomerLight : AutoObject
{
    public CustomerLight()
    {
    }

    public string FullName
    {
        get
        {
            return GetProperty();
        }
        set
        {
            SetProperty(value);
        }
    }

    public string FullAddress
    {
        get
        {
            return GetProperty();
        }
        set
        {
            SetProperty(value);
        }
    }

}

And that’s it! Since the AutoObject implements the INotifyPropertyChanged interface for me, I can data-bind my UI to it easily. In fact, here’s a sample console application which prints received PropertyChanged events on the command line:

class Program
{
    private static CustomerLight _cv;

    static void Main(string[] args)
    {
        _cv = new CustomerLight();
        _cv.PropertyChanged += new PropertyChangedEventHandler(OnCustomerLightPropertyChanged);

        Console.WriteLine("Type-in customer full name:");
        _cv.FullName = Console.ReadLine();
        Console.ReadKey();
    }

    static void OnCustomerLightPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        Console.WriteLine("PropertyChanged: " + e.PropertyName);
    }
}

Here’s the result:

Type-in customer full name:
John Doe
PropertyChanged: FullName

Data Validation (IDataErrorInfo):

It’s exactly the same as for INotifyPropertyChanged, lots of UI control provide nice UI validation features if you implement this interface, and here’s how to do it using the AutoObject:

public class CustomerLight : AutoObject
{
    public CustomerLight()
    {
    }

    public string FullName
    {
        get
        {
            return GetProperty();
        }
        set
        {
            SetProperty(value);
        }
    }

    public string FullAddress
    {
        get
        {
            return GetProperty();
        }
        set
        {
            SetProperty(value);
        }
    }

    protected override void Validate(IList errors, string memberName)
    {
        // If no memberName, validate all members.
        if (string.IsNullOrEmpty(memberName) || memberName == "FullName")
        {
            if (FullName.IndexOfAny(new char[] { '!', '*', '@', '?' }) != -1)
            {
                errors.Add("Invalid name: the following characters are considered invalid: '!*@?'.");
            }
        }
    }
}

And we’ll update our console application to retrieve the validation error and print it if any:

static void OnCustomerLightPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    Console.WriteLine("PropertyChanged: " + e.PropertyName);

    string error = ((IDataErrorInfo)_cv).Error;
    if (string.IsNullOrEmpty(error))
        return;

    Console.WriteLine(error);
}

And here’s the result:

Type-in customer full name:
test*test
PropertyChanged: FullName
Invalid name: the following characters are considered invalid: ‘!*@?’.

Hope this helps,

Carl Anderson

  1. No comments yet.
  1. No trackbacks yet.

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