Real Test Driven Development – Thinking of Money 8

 

so I now have an add and update, the delete will follow the same format, I have currently stored the connection string in a constant, but I suppose it can eventually be stored in an application setting. so for the delete

        [Test]
        public void DeleteTransaction()
        {
            DBMoneyTransaction transaction = new DBMoneyTransaction();

            string helpText = string.Empty;

            Assert.IsTrue(transaction.DeleteTransaction(0, ref helpText), helpText);
        }
        public bool DeleteTransaction(int transID, ref string helpText)
        {
            SqlCommand cmd = null;

            try
            {
                cmd = GetCommand("uspDeleteTransaction", STR_Connection);
                cmd.Parameters.Add(GetParam("@TransID", transID, SqlDbType.Int, ParameterDirection.Input));

                cmd.ExecuteNonQuery();

                return true;
            }
            catch (SqlException sqlEx)
            {
                helpText = sqlEx.Message;
                return false;
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Connection.Close();
                    cmd.Dispose();
                }
            }
        }

this passes and with the new functions GetCommand, and GetParam, the procedure is nice and short.  now we need to be able to load a transaction or a group of transactions. I have been thinking that if

the engine should have a collection of transactions, so I think it is here that the get routine should be, which means that the transaction data class is finished, so we can no finish the transaction class by linking the add, update and delete functions to the transaction class.

first I want to create an update function, this will call the insert or update database routines as required.

        [Test]
        public void Update()
        {
            Assert.IsTrue(_MoneyTrans.Update(),_MoneyTrans.ErrorText);
        }
        public bool Update()
        {
            DBMoneyTransaction database = new DBMoneyTransaction();

            if (TransactionID == 0)
            {
                _TransactionID = database.InsertTransaction((int)TransType, TransactionDate, TransactionCategory, 
TransactionDescription, TransactionAmount, ref _ErrorText);
                if (TransactionID == 0)
                    return false;
                else
                    return true;
            }
            else
                return database.UpdateTransaction(TransactionID, (int)TransType, TransactionDate, 
TransactionCategory, TransactionDescription, TransactionAmount, ref _ErrorText);
        }

and a delete function should rearly be part of the update, so we simply have to call update on transaction and the appropriate actions will be performed.

        [Test]
        public void Delete()
        {
       // so we actually have to call the delete routine 
       Assert.IsTrue(_MoneyTrans.Update(), _MoneyTrans.ErrorText); 
            _MoneyTrans.ToDelete = true;
            Assert.IsTrue(_MoneyTrans.Update());
        }

and after a bit of refractoring to make the update function more readable I ended with this

     public bool Update()
        {
            DBMoneyTransaction database = new DBMoneyTransaction();

            if (ToDelete)
                return DeleteTransaction(database);

            if (TransactionID == 0)
                return InsertTransaction(database);
            else
                return database.UpdateTransaction(TransactionID, (int)TransType, TransactionDate, 
                       TransactionCategory, TransactionDescription, TransactionAmount, ref _ErrorText);
        }

        private bool DeleteTransaction(DBMoneyTransaction database)
        {
            if (TransactionID != 0)
                return database.DeleteTransaction(TransactionID, ref _ErrorText);
            else
                return true;            
        }

        private bool InsertTransaction(DBMoneyTransaction database)
        {
            _TransactionID = database.InsertTransaction((int)TransType, TransactionDate, 
                             TransactionCategory, TransactionDescription, TransactionAmount, ref _ErrorText);
            if (TransactionID == 0)
                return false;
            else
                return true;
        }

So next we need to get back to the money engine and create a group of transactions and load them from the database, then be able to update and delete them each using the above command.

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