Real Test Driven Development – Thinking of Money 6

The data access layer: 

We are going to be loading and saving transactions, so we are going to be testing adding and deleting transactions individually, each transaction should know how to add or delete itself, and also know what its state is.

my plan is that the engine class will have an update routine that simply walks through the collection of transactions and each updates and deletes as required.

So money transaction will need to be able to work out its state. we can do the basics add or update to the database by using a new property transactionID, to mark for delete we can use a new property toDelete.

So we add a couple of new properties, in order to set the transactionID value we create a new constructor with the transactionID value, this will allow us to create items from the database as well as creating new items, both with their own constructor.  to set the internal values I have created a procedure called from the constructors.  the unit tests we created earlier will continue to work well, and we simply need to add a single test for the ToDelete property to ensure its default is false.

        [Test]
        public void ToDelete()
        {
            Assert.IsFalse(_MoneyTrans.ToDelete);
        }

 the new constructors look like

        /// <summary>
        /// Initializes a new instance of the MoneyTransaction, this will need inserting into the data store.
        /// </summary>
        /// <param name="transactionAmount"></param>
        /// <param name="transactionCategory"></param>
        /// <param name="transactionDescription"></param>
        /// <param name="transactionDate"></param>
        /// <param name="transType"></param>
        public MoneyTransaction(double transactionAmount, string transactionCategory, string transactionDescription, DateTime transactionDate, TransactionType transType)
        {
            SetDefaultValues(0, transactionAmount, transactionCategory, transactionDescription, transactionDate, transType);
        }


        /// <summary>
        /// Initializes a new instance That contains data retrevied from a data store, that has an ID
        /// </summary>
        /// <param name="transactionID"></param>
        /// <param name="transactionAmount"></param>
        /// <param name="transactionCategory"></param>
        /// <param name="transactionDescription"></param>
        /// <param name="transactionDate"></param>
        /// <param name="transType"></param>
        public MoneyTransaction(int transactionID, double transactionAmount, string transactionCategory, string transactionDescription, DateTime transactionDate, TransactionType transType)
        {
            SetDefaultValues(transactionID, transactionAmount, transactionCategory, transactionDescription, transactionDate, transType);
        }

        private void SetDefaultValues(int transactionID, double transactionAmount, string transactionCategory, string transactionDescription, DateTime transactionDate, TransactionType transType)
        {
            _TransactionID = transactionID;
            _TransactionAmount = transactionAmount;
            _TransactionCategory = transactionCategory;
            _TransactionDescription = transactionDescription;
            _TransactionDate = transactionDate;
            _TransType = transType;
            _ToDelete = false;            
        }

 and the new properties look like

              private int _TransactionID;
              public int TransactionID
             {
                    get
                    {
                           return _TransactionID;
                     }
              }

        private bool _ToDelete;
        public bool ToDelete
        {
            get
            {
                return _ToDelete;
            }
            set
            {
                _ToDelete = value;
            }
        }

 

So we now have to write the data base access methods for this class,  called from a single update command, this will first test for add or update, then test for delete, (we won’t actually be deleting anything, we will simply mark the item as deleted this is so that we can get a full history and allow people to undo errors.

by doing the add/update first, every created transaction will be added to the database even if its deleted directly afterwards.

This change will require us to change the table and the stored procedures created earlier, don’t you just love it when program specs change!

think we shall leave all this for the next entry.

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Projects. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s