ASP.NET forex control

Previous page: The data retrieval classes.

Currency exchange rates are provided by the FXDataSource control.  This derives from System.Web.UI.DataSourceControl so you can use it to drive data bound controls like GridView.  It is a very simple control to use because it takes no inputs.  Internally it uses the FxTopScraper class to pull currency exchange rates from a page on fxtop.com.  This page provides all the exchange rates between nine currencies: US dollar, euro, yen, sterling,  Canadian dollar, Swiss franc, South African rand, Australian and New Zealand dollars.  It also records exchange rates for "GBX" (sterling pence) - this is simply one hundredth of the "GBP" rates.

Since the source data is updated infrequently (I think daily), it makes sense for our control to cache it.  You control the caching with these properties:

  • EnableCaching - set this to "true" to enable the control to cache the rate data in the ASP.NET application cache.  The default is false.
  • CacheDuration - the time, in seconds, for which data is to be retained in the cache.  The default is zero.

But FXDataSource is more than just a data source.  It also exposes a ConvertAmount method that a developer can use to convert an amount from one currency to another.  I don't know if this 'dual-use' control style appears in the ASP.NET book of best practice, but it seems reasonable to me.  In this way, I have made FXDataSource useful to two different audiences.  If you want to display exchange rates without writing any code then use it as a data source.  If you want to use it to provide functionality then ignore the data source functionality and just call ConvertAmount.

The declarative use of the control looks like this:

<cgjfin:FXDataSource runat="server" ID="fxdata" EnableCaching="true" CacheDuration="300" />

Next page: The AssetPricesDataSource control

©2009 Carl Johansen