Creating a Basic Model

  • Create a public interface extending ISuperModel (or simply INotifyPropertyChanged)
  • Declare your data items as properties with getters and setters.
  • Invoke the SuperModel factory with your interface specification

   public interface IAwesomeViewModel : ISuperModel
   {
      int Value { get; set; }
      string Message { get; set; }
   }

   // NOTE: we can put interceptors around this model, but we can't use the nice [MarksDirty] attribute.
   public interface ISlightlyLessAwesomeButStillPrettyCoolViewModel : INotifyPropertyChanged
   {
       int FunkinessLevel {get; set;}
   }

   ...

   var factory = new SuperModelFactory();
   var myModel = factory.Create<IAwesomeViewModel>();

Checking A Property for Changes Before Update/Notification

  • Apply a BeforePropertySet attribute to your property
  • Insert the CheckIfPropertyChanged filter into the attribute.

   public interface ICheckForChanges : ISuperModel
   {
      // This property will fire PropertyChanged after an equality check.
      [BeforePropertySet(typeof(CheckIfPropertyChanged))]
      int Value { get; set; }
      
      // This property will always fire PropertyChanged events on Set
      int OtherValue { get; set; }
   }

Dirty-Marking Models

  • Declare a boolean property on your interface named "IsDirty" with a getter and setter.
  • (optional) Use the MarksDirty attribute on the interface to have all properties mark the model as dirty.
  • (optional) Use the MarksDirty attribute on properties to have properties opt in or out of dirty-marking.
  • Whenever properties are updated, IsDirty will be set to true.
  • Setting the IsDirty property to false will not mark the model as dirty.

   [MarksDirty]
   public interface IMarkDirty : ISuperModel
   {
      // The dirty flag.
      bool IsDirty { get; set; }

      // Since the interface marks dirty, this member will mark dirty.
      int MyValue { get; set; }

      // Will not mark the model as dirty.
      [MarksDirty(false)]
      int MyOtherValue { get; set; }
   }

Subscribing to Model Events

  • Use the Subscribe method on ISuperModel to get update notification.

   ...
   var model = modelFactory.Create<IAwesomeViewModel>();
   model.Subscribe(() => model.Value, () => HandleValueChanged(model));
   ...
   private void HandleValueChanged(IAwesomeModel model)
   {
      ...
   }

Class-Based Models

  • Extend BaseSuperModel or BaseNotifyPropertyChanged to write your own custom model!
    public class ExtendoModel : BaseSuperModel, IMyModelInterface
    {
        private int _value;
        public int Value
        {
            get{ return _value; }
            set 
            {
                 _value = value;
                NotifyByName("Value");
            }
        }
 
        public void IKnowIShouldntMixModelsAndBusinessLogicButOhWell()
        {
            ...
        }
    }

    var model = new ExtendoModel();
    ...

Declaratively Customizing Behavior with Filter Attributes

TBD

Last edited Mar 16, 2010 at 10:47 PM by darthtrevino, version 11

Comments

No comments yet.