Saturday, November 1, 2008

Simple Banking Transaction Model in M

At PDC I meet Alejandro. He and I worked through a very simple example of a bank transaction model.

Here's a warning. Not everything writen here is currently supported by the CTP compiler, such as enumerated values for Transaction.Kind. But you'll get an idea of what we would like to support by the time we ship.

Here's the model. Check out the computed value to test that an instance is in fact a valid Transaction. It uses type ascription to test that t adheres to the shape and constraints of the Transaction type.

Also notice the computed value, ChildrenTransactions, to determine the transactions that are children of another transaction. It uses a shorter syntax of LINQ-style queries that we call compact query syntax. I put the full LINQ syntax in quotes so you can compare.

Finally, notice the constraint to validate that Transaction.Target has been set when the Kind is a "Transfer".

module Banking 
    Transactions : Transaction* 
where item.Source in Accounts, 
item.Target in Accounts;

    Accounts : Account*;
    ValidTransactionGoodness (t : Entity)
        t : Transaction

    ChildrenTransactions(t : Transaction)
        Transactions where value.Parent == t
     type Account
        Id : Integer64 = AutoNumber();
    }  where identity Id;

    type Transaction
        Id : Integer64 = AutoNumber();
        Parent : Transaction?;
        Kind : { "Transfer", "Deposit", "Withdrawal"};
        Source : Account;
        Target : Account?;
        Amount : Decimal28;
    }  where identity Id, 
Kind == "Transfer" ? Target != null : Target == null;