Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / SQL

Optimistic Concurrency with C# using the IOC and DI Design Patterns

Rate me:
Please Sign up or sign in to vote.
4.23/5 (6 votes)
25 Feb 2009CPOL2 min read 38.4K   223   32  
Discussion of concurrency using the IOC and DI Design Patterns with the PostgreSQL database.
using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;

namespace WFrmAppIOC
{
    /// <summary>
    /// ClsUser
    /// </summary>
    public class ClsUser
    {
        private iDatabase idb;
        /// <summary>
        /// ClsUser
        /// </summary>
        public ClsUser()
        {
            idb = new Postgres();
            idb.DBName = "CONNC";
            idb.Server = "localhost";
            idb.SAUserName = "postgres";
            idb.SAPassword = "Password1";
        }
        /// <summary>
        /// GetUserPassword
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public string GetUserPassword(string userName)
        {
            string password = "";
            // check to see of the connection is set
            try
            {
                idb.CheckDBConnectionInfo();
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }
            // get the users password
            try
            {
                password = idb.GetUserPassword(userName);
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }
            return (password);
        }
        /// <summary>
        /// UpdateUser
        /// </summary>
        /// <param name="ud"></param>
        /// <returns></returns>
        public int UpdateUser(UserData ud)
        {
            int ires = -1;
            // check to see of the connection is set
            try {
                idb.CheckDBConnectionInfo();
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }
            // update the users data
            try
            {
                ires = idb.UpdateUser(ud);
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }
            return (ires);
        }
        /// <summary>
        /// AddUser
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="Password"></param>
        /// <param name="ModUSer"></param>
        /// <returns></returns>
        public int AddUser(string userName, string Password, string ModUSer)
        {
            int ires = -1;
            // check to see of the connection is set
            try
            {
                idb.CheckDBConnectionInfo();
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }

            try
            {
                ires = idb.AddUser(userName, Password, ModUSer); 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return (ires);
        }
        /// <summary>
        /// GetUser
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserData GetUser(string userName)
        {
            UserData ud;
            // check to see of the connection is set
            try
            {
                idb.CheckDBConnectionInfo();
            }
            catch (Exception ex)
            {
                string ostr;
                // show where the error came from. Get the class and methodname to display in the log file. 
                ostr = string.Format("{0}{1}", this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                LogFile.WriteError(ostr, ex.Message);
                throw;
            }
            // Get the users data
            try
            {
                ud = idb.GetUser(userName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return (ud);
        }
    }
}

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
I am a Director of Engineering, have an MBA and work in C# forms, Asp.Net and vb.net. I have been writing Windows program since windows 3.0. I am currently working in the Healthcare industry.

I enjoy reading, music (most types), and learning new technology. I am involved in opensource projects at codeplex.

My linkedin link is
http://www.linkedin.com/in/donsweitzer

Comments and Discussions