One of the things I love about dynamic data is no matter what the nature of the data which you are dealing with the code will look the same. I also love the power of data transformations and just how easy it is to tame sequences of data. However a lot of the capability and power is not obvious at a glance so this is the start of a series of posts where step by step I will illustrate usage of the library. I have decided to go in at the deep end and demonstrate a trading system as these systems generally have rapidly moving data and loads of complexity. What I hope to achieve is to show that by using dynamic data your code can be simplified greatly.
Source code can be found here
We will start off with a simple trade object
public class Trade { public long Id { get; private set; } public string CurrencyPair { get; private set; } public string Customer { get; private set; } public decimal Price { get; set; } public TradeStatus Status { get; private set; } public Trade(long id, string customer, string currencyPair, TradeStatus status) { Id = id; Customer = customer; CurrencyPair = currencyPair; Status = status; } }
I want a service which exposes a load of trades.
public interface ITradeService { IObservableCache<Trade, long> Trades { get; } }
Implementing this is a trivial matter. For clarity not all code is shown.
public class TradeService : ITradeService { private readonly IObservableCache<Trade, long> _tradesCache; public TradeService() { //create a cache specifying that the unique key is id var tradesSource = new SourceCache<Trade, long>(trade => trade.Id); //call AsObservableCache() to hide the update methods as we are exposing the cache _tradesCache = tradesSource.AsObservableCache(); //code to emulate an external trade provider var tradeLoader = GenerateTradesAndMaintainCache(); //..... } public IObservableCache<Trade, long> Trades { get { return _tradesCache; } } }
We now have a trades service which I will be using in subsequent posts to demonstrate the functionality of dynamic data.