Event Driven Classes - MetaPropertyHost

IMetaPropertyHost

The MetaPropertyHost class used in the Event Driven Properties - MetaProperty`1 section provides a simple way for you to conform to the IMetaPropertyHost interface defined in MetaProperties.Framework.

This interface is defined as follows:

    public interface IMetaPropertyHost : INotifyPropertyChanged
    {
        IEnumerable<IMetaProperty> MetaProperties { get; }
        
        IMetaProperty GetMetaProperty(string propertyName);
    }


IMetaPropertyHost provides a simple interface to access the exposed IMetaProperty instances for a class. This in turn gives a fast way of navigating class hierarchies without using reflection. In addition it implements the INotifyPropertyChanged interface which allows traditional data binding techniques to work.

In general when using MetaProperties you don't need to worry about this interface. However the provided implementation of this interface MetaPropertyHost can simplify how you hook up your exposed IMetaProperty instances to your class' PropertyChanged event.

MetaPropertyHost

The section Event Driven Properties - MetaProperty`1 showed how you can automatically or manually hook up your publicly exposed IMetaProperty`1 instances to your class by deriving from MetaPropertyHost:

    public class Cat : MetaPropertyHost
    {
        private readonly Observable<string> observableName = new Observable<string>();

        public string Name
        {
            get { return this.observableName.Value; }
            set { this.observableName.Value = value; }
        }

        public IMetaProperty<string> NameProperty
        {
            get { return this.observableName.MetaProperty; }
        }
    }


This is often as much information as you need to start using MetaProperties with data binding, and so we won't repeat it all here. However we shall now discuss some more advanced topics.

Automatic initialization without a base class

Sometimes it is not possible to derive from MetaPropertyHost because your class is already deriving from something else.

MetaProperties provides another class, MetaPropertyHostHelper, which provides a simple way to implement IMetaPropertyHost including all the functionality of MetaPropertyHost without deriving from a particular base class.

The code below shows its usage:

    // We no longer derive from MetaPropertyHost, but instead 
    // implement IMetaPropertyHost.
    public class Example : IMetaPropertyHost
    {
        // This instance will help us implement IMetaPropertyHost.
        private readonly MetaPropertyHostHelper metaPropertyHostHelper;

        public Example()
        {
            // Initialize the helper class.
            this.metaPropertyHostHelper = new MetaPropertyHostHelper(this);
            
            // Optionally automatically initialize any public MetaProperties.
            this.metaPropertyHostHelper.InitializePublicMetaProperties();
        }

        // Pass the event through to the helper.
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                this.metaPropertyHostHelper.PropertyChanged += value;
            }

            remove
            {
                this.metaPropertyHostHelper.PropertyChanged -= value;
            }
        }

        // Pass the property through to the helper.
        public IEnumerable<IMetaProperty> MetaProperties
        {
            get
            {
                return this.metaPropertyHostHelper.MetaProperties;
            }
        }

        // Pass the method through to the helper.
        public IMetaProperty GetMetaProperty(string propertyName)
        {
            return this.metaPropertyHostHelper.GetMetaProperty(propertyName);
        }
    }


Internally the MetaPropertyHost class also uses the MetaPropertyHostHelper.

Last edited Mar 9, 2010 at 1:15 PM by jamesthurley, version 2

Comments

No comments yet.