Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

Designing Application Using Test Driven Development

Rate me:
Please Sign up or sign in to vote.
4.49/5 (18 votes)
1 May 2008CPOL4 min read 39.4K   206   60  
In this article, we will take a look at designing application using test driven development.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DomainObjects
{
    public class SQLDataAccess
    {
        private static string connectionString = "Server=localhost;Database=School;Trusted_Connection=true";


        public static void UpdatePerson(Person person)
        {
            string query = @"UPDATE Persons SET FirstName = @FirstName, LastName = @LastName, DOB = @DOB, DateModified = GETDATE()
                            WHERE PersonID = @PersonID";

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand myCommand = new SqlCommand(query, conn);
                myCommand.Parameters.AddWithValue("@FirstName", person.FirstName);
                myCommand.Parameters.AddWithValue("@LastName", person.LastName);
                myCommand.Parameters.AddWithValue("@DOB", person.DOB);
                myCommand.Parameters.AddWithValue("@PersonID", person.Id); 

                
                conn.Open();
                myCommand.ExecuteNonQuery(); 
            }
        }
 

        public static void SaveAddress(IList<Address> addresses)
        {
            string query = "INSERT INTO Addresses(PersonID,Street) VALUES(@PersonID, @Street) SELECT SCOPE_IDENTITY()";

            foreach (Address address in addresses)
            {

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    SqlCommand myCommand = new SqlCommand(query, conn);
                    myCommand.Parameters.AddWithValue("@PersonID", address.Person.Id);
                    myCommand.Parameters.AddWithValue("@Street", address.Street);

                    conn.Open();
                    address.Id = Convert.ToInt32(myCommand.ExecuteScalar());
                    myCommand.Parameters.Clear(); 
                }
            }

        }

        public static void SavePerson(Person person)
        {            
            string query = "INSERT INTO Persons(FirstName,LastName,DOB) VALUES(@FirstName,@LastName,@DOB) SELECT SCOPE_IDENTITY()";

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand myCommand = new SqlCommand(query, conn);
                myCommand.Parameters.AddWithValue("@FirstName", person.FirstName);
                myCommand.Parameters.AddWithValue("@LastName", person.LastName);
                myCommand.Parameters.AddWithValue("@DOB", person.DOB);
                conn.Open(); 
                person.Id = Convert.ToInt32(myCommand.ExecuteScalar());

                // save the addresses! 
                SaveAddress(person.Addresses); 
            }            
        }

        public static Person GetPersonById(int id)
        {
            /*
             * we used left outer join since we want to return the person object even if it does not have 
             * the corresponding address object!
             * */
            string query = @"SELECT p.PersonID,p.FirstName,p.LastName,p.DOB,a.street FROM Persons p 
                            left outer join addresses a on a.personid = p.personid
                            WHERE p.PersonID = @PersonID";

            Person person = new Person();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand myCommand = new SqlCommand(query, conn);
                myCommand.Parameters.AddWithValue("@PersonID", id);
                conn.Open();
                SqlDataReader reader = myCommand.ExecuteReader();
                while (reader.Read())
                {
                    person.Id = (int)reader["PersonID"]; 
                   person.FirstName = reader["FirstName"] as String; 
                   person.LastName = reader["LastName"] as String; 
                   person.DOB = Convert.ToDateTime(reader["DOB"]);
                   
                    // add the address to the person object! 
                    person.AddAddress(new Address(person) { Street = reader["Street"] as String });           
                 
                };                   
                              
            }

            return person; 
        }

    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions