Press "Enter" to skip to content

Self Aware Objects adds the ability to assign default values during object instantiation

I’ve made more updates to my Self Aware Objects project.

Validation performance is improved by around 15-20%.  I’ve also added the ability to set up default property values, which are automatically set when you instantiate an object.

Here’s how you can use it.

First, create your class, using SAObject as its base class.

Second, for the properties you want to have a default value, add an SAO default applicator attribute.  In the sample below, the ID and Name properties have them.

using System;

using SAO;
using SAO.Attributes.Property;

namespace Engine.Model
    public class Customer : SAObject
        private string _name;

        public Guid ID { get; set; }

        [DefaultNullOrEmptyStringTo("Test User")]
        public string Name 
            get { return _name; }
                if(_name != value)
                    _name = value;

        [MinimumLength(5, "Address cannot be less than 5 characters long")]
        [MaximumLength(100, "Address cannot be more than 100 characters long")]
        public string Address { get; set; }

        [MinimumLength(2, "City cannot be less than 2 characters long")]
        [MaximumLength(100, "City cannot be more than 100 characters long")]
        public string City { get; set; }

        [ExactLengthString(2, "State must be two characters")]
        [ContainsOnlyLettersAttribute("State can only contain letters")]
        public string StateCode { get; set; }

        [MatchesRegEx(RegExConstants.CountrySpecific.US.ZIP_CODE, "ZIP Code must be formatted like '99999' or '99999-9999'")]
        public string ZIPCode { get; set; }

The ID property uses DefaultEmptyGuidToNewGuid, which will take care of giving every instance of the Customer class a unique ID value.

The Name property gets the value of “Test User” if its value on construction is null or an empty string.

The nice thing is that you don’t need to make any special calls to set these values (as you can see in the screenshot below, the properties are set immediately after instantiating the Customer object).

Self Aware Objects - Default property values set on instaantiation

Limits to setting defaults

Right now, I’m having trouble initializing collection properties.

The code can detect that it’s dealing with a property type that’s a collection.  However, initializing it to a collection of the proper type is proving difficult.

I’m putting that problem in my ‘parking lot’ right now, since I have more features to add that should be easier to get in place.

For now, it’s necessary to have a constructor in the class, where you set your collection properties to an empty collection of the proper type.

Binding notification

You may have noticed that the setter for the Name property has a call to OnPropertyChanged.  This is to update any bindings you have to the Customer object.  SAObject implements INotifyPropertyChanged, and has the required event handling code in place.

Validating your SAObject class

I’ve rearranged some of the code, so you can validate your class by calling the Validate() method from the SAObject base class.  Your class will be able to validate itself – hence the name Self Aware Object.

Upcoming release of Self Aware Objects project

I think this project is starting to become useful, and would like to get it out into the world.  So I should be releasing it sometime soon.

If you think this is something you’d use, please leave a comment below to let me know:

  1. What form you’d like to see it (DLL, full source code, etc.).
  2. How you’d like to see it available (download from my site, public SVN repository, GitHub, CodePlex, etc.).
Leave a Reply

Your email address will not be published. Required fields are marked *