GreenRock Software Code Comment

February 19, 2008

Filed under: BLL Classes — greenrocksoftware @ 11:20 am

 This code is part of an application that demonstrates our adopted approach to the separation of GUI and database. The EntityInfo tier is used as a container to carry data objects in and out of the DAL as well as holding the attribute data for BLL objects.

 This code is an example of a BLL class.

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Serialization;

using EntityInfo;

using DataAccess;

 

namespace BusinessLogic

{

    public class User

    {

 

        #region private members

 

        // create the DataAccess class

        private static readonly UserDB _dal = new UserDB();

 

        // EntityInfo object reference that stores User attributes

        private UserInfo _userInfo;

 

        #endregion private members

 

        #region public properties

 

        public String Login

        {

            get { return _userInfo.Login; }

            set { _userInfo.Login = value; }

        }

 

        public String Password

        {

            get { return _userInfo.Password; }

            set { _userInfo.Password = value; }

        }

 

        public String FirstName

        {

            get { return _userInfo.FirstName; }

            set { _userInfo.FirstName = value; }

        }

 

        public String LastName

        {

            get { return _userInfo.LastName; }

            set { _userInfo.LastName = value; }

        }

 

        public String Address

        {

            get { return _userInfo.Address; }

            set { _userInfo.Address = value; }

        }

 

        public String City

        {

            get { return _userInfo.City; }

            set { _userInfo.City = value; }

        }

 

        public String State

        {

            get { return _userInfo.State; }

            set { _userInfo.State = value; }

        }

 

        public String Zip

        {

            get { return _userInfo.Zip; }

            set { _userInfo.Zip = value; }

        }

 

        public String Phone

        {

            get { return _userInfo.Phone; }

            set { _userInfo.Phone = value; }

        }

 

        #endregion public properties

 

        #region constructors

 

        public User(UserInfo uInfo)

        {

            this._userInfo = uInfo;

        }

 

        public User(string login, string password)

        {

            Users users = new Users();

            this._userInfo = users.GetUser(login, password);

        }

 

        public User(string login, string password, string firstname, string lastName, string address, string city, string state, string zip, string phone)

        {

            UserInfo uInfo = new UserInfo();

            uInfo.Login = login;

            uInfo.Password = password;

            uInfo.FirstName = firstname;

            uInfo.LastName = lastName;

            uInfo.Address = address;

            uInfo.City = city;

            uInfo.State = state;

            uInfo.Zip = zip;

            uInfo.Phone = phone;

 

            this._userInfo = uInfo;

        }

 

        #endregion  constructors

 

        #region public methods

 

        public void AddUser()

        {

            if (this._userInfo != null)

            {

                Users users = new Users();

                users.AddUser(this._userInfo);

            }

        }

 

        /// <summary>

        /// Checks if a valid instance.

        /// </summary>

        public bool isValid

        {

            get

            {

                bool valid = true;

                if (this._userInfo == null)

                {

                    valid = false;

                }

                else

                {

                    if (this._userInfo.Login == string.Empty || this._userInfo.Password == string.Empty)

                    {

                        valid = false;

                    }

                }

                return valid;

            }

        }

 

        #endregion public methods

 

    }

}

 

Filed under: DTO Classes — greenrocksoftware @ 11:07 am

This code is part of an application that demonstrates our adopted approach to the separation of GUI and database. The EntityInfo tier is used as a container to carry data objects in and out of the DAL as well as holding the attribute data for BLL objects.

This code is an example of a DTO class.

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Serialization;

 

namespace EntityInfo

 

{

    // EntityInfo object used to pass data to the data access layer

    public class UserInfo

    {

        #region private members

        private string _login;

        private string _password;

        private string _firstname;

        private string _lastName;

        private string _address;

        private string _city;

        private string _state;

        private string _zip;

        private string _phone;

        #endregion private members

        #region properties

        [XmlAttribute(“Login”)]

        public String Login

        {

            get { return _login.Trim(); }

            set { _login = value; }

        }

        [XmlAttribute(“Password”)]

        public String Password

        {

            get { return _password.Trim(); }

            set { _password = value; }

        }

        [XmlElement(“First Name”)]

        public String FirstName

        {

            get { return _firstname; }

            set { _firstname = value; }

        }

        [XmlElement(“Last Name”)]

        public String LastName

        {

            get { return _lastName; }

            set { _lastName = value; }

        }

        [XmlElement(“Address”)]

        public String Address

        {

            get { return _address; }

            set { _address = value; }

        }

        [XmlElement(“City”)]

        public String City

        {

            get { return _city; }

            set { _city = value; }

        }

        [XmlElement(“State”)]

        public String State

        {

            get { return _state; }

            set { _state = value; }

        }

        [XmlElement(“Zip”)]

        public String Zip

        {

            get { return _zip; }

            set { _zip = value; }

        }

        [XmlElement(“Telephone”)]

        public String Phone

        {

            get { return _phone; }

            set { _phone = value; }

        }

        #endregion properties

 

        #region constructors

 

        public UserInfo()

        {

        }

        public UserInfo(string login, string password, string firstname, string lastName, string address, string city, string state, string zip, string phone)

        {

            this.Login = login;

            this.Password = password;

            this.FirstName = firstname;

            this.LastName = lastName;

            this.Address = address;

            this.City = city;

            this.State = state;

            this.Zip = zip;

            this.Phone = phone;

        }

    #endregion constructors

    }

}

 

XML DAL Class

Filed under: DAL Classes — greenrocksoftware @ 10:53 am

This code is part of an application that demonstrates our adopted approach to the separation of GUI and database. The EntityInfo tier is used as a container to carry data objects in and out of the DAL as well as holding the attribute data for BLL objects.

This code is the main DTO tier dealing with saving and reading XML files.

using System;

using System.IO;

using System.Text;

using System.Xml;

using System.Data;

using System.Collections.Generic;

using System.Xml.Serialization;

using System.Data.SqlClient;

using System.Configuration;

using EntityInfo;

namespace DataAccess

{

    public class UserDB

    {

        #region private members

        private static string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

        private static string DataStore = ConfigurationManager.AppSettings[“DataStore”];

        private static string xmlFile = ConfigurationManager.AppSettings[“XmlData”];

        private static string ConnectionString = ConfigurationManager.ConnectionStrings[“SQLConnString”].ConnectionString;

        #endregion private members

        // constructor

        public UserDB()

        {

        }

 

        #region XML Access

        ///// <summary>

        ///// Serialization – Saves the XML to disk.

        ///// </summary>

        ///// <param name=”userList”>The user list.</param>

        //public void SaveXmlToDisk(UserList userList)

        //{

        // XmlSerializer s = new XmlSerializer(typeof(UserList));

        // TextWriter w = new StreamWriter(xmlFile);

        // s.Serialize(w, userList);

        // w.Close();

        //}

        /// <summary>

        /// Serialization – Writes the XML to disk.

        /// </summary>

        public void SaveXmlToDisk(UserList userList)

        {

            if (userList == null || userList.Count < 1)

            {

                throw new ArgumentException(“The collection must contain data”);

            }

            try

            {

                if (File.Exists(xmlFile))

                {

                    File.Delete(xmlFile);

                }

            }

            catch (IOException)

            {

                throw;

            }

 

            XmlSerializer xser = new XmlSerializer(userList.GetType());

            using (TextWriter writer = new StreamWriter(xmlFile))

            {

                // serialize without the default namespace

                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

                ns.Add(“”, “”);

                xser.Serialize(writer, userList, ns);

            }

        }

        ///// <summary>

        ///// Deserialization – Reads the XML from disk.

        ///// </summary>

        //public UserList ReadXmlFromDisk()

        //{

        // UserList userList;

        // //TODO: try catch

        // TextReader r = new StreamReader(xmlFile);

        // XmlSerializer s = new XmlSerializer(typeof(UserList));

        // userList = (UserList)s.Deserialize(r);

        // r.Close();

        // return userList;

        //}

        /// <summary>

        /// Deserialization – Reads the XML from disk.

        /// </summary>

        public UserList ReadXmlFromDisk()

        {

            UserList userList = new UserList();

            userList = null;

            if (!File.Exists(xmlFile))

            {

                // if creating a new file, insert at least one record

                throw new IOException(“The xml file specified in App.config must exist”);

            }

 

            if (File.Exists(xmlFile))

            {

                using (XmlTextReader xr = new XmlTextReader(xmlFile))

                {

                    XmlSerializer xs = new XmlSerializer(typeof(UserList));

                    userList = (UserList)xs.Deserialize(xr);

                }

            }

            return userList;

        }

 

        public UserInfo Load(string login, string password)

        {

            UserInfo userInfo = null;

            UserList userList = ReadXmlFromDisk();

            foreach (UserInfo user in userList)

            {

                if (user.Login == login && user.Password == password)

                {

                    userInfo = user;

                }

            }

            return userInfo;

        }

 

        #endregion XML Access

        #region SQL Access

 

        /// <summary>

        /// Write to the database

        /// </summary>

        /// <returns>An error string, if any</returns>

        public void WriteToDB(UserInfo userInfo)

        {

            if (userInfo == null)

            {

                throw new ArgumentException(“No user was supplied”);

            }

            try

            {

                SqlParameter[] paramsToStore = new SqlParameter[9];

                SetParameters(userInfo, paramsToStore);

                SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, “pUsers_i”, paramsToStore);

            }

            catch (SqlException)

            {

                throw;

            }

        }

 

        private static void SetParameters(UserInfo userInfo, SqlParameter[] paramsToStore)

        {

            paramsToStore[0] = new SqlParameter(“@Login”, SqlDbType.NVarChar);

            paramsToStore[0].Value = userInfo.Login;

            paramsToStore[1] = new SqlParameter(“@Password”, SqlDbType.NVarChar);

            paramsToStore[1].Value = userInfo.Password;

            paramsToStore[2] = new SqlParameter(“@FirstName”, SqlDbType.NVarChar);

            paramsToStore[2].Value = userInfo.FirstName;

            paramsToStore[3] = new SqlParameter(“@LastName”, SqlDbType.NVarChar);

            paramsToStore[3].Value = userInfo.LastName;

            paramsToStore[4] = new SqlParameter(“@Address”, SqlDbType.NVarChar);

            paramsToStore[4].Value = userInfo.Address;

            paramsToStore[5] = new SqlParameter(“@City”, SqlDbType.NVarChar);

            paramsToStore[5].Value = userInfo.City;

            paramsToStore[6] = new SqlParameter(“@State”, SqlDbType.NVarChar);

            paramsToStore[6].Value = userInfo.State;

            paramsToStore[7] = new SqlParameter(“@Zip”, SqlDbType.NVarChar);

            paramsToStore[7].Value = userInfo.Zip;

            paramsToStore[8] = new SqlParameter(“@Telephone”, SqlDbType.NVarChar);

            paramsToStore[8].Value = userInfo.Phone;

        }

 

        /// <summary>

        /// Function to get a list from the database

        /// </summary>

        /// <returns>A Generic List of UserInfo</returns>

        public UserList ReadFromDB()

        {

            UserInfo userInfo = new UserInfo();

            UserList userList = new UserList();

            using (SqlDataReader rdr = SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, “pUsers_l”, null))

            {

                // Scroll through the results

                while (rdr.Read())

                {

                    userInfo = PopulateItem(rdr);

                    userList.Add(userInfo);

                }

            }

            return userList;

            }

 

        /// <summary>

        /// Get an individual item

        /// </summary>

        /// <param name=”itemId”>unique key</param>

        /// <returns>Details about the Item</returns>

        public UserInfo GetUser(string login, string password)

        {

            UserInfo userInfo = new UserInfo();

            //Create a parameter

            SqlParameter[] paramsToStore = new SqlParameter[2];

            paramsToStore[0] = new SqlParameter(“@Login”, SqlDbType.NVarChar);

            paramsToStore[0].Value = login;

            paramsToStore[1] = new SqlParameter(“@Password”, SqlDbType.NVarChar);

            paramsToStore[1].Value = password;

            using (SqlDataReader rdr = SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, “pUsers_l”, null))

            {

                // Scroll through the results

                while (rdr.Read())

                {

                    userInfo = PopulateItem(rdr);

                }

            }

            return userInfo;

        }

 

        private static UserInfo PopulateItem(SqlDataReader rdr)

        {

            UserInfo userInfo = new UserInfo();

            userInfo.Login = rdr[“Login”].ToString();

            userInfo.Password = rdr[“Password”].ToString();

            userInfo.FirstName = rdr[“FirstName”].ToString();

            userInfo.LastName = rdr[“LastName”].ToString();

            userInfo.Address = rdr[“Address”].ToString();

            userInfo.City = rdr[“City”].ToString();

            userInfo.State = rdr[“State”].ToString();

            userInfo.Zip = rdr[“Zip”].ToString();

            userInfo.Phone = rdr[“Telephone”].ToString();

            return userInfo;

        }

 

    #endregion SQL Access

    }

}

Welcome

Filed under: DAL Classes — greenrocksoftware @ 4:22 am

This is a blog that shows us some of the snippet code we have used.

Blog at WordPress.com.