Skip to content

ProjectionBase class

Duncan Jones edited this page Jun 7, 2019 · 1 revision

ProjectionBase class

This class is an abstract class that can be used rapidly to put together a projection type to be run over an event stream by the Projection class. You create a specific projection by inheriting from this class and implementing the IHandleEventType interface for each type of event the projection should process.

Properties

CurrentSequenceNumber

int - The sequence number of the last event from the event stream that was read by the projection when it was run. This gives an indication of how up to date the data in the projection is, and can be used

Example

For a projection to get the running balance of a bank account it needs to handle the money deposited and money withdrawn events and update an internal current balance amount accordingly:

    /// <summary>
    /// The running balance of the account
    /// </summary>
    public class Balance
        : ProjectionBase,
        IHandleEventType<MoneyDeposited>,
        IHandleEventType<MoneyWithdrawn >
    {

        private decimal currentBalance;

        /// <summary>
        /// The current balance after the projection has run over a bank account event stream
        /// </summary>
        public decimal CurrentBalance
        {
            get
            {
                return currentBalance;
            }
        }



        public void HandleEventInstance(MoneyDeposited eventInstance)
        {
            if (null != eventInstance )
            {
                currentBalance += eventInstance.AmountDeposited;
            }
        }

        public void HandleEventInstance(MoneyWithdrawn eventInstance)
        {
            if (null != eventInstance )
            {
                currentBalance -= eventInstance.AmountWithdrawn;
            }
        }
    }