Single Bank Platform: The Coming Together of C# and F# (Part 1)


There’s a lot of talk about multi-language usage when building business solutions. It’s been common for years to have different languages on the client compared to the server. However, its not as common to have multi-language usage on the server. Server builds are often either 100% Java, C++ or .NET. In the last few years some banks have begun to allow Python or similar on the server. Likewise in the .NET space with the release from Microsoft of the DLR, dynamic languages are beginning to appear in solutions. Don Syme’s has been on a mission for sometime to evangelise the usage of F#. Don’s posting on Async and Parallel Design Patterns touches on scalable web servers. In the Java space Scala is much talked about by software engineers, but its still early days for the language uptake in the corporate space.

LMAX has over the last year or so help to increase the noise levels on various design patterns. There noise levels have been echoed in various other companies, including Lab49. One such noise that I’ve been grappling with over the last time period is the coming together of C# and F#. From a functional perspective I see the advantages, and from the work I’ve been involved in with regards to market risk, I see the benefits of writing the math’s centric code in a functional language. I’ve however taken more time to adjust to using F# and C# together to build full business solution. Axum (as previously blogged) was of great interest to me. With all this “noise” its clear (as it should be to many others) that we have for some time be at the point where we should be considering using different languages for different parts of the solution, not just from an architectural process perspective, but also within process.

All this has essentially lead me to build a very small simplistic Single Bank Platform, that runs within a single process (initially) 🙂 The codebase so far isn’t feature rich, and is very much a proof of concept. It does however offer some interesting ideas and thoughts on future direction. So to the code:

To allow me to fake an SDP I need a streaming server. So I’ve created a very simple implementation leveraging Rx to provide a channel bus:

    public class ChannelBus<T>
    {
        private readonly Subject<T> _channel = new Subject<T>();

        public IObservable<T> Channel { get {return _channel.AsObservable();} }

        public void Post(T payload)
        {
            _channel.OnNext(payload);
        }
    }

I obviously need a pricing engine, so here’s my “simple: C# version. I’ll add in bank tiers soon, couple with the obviously introduction of client tiers, which maybe mean I’ll add StreamInsight into the non-RIA SBP.

    public class PriceEngine
    {
        public IObservable<decimal> SourcePrices { get; set; }

        public PriceEngine()
        {
            SourcePrices = Observable.GenerateWithTime(
                true,
                _ => true,
                _ => _,
                count =>
                    {
                        decimal bidRate = Convert.ToDecimal(1.1*(0.5 + new Random().NextDouble()));
                        return bidRate;
                    },
                _ => TimeSpan.FromMilliseconds(1000*new Random().NextDouble())
                );
        }
    }

Here’s a start with F# F# Discriminated Unions:

type ProBufferMessage = {
    channel : string;
    probuf : byte[];
    }

type Agent<'T> = MailboxProcessor<'T>

type CommandMessages = ProBuffer of ProBufferMessage

type PriceMessage = 
    | Stop
    | Price of decimal 
and SupervisorMessage = Create of string | Created of string
and SessionMessage =  
    | StartRFS of string
    | StopRFS of string
    | RequestRFQ of string
    | StartedRFQ of string
and RFQConversationMessage =
    | CreateRFQ of string * Agent<SessionMessage>
    | RequestPrice of string
    | AcceptPrice
    | RejectPrice

I still need to sort out my testing. So far I’ve got limited NUnit tests, and no moq. I also need to add Xunit.

~ by mdavey on October 17, 2010.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.