Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Testable Applications

Rate me:
Please Sign up or sign in to vote.
3.30/5 (9 votes)
20 Apr 200712 min read 45.2K   198   35  
Make your application testable.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TestableApplication.Testable.Dalc
{
    public class InvoiceDalc : DalcBase<Invoice>, IInvoiceDalc
    {
        Invoice _invoice;
        public InvoiceDalc(Invoice invoice)
        {
            _invoice = invoice;
        }
        public Invoice IssueInvoice(string description, ICustomer customer, List<InvoiceItem> items)
        {
            Console.WriteLine("--- IssueInvoice Method was invoked from object {0} ", this);
            Console.WriteLine("press enter to continue ....");
            Console.ReadLine();

            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter param = new SqlParameter("@DESCRIPTION", SqlDbType.NVarChar);
            param.Value = description;
            parameters.Add(param);

            param = new SqlParameter("@CUS_ID", SqlDbType.Int);
            param.Value = customer.ID; ;
            parameters.Add(param);

            DateTime issueDate = DateTime.Now;
            param = new SqlParameter("@ISSUE_DATE", SqlDbType.DateTime);
            param.Value = issueDate;
            parameters.Add(param);

            param = new SqlParameter("@DUE_DATE", SqlDbType.DateTime);
            param.Value = issueDate.AddMonths(1); ;
            parameters.Add(param);

            object idObj = ExecuteStoredScalerProcedure("stp_add_invoice_header", parameters);
            // for test only
            idObj = DateTime.Now.Millisecond;
            int invoiceNumber = int.Parse(idObj.ToString());

            parameters.Clear();
            int seq = 0;
            foreach (InvoiceItem item in items)
            {

                param = new SqlParameter("@INV_ID", SqlDbType.Int);
                param.Value = invoiceNumber;
                parameters.Add(param);

                param = new SqlParameter("@QTY", SqlDbType.Int);
                param.Value = item.Quantity;
                parameters.Add(param);

                param = new SqlParameter("@SEQ_NUM", SqlDbType.Int);
                param.Value = ++seq;
                parameters.Add(param);
                ExecuteStoredScalerProcedure("stp_add_invoice_line", parameters);

            }
            Invoice newInvoice = new Invoice(invoiceNumber, description, customer, issueDate, Status.Issued, items);
            customer.Debt(newInvoice.TotalAmount, newInvoice.Description, newInvoice.InvoiceNumber, newInvoice.IssueDate);
            return newInvoice;
        }
        public void PayOffInvoice(ICustomer customer)
        {
            Console.WriteLine("--- IssueInvoice Method was invoked from object {0} ", this);
            Console.WriteLine("press enter to continue ....");
            Console.ReadLine();

            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter param = new SqlParameter("@INV_ID", SqlDbType.Int);
            param.Value = _invoice.InvoiceNumber;
            parameters.Add(param);

            param = new SqlParameter("@CUS_ID", SqlDbType.Int);
            param.Value = customer.ID; ;
            parameters.Add(param);

            DateTime issueDate = DateTime.Now;
            param = new SqlParameter("@PAY_OFF_DATE", SqlDbType.DateTime);
            param.Value = DateTime.Now;
            parameters.Add(param);

            object idObj = ExecuteStoredScalerProcedure("stp_pay_off_invoice", parameters);
            _invoice._status = Status.Paid;
            customer.Credit(_invoice.TotalAmount, _invoice.Description, _invoice.InvoiceNumber, _invoice.IssueDate);
        }
        public override Invoice Make(DataRow dr)
        {
           // Customer cus = new Customer().GetCustomer(int.Parse(dr["CUS_ID"].ToString()));
           // List<InvoiceItem> items = new InvoiceItem().GetItems(int.Parse(dr["INV_ID"].ToString()));
           // return new Invoice(int.Parse(dr["INV_ID"].ToString()), dr["DESCRIPTION"].ToString(), cus, DateTime.Parse(dr["@ISSUE_DATE"].ToString()), (Status)Enum.Parse(typeof(Status), dr["STATUS"].ToString()), items);
            return new Invoice(DateTime.Now.Millisecond, "qq", new Customer(1, "aa", "bb"));
        }
        public Invoice GetInvoice(int invoiceNomber)
        {
            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter param = new SqlParameter("@ID", SqlDbType.Int);
            param.Value = invoiceNomber;
            parameters.Add(param);

            DataTable dt = ExecuteStoredProcedure("stp_get_Invoice", parameters);
            #region for test only
            dt.Rows.Add(dt.NewRow());
            #endregion 
            return Make(dt.Rows[0]);
        }
        public void Update()
        {
            throw new InvalidOperationException("Invoice Update method, This Error did in purpose as Example of Invoice  Dependency Error");
            
            
            Console.WriteLine("--- Update Method was invoked from object {0} ", this);
            Console.WriteLine("press enter to continue ....");
            Console.ReadLine();

            List<SqlParameter> parameters = new List<SqlParameter>();
            SqlParameter param = new SqlParameter("@INV_ID", SqlDbType.Int);
            param.Value = _invoice.InvoiceNumber;
            parameters.Add(param);

            param = new SqlParameter("@DESCRIPTION", SqlDbType.NVarChar);
            param.Value = _invoice.Description;
            parameters.Add(param);

            param = new SqlParameter("@CUS_ID", SqlDbType.Int);
            param.Value = _invoice.Customer.ID;
            parameters.Add(param);


            param = new SqlParameter("@ISSUE_DATE", SqlDbType.DateTime);
            param.Value = _invoice.IssueDate;
            parameters.Add(param);

            param = new SqlParameter("@DUE_DATE", SqlDbType.DateTime);
            param.Value = _invoice.DueDate;
            parameters.Add(param);

            ExecuteStoredScalerProcedure("stp_update_invoice_header", parameters);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) NSW Curriculum & Learning Innovation Centre
Australia Australia
I am a senior developer self taught,
the main study is electronics and communication engineering

I am working as senior programmer in center for learning and innovation
www.cli.nsw.edu.au

I develop Software since 1995 using Delphi with Microsoft SQL Server

before 2000, I didn’t like Microsoft tools and did many projects using all database tools and other programming tools specially Borland C++ Builder, Delphi, Power Builder
And I loved Oracle database for its stability until I was certified as Master in Database Administration from Oracle University.

I tried to work in web programming but I felt that Java is hard and slow in programming, specially I love productivity.

I began worked with .Net since 2001 , and at the same time Microsoft SQL Server 7 was very stable so I switched all my way to Microsoft Tech.
I really respect .Net Platform especially in web applications

I love database Applications too much
And built library with PowerBuilder it was very useful for me and other developers

I have a wide experience due to my work in different companies
But the best experience I like in wireless applications, and web applications.
The best Application I did in my life is Novartis Marketing System 1999 it takes 8 months developing with PowerBuilder and Borland C++, SQL Server
Performance was the key challenge in this Application.
The other 2 applications that I loved Multilingual Project in Scada company in Italy 2000 and SDP Mobile media content platform server for ChinaUnicom 2004
I hope that you enjoy any Article I post.
God bless you.

Comments and Discussions