Monday, August 18, 2008

StockSample: The Model's Interfaces

This post is part of my series on the Windows Forms Model View Presenter with Data Binding sample program I wrote I call StockSample.

One of the big things that is enabled by following the Model View Presenter pattern is "persistence ignorance".  For the model, I like to have POCO (Plain Old CLR Object) classes that represent the logical model that makes the most sense for the application.  This logical model might be different from the physical model if it makes the application easier to write.  Each of the model objects will also implement an interface that covers all the relevant publicly accessible properties, methods and events on the model types.  Interfaces will help with testability during development as you can create simpler test classes or use a mocking framework to create the desired test behavior.

In the current StockSample snapshot (20080818), I have one model, the Stock.  It has the following interface:

   1:  public interface IStock
   2:  {
   3:      string Name { get; }
   4:      string TickerSymbol { get; }
   5:      decimal CurrentPrice { get; }
   6:      DateTime LastUpdate { get; }
   7:  }

The Stock is special in that most of the application will not be modifying the stock information, but will be listening to changes to the stock.  I didn't make this interface inherit from INotifyPropertyChanged because the model class could use an alternative trigger for property change notifications (PropertyNameChanged events, for example).  The stock information will be populated and updated by the IStockProvider:

   1:  public interface IStockProvider
   2:  {
   3:      IStockList Stocks { get; }
   4:  }

Now that I think about it, I suppose a better name would be StockListProvider, I'll have to make those tweaks later.  The IStockList represents a collection of IStock objects:

   1:  public interface IStockList : IBindingList
   2:  {
   3:      IStock CreateNew(string tickersymbol, string name);
   4:      void Add(IStock stock);
   5:      IStock FindByTickerSymbol(string tickersymbol);
   6:      new IStock this[int index] { get; }
   7:  }

This interface has some more meat to it.  I inherit from IBindingList because I want to make sure we can bind this collection.  IBindingList is a bundle of interfaces that is necessary for binding a collection to places where a DataSet or DataView would typically go.  I'll go into more details about this later.

So, when dealing with the Model layer looking purely at the interfaces, we start with an IStockProvider.  The implementation of this is not important, all we care is that it provides is with an IStockList.  This list contains all the IStock instances that we will be able to use in our application.  The implementation of the IStockProvider will allow us to determine how we will actually get updated stock information.  The IStockList will tell us how many stocks there are and allow us to iterate on the list, and finally, the IStock will give us information on the stocks themselves.

Technorati Tags: ,,

No comments: