Click here to Skip to main content
15,895,779 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 Harlinn.Oracle.DBTool.Types.Projects;
using Harlinn.Oracle.DBTool.Generators.Utils;

namespace Harlinn.Oracle.DBTool.Generators.ServiceImplementation
{
    public class WCFServiceGenerator : GeneratorBase
    {
        private Project project;
        private string className = "DataService";
        SortedSet<string> methodNames = new SortedSet<string>();


        public WCFServiceGenerator(Project project)
        {
            this.project = project;    
        }

        public override string GetFilename()
        {
            
            string directory = GetDirectory();

            string result = directory + "\\" + className + ".cs";
            return result;
        }

        public override string GetDirectory()
        {
            string namespace_ = Project.Current.ServiceImplementationNamespace;
            string directory = Project.Current.NamespaceToDirectory(namespace_);
            return directory;
        }


        public void Generate()
        {
            string commonNamespace = Project.Current.CommonNamespace;
            string dataNamespace = Project.Current.DataNamespace;
            string implmentationNamespace = Project.Current.ServiceImplementationNamespace;

            WriteLine("using System;");
            WriteLine("using System.Text;");
            WriteLine("using System.ComponentModel;");
            WriteLine("using System.Collections.Generic;");
            WriteLine("using System.ServiceModel;");
            WriteLine("using System.ServiceModel.Activation;");
            WriteLine("using System.Security;");
            WriteLine("using System.Security.Permissions;");
            WriteLine("using System.Security.Authentication;");
            WriteLine("using System.Security.Principal;");
            WriteLine("using System.Reflection;");
            WriteLine();
            WriteLine("using " + commonNamespace + ";");
            WriteLine("using " + dataNamespace + ";");
            WriteLine();
            WriteLine("namespace " + implmentationNamespace);
            WriteLine("{");
            WriteLine();
            WriteLine();
            WriteLine("    public partial class " + className + "DataContextProvider");
            WriteLine("    {");
            WriteLine("        private static IDataContextProvider dataContextProvider;");
            WriteLine();
            WriteLine();
            WriteLine("        public static IDataContextProvider DataContextProvider");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                if( dataContextProvider == null )");
            WriteLine("                {");
            WriteLine("                    dataContextProvider = ServiceImplementation.Implementation;");
            WriteLine("                }");
            WriteLine("                return dataContextProvider;");
            WriteLine("            }");
            WriteLine("            set");
            WriteLine("            {");
            WriteLine("                dataContextProvider = value;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("    }");
            WriteLine();
            WriteLine();
            WriteLine("    [ServiceContract(SessionMode = SessionMode.Required, Namespace = Constants.Namespace)]");
            WriteLine("    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]");
            WriteLine("    public partial class " + className + " : DataObserver, IDisposable");
            WriteLine("    {");

            WriteLine("        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
            WriteLine();
            WriteLine("        private static void LogException(Exception exc, MethodBase method)");
            WriteLine("        {");
            WriteLine("            Logger.LogException(sfLog, exc, method);");
            WriteLine("        }");
            WriteLine();
            WriteLine("        static void Entering( MethodBase method )");
            WriteLine("        {");
            WriteLine("            Logger.Entering(sfLog, method);");
            WriteLine("        }");
            WriteLine();
            WriteLine("        static void Leaving( MethodBase method )");
            WriteLine("        {");
            WriteLine("            Logger.Leaving(sfLog, method);");
            WriteLine("        }");
            WriteLine();
            WriteLine("        public const string DATASERVICE_USERS_GROUP = \""+project.RoleName+"\";");
            WriteLine();
            WriteLine("        private Guid clientId;");
            WriteLine("        private long systemId;");
            WriteLine("        private static object synchObj = new object();");
            WriteLine();
            WriteLine();
            WriteLine("        public " + className + "()");
            WriteLine("        {");
            WriteLine("            clientId = Guid.NewGuid();");
            WriteLine("            IDataContextProvider dataContextProvider = " + className + "DataContextProvider.DataContextProvider;");
            WriteLine("            Attach(dataContextProvider);");
            WriteLine("        }");
            WriteLine();
            WriteLine("        public void Dispose()");
            WriteLine("        {");
 	        WriteLine("            IDataContextProvider dataContextProvider = DataContextProvider;");
            WriteLine("            if(dataContextProvider != null)");
            WriteLine("            {");
            WriteLine("                Detach();");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine("        public Guid ClientId");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                return clientId;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine("        private void ConvertExceptionToFaultAndThrow(Exception exc, System.Reflection.MethodBase method)");
            WriteLine("        {");
            WriteLine("        }");
            WriteLine();
            WriteLine("        protected IDisposable CreateOperationContext(MethodBase method)");
            WriteLine("        {");
            WriteLine("            " + commonNamespace + ".OperationContext result = new " + commonNamespace + ".OperationContext(method);");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();


            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public Guid Connect()");
            WriteLine("        {");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    Guid result = ClientId;");
            WriteLine("                    return result;");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    throw;");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("        [OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public void Disconnect()");
            WriteLine("        {");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    throw;");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("        }");

            WriteLine();
            WriteLine();
            Process();

            WriteLine("    }");
            WriteLine("}");
            WriteLine();
        }

        private void Process()
        {
            WriteLine();

            Process(project.GetProjectTables());
            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public Interval GetTagValueInterval( long tag )");
            WriteLine("        {");
            WriteLine("            Interval result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider.GetTagValueInterval( tag );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException( exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();

        }

        private void Process(ProjectTables projectTables)
        {
            foreach (ProjectTable projectTable in projectTables.Children)
            {
                Process(projectTable);
                WriteLine();
            }
        }

        private void Process(ProjectTable projectTable)
        {
            GenerateGetMethods(projectTable);

            GenerateSaveMethod(projectTable);
            GenerateInsertMethod(projectTable);
            GenerateUpdateMethod(projectTable);
            GenerateDeleteMethod(projectTable);

        }


        public void GenerateGetMethods(ProjectTable projectTable)
        {
            GenerateGetAllMethod(projectTable);

            GenerateGetByPrimaryKeyMethod(projectTable);

            ProjectTabularIndexes indexes = projectTable.GetIndexes();
            foreach (ProjectTabularIndex index in indexes.Children)
            {
                GenerateGetByIndexMethod(projectTable, index);
            }


            ProjectTabularReferences references = projectTable.GetReferences();
            foreach (ProjectTabularReference reference in references.Children)
            {
                GenerateGetByReferenceMethod(projectTable, reference);
            }

            GenerateTagValueGetMethods(projectTable);

        }

        private void GenerateGetAllMethod(ProjectTable projectTable)
        {
            string tagArgDecl = "";
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArgDecl = " long tag ";
                tagArg = " tag ";
            }
            string baseName = projectTable.GetBaseName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string methodName = "GetAll" + baseName + "s";

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public List<" + dataClassName + "> " + methodName + "(" + tagArgDecl + ")");
            WriteLine("        {");
            WriteLine("            List<" + dataClassName + "> result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "(" + tagArg + ");");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
        }
        private void GenerateGetByPrimaryKeyMethod(ProjectTable projectTable)
        {
            string tagArgDecl = "";
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArgDecl = " long tag,";
                tagArg = " tag,";
            }
            string dataClassName = projectTable.GetDataTypeClassName();
            ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
            List<ProjectTabularField> fields = primaryKey.GetFieldList();
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(primaryKey);
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public " + dataClassName + " " + methodName + "(" + tagArgDecl + argDecl + " )");
            WriteLine("        {");
            WriteLine("            " + dataClassName + " result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "(" + tagArg + args + ");");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
        }

        private void GenerateGetByIndexMethod(ProjectTable projectTable, ProjectTabularIndex index)
        {
            string tagArgDecl = "";
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArgDecl = " long tag,";
                tagArg = " tag,";
            }
            string dataClassName = projectTable.GetDataTypeClassName();

            List<ProjectTabularField> fields = index.GetFieldList();
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(index);

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public " + dataClassName + " " + methodName + "(" + tagArgDecl + argDecl + " )");
            WriteLine("        {");
            WriteLine("            " + dataClassName + " result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "(" + tagArg + args + ");");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
            if ((projectTable.IsTimeSeries) && (projectTable.IsTagValue == false))
            {

                methodName = MethodNamesHelper.GetServiceGetByForTimeStampMethodName(index);
                WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
                WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
                WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
                WriteLine("        public " + dataClassName + " " + methodName + "(" + tagArgDecl + argDecl + " )");
                WriteLine("        {");
                WriteLine("            " + dataClassName + " result = null;");
                WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
                WriteLine("            Entering(currentMethod);");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                try");
                WriteLine("                {");
                WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
                WriteLine("                    using(operationContext)");
                WriteLine("                    {");
                WriteLine("                        result = DataContextProvider." + methodName + "(" + tagArg + args + ");");
                WriteLine("                    }");
                WriteLine("                }");
                WriteLine("                catch (Exception exc)");
                WriteLine("                {");
                WriteLine("                    LogException(exc, currentMethod );");
                WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
                WriteLine("                }");
                WriteLine("            }");
                WriteLine("            finally");
                WriteLine("            {");
                WriteLine("                Leaving(currentMethod);");
                WriteLine("            }");
                WriteLine("            return result;");
                WriteLine("        }");
            }

        }
        private void GenerateGetByReferenceMethod(ProjectTable projectTable, ProjectTabularReference reference)
        {
            string tagArgDecl = "";
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArgDecl = " long tag,";
                tagArg = " tag,";
            }
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            List<ProjectTabularField> fields = reference.GetFieldList();
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(reference);
            string accessorMethodName = MethodNamesHelper.GetManagerGetByMethodName(reference);

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public List<" + dataClassName + "> " + methodName + "(" + tagArgDecl + argDecl + " )");
            WriteLine("        {");
            WriteLine("            List<" + dataClassName + "> result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "(" + tagArg + args + ");");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();

        }


        private void GenerateTagValueGetMethods(ProjectTabular tabular)
        {
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;

                if (projectTable.IsTagValue)
                {
                    string baseName = projectTable.GetBaseName();
                    string dataClassName = projectTable.GetDataTypeClassName();

                    string methodName = "Get" + baseName + "ForTimeStamp";

                    WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
                    WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
                    WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
                    WriteLine("        public " + dataClassName + " " + methodName + "( long tag, DateTime ts )");
                    WriteLine("        {");
                    WriteLine("            " + dataClassName + " result = null;");
                    WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
                    WriteLine("            Entering(currentMethod);");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                try");
                    WriteLine("                {");
                    WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
                    WriteLine("                    using(operationContext)");
                    WriteLine("                    {");
                    WriteLine("                        result = DataContextProvider." + methodName + "( tag, ts);");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                catch (Exception exc)");
                    WriteLine("                {");
                    WriteLine("                    LogException(exc, currentMethod );");
                    WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            finally");
                    WriteLine("            {");
                    WriteLine("                Leaving(currentMethod);");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();

                    methodName = "Get" + baseName + "ForLastTimeStamp";

                    WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
                    WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
                    WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
                    WriteLine("        public " + dataClassName + " " + methodName + "( long tag )");
                    WriteLine("        {");
                    WriteLine("            " + dataClassName + " result = null;");
                    WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
                    WriteLine("            Entering(currentMethod);");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                try");
                    WriteLine("                {");
                    WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
                    WriteLine("                    using(operationContext)");
                    WriteLine("                    {");
                    WriteLine("                        result = DataContextProvider." + methodName + "( tag );");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                catch (Exception exc)");
                    WriteLine("                {");
                    WriteLine("                    LogException(exc, currentMethod );");
                    WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            finally");
                    WriteLine("            {");
                    WriteLine("                Leaving(currentMethod);");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();


                    methodName = "Get" + baseName + "ForInterval";

                    WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
                    WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
                    WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
                    WriteLine("        public List<" + dataClassName + "> " + methodName + "( long tag, DateTime startOfInterval, DateTime endOfInterval )");
                    WriteLine("        {");
                    WriteLine("            List<" + dataClassName + "> result = null;");
                    WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
                    WriteLine("            Entering(currentMethod);");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                try");
                    WriteLine("                {");
                    WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
                    WriteLine("                    using(operationContext)");
                    WriteLine("                    {");
                    WriteLine("                        result = DataContextProvider." + methodName + "( tag, startOfInterval, endOfInterval );");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                catch (Exception exc)");
                    WriteLine("                {");
                    WriteLine("                    LogException(exc, currentMethod );");
                    WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
                    WriteLine("                }");
                    WriteLine("            }");
                    WriteLine("            finally");
                    WriteLine("            {");
                    WriteLine("                Leaving(currentMethod);");
                    WriteLine("            }");
                    WriteLine("            return result;");
                    WriteLine("        }");
                    WriteLine();
                }
            }

        }


        private void GenerateSaveMethod(ProjectTable projectTable)
        {
            ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
            if (concurrencyField == null)
            {
                return;
            }
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            string methodName = "Save" + baseName;

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public " + dataClassName + " " + methodName + "(Guid clientId, " + dataClassName + " element )");
            WriteLine("        {");
            WriteLine("            " + dataClassName + " result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "( clientId, element );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
        }

        private void GenerateInsertMethod(ProjectTable projectTable)
        {
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            string methodName = "Insert" + baseName;

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public " + dataClassName + " " + methodName + "(Guid clientId, " + dataClassName + " element )");
            WriteLine("        {");
            WriteLine("            " + dataClassName + " result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "( clientId, element );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public void " + methodName + "List(Guid clientId, List<" + dataClassName + "> elements )");
            WriteLine("        {");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        DataContextProvider." + methodName + "List( clientId, elements );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();

        }

        private void GenerateUpdateMethod(ProjectTable projectTable)
        {
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            string methodName = "Update" + baseName;

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public " + dataClassName + " " + methodName + "(Guid clientId, " + dataClassName + " element )");
            WriteLine("        {");
            WriteLine("            " + dataClassName + " result = null;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "( clientId, element );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
        }


        private void GenerateDeleteMethod(ProjectTable projectTable)
        {
            string tagArgDecl = "";
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArgDecl = " long tag,";
                tagArg = " tag,";
            }
            string baseName = projectTable.GetBaseName();
            List<ProjectTabularField> fields = projectTable.GetPrimaryKeyFieldList();
            ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
            if (concurrencyField != null)
            {
                fields.Add(concurrencyField);
            }

            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
            string methodName = "Delete" + baseName;

            WriteLine("        [OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = false)]");
            WriteLine("        [PrincipalPermission(SecurityAction.Demand, Role = DATASERVICE_USERS_GROUP)]");
            WriteLine("        [OperationBehavior(Impersonation = ImpersonationOption.Required)]");
            WriteLine("        public int " + methodName + "(Guid clientId, " + tagArgDecl + argDecl + " )");
            WriteLine("        {");
            WriteLine("            int result = 0;");
            WriteLine("            MethodBase currentMethod = MethodBase.GetCurrentMethod();");
            WriteLine("            Entering(currentMethod);");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                try");
            WriteLine("                {");
            WriteLine("                    IDisposable operationContext = CreateOperationContext(currentMethod);");
            WriteLine("                    using(operationContext)");
            WriteLine("                    {");
            WriteLine("                        result = DataContextProvider." + methodName + "( clientId, " + tagArg + args + " );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                catch (Exception exc)");
            WriteLine("                {");
            WriteLine("                    LogException(exc, currentMethod );");
            WriteLine("                    ConvertExceptionToFaultAndThrow( exc, currentMethod );");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            finally");
            WriteLine("            {");
            WriteLine("                Leaving(currentMethod);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
        }



    }
}

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