Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / WPF

DBTool for Oracle - Part 1

Rate me:
Please Sign up or sign in to vote.
4.92/5 (45 votes)
13 Apr 2014CPOL18 min read 138.2K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Security.Principal;

using Oracle.DataAccess.Client;

namespace Harlinn.Oracle.DBTool.Example.Common
{
    public class OperationContext : IDisposable
    {
        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, System.Reflection.MethodBase method)
        {
            Logger.LogException(sfLog, exc, method);
        }

        public const string DEFAULTCONFIGURATIONSTRINGNAME = "OracleConnection";

        OracleConnection oracleConnection;
        readonly MethodBase method;
        readonly IPrincipal principal;

        private static string oracleConfigurationStringName = DEFAULTCONFIGURATIONSTRINGNAME;
        [ThreadStatic]
        private static OperationContext current;

        public OperationContext(MethodBase method, IPrincipal principal)
        {
            current = this;
            this.method = method;
            this.principal = principal;
        }


        public OperationContext(MethodBase method)
        {
            current = this;
            this.method = method;
            this.principal = System.Threading.Thread.CurrentPrincipal;
        }

        public void Dispose()
        {
            current = null;
            if (oracleConnection != null)
            {
                oracleConnection.Dispose();
            }
        }

        public static OperationContext Current
        {
            get
            {
                return current;
            }
            set
            {
                current = value;
            }
        }

        public static string OracleConfigurationStringName
        {
            get
            {
                return oracleConfigurationStringName;
            }
            set
            {
                oracleConfigurationStringName = value;
            }
        }


        public static string GetOracleConnectionString()
        {
            try
            {
                string result = string.Empty;
                ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[OracleConfigurationStringName];
                if (connectionStringSettings != null)
                {
                    result = connectionStringSettings.ConnectionString;
                }
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public static OracleConnection CreateOracleConnection()
        {
            try
            {
                string oracleConnectionString = GetOracleConnectionString();
                OracleConnection result = new OracleConnection(oracleConnectionString);
                result.Open();
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


        public OracleConnection OracleConnection
        {
            get
            {
                try
                {
                    if (oracleConnection == null)
                    {
                        oracleConnection = CreateOracleConnection();
                    }
                    return oracleConnection;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
            set
            {
                try
                {
                    if ((oracleConnection != value) && (oracleConnection != null))
                    {
                        throw new InvalidOperationException("OracleConnection is already set for this operation context.");
                    }
                    oracleConnection = value;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public MethodBase Method
        {
            get
            {
                return method;
            }
        }

        public IPrincipal Principal
        {
            get
            {
                return principal;
            }
        }


    }
}

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
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions