Click here to Skip to main content
15,886,137 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 136.2K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Harlinn.DBTool.Projects;

namespace Harlinn.DBTool.CodeGenerators.Entities
{
    public class EntitySourceGenerator : GeneratorBase
    {
        Project project;
        string className = "EntitySource";
        List<ProjectTable> tagValuetables;
        List<ProjectTable> entityTables;

        public EntitySourceGenerator(Project project)
        {
            this.project = project;
            tagValuetables = new List<ProjectTable>();
            entityTables = new List<ProjectTable>();
            List<ProjectTable> allTablesList = project.GetProjectTableList();

            for (int i = 0; i < allTablesList.Count; i++)
            {
                ProjectTable projectTable = allTablesList[i];
                if (projectTable.IsTagValue)
                {
                    tagValuetables.Add(projectTable);
                }
                else
                {
                    entityTables.Add(projectTable);
                }
            }
        }


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

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

        public override string GetDirectory()
        {
            string namespace_ = project.EntityNamespace;
            string directory = project.NamespaceToDirectory(namespace_);
            return directory;
        }

        public void Generate()
        {
            string commonNamespace = project.CommonNamespace;
            string dataNamespace = project.DataNamespace;
            string entityNamespace = project.EntityNamespace;
            string implementationNamespace = project.ServiceImplementationNamespace;

            WriteLine("using System;");
            WriteLine("using System.ComponentModel;");
            WriteLine("using System.Windows.Forms;");
            WriteLine("using System.Reflection;");
            WriteLine();
            WriteLine("using " + commonNamespace + ";");
            WriteLine("using " + dataNamespace + ";");
            WriteLine("using " + implementationNamespace + ";");
            WriteLine();
            WriteLine("namespace " + entityNamespace);
            WriteLine("{");

            WriteLine("    public class DefaultEntitySourceEntityContext");
            WriteLine("    {");
            WriteLine();
            WriteLine("        private static EntityContext context;");
            WriteLine("        private static object synchObj = new object();");
            WriteLine();
            WriteLine("        public static EntityContext Context");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                lock (synchObj)");
            WriteLine("                {");
            WriteLine("                    if (context == null)");
            WriteLine("                    {");
            WriteLine("                        IDataContextProvider dataContextProvider = ServiceImplementation.Implementation;");
            WriteLine("                        context = new EntityContext(dataContextProvider);");
            WriteLine("                    }");
            WriteLine();
            WriteLine("                    return context;");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            set");
            WriteLine("            {");
            WriteLine("                lock (synchObj)");
            WriteLine("                {");
            WriteLine("                    if (context == value)");
            WriteLine("                        return;");
            WriteLine("                    context = value;");
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine("    }");
            WriteLine();
            WriteLine();
            WriteLine("    public class EntitySource : EntitySourceBase, INotifyPropertyChanged, ITypedList");
            WriteLine("    {");
            WriteLine("        private EntityType? entityType;");
            WriteLine("        private EntityContext context;");
            WriteLine("        private bool autoRetrieve;");
            WriteLine();
            WriteLine("        public event PropertyChangedEventHandler PropertyChanged;");
            WriteLine();
            WriteLine("        public EntitySource()");
            WriteLine("        {");
            WriteLine();
            WriteLine("        }");


            WriteLine();
            WriteLine("        protected void OnPropertyChanged(string propertyName)");
            WriteLine("        {");
            WriteLine("            if (PropertyChanged != null)");
            WriteLine("            {");
            WriteLine("                PropertyChangedEventArgs eventArgs = new PropertyChangedEventArgs(propertyName);");
            WriteLine("                PropertyChanged(this, eventArgs);");
            WriteLine("            }");
            WriteLine("        }");


            WriteLine();
            WriteLine("        public EntityContext Context");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                if (context == null)");
            WriteLine("                {");
            WriteLine("                    if (DesignMode == false)");
            WriteLine("                    {");
            WriteLine("                        context = DefaultEntitySourceEntityContext.Context;");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return context;");
            WriteLine("            }");
            WriteLine("            set");
            WriteLine("            {");
            WriteLine("                if (context == value)");
            WriteLine("                    return;");
            WriteLine("                context = value;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine("        [Browsable(true), Bindable(true)]");
            WriteLine("        public EntityType? EntityType");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                return entityType;");
            WriteLine("            }");
            WriteLine("            set");
            WriteLine("            {");
            WriteLine("                if (entityType == value)");
            WriteLine("                    return;");
            WriteLine("                SetBindingList( null );");
            WriteLine("                entityType = value;");
            WriteLine("                OnPropertyChanged(\"EnityType\");");
            WriteLine("                propertyDescriptorCollection = null;");
            WriteLine("                OnListChanged(new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, null));");
            WriteLine("            }");
            WriteLine("        }");

            WriteLine();
            WriteLine("        public bool AutoRetrieve");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                return autoRetrieve;");
            WriteLine("            }");
            WriteLine("            set");
            WriteLine("            {");
            WriteLine("                if (autoRetrieve == value)");
            WriteLine("                    return;");
            WriteLine("                autoRetrieve = value;");
            WriteLine("                OnPropertyChanged(\"AutoRetrieve\");");
            WriteLine("            }");
            WriteLine("        }");

            List<ProjectTable> tableList = project.GetProjectTableList();
            List<ProjectTable> entityList = new List<ProjectTable>();

            foreach (ProjectTable projectTable in tableList)
            {
                if (projectTable.IsTagValue == false)
                {
                    entityList.Add(projectTable);
                }
            }

            WriteLine();
            WriteLine("        protected override IBindingList GetBindingList()");
            WriteLine("        {");
            WriteLine("            IBindingList result = base.GetBindingList();");
            WriteLine("            if ((result == null)&&(EntityType.HasValue))");
            WriteLine("            {");
            WriteLine("                EntityContext entityContext = Context;");
            WriteLine("                switch (EntityType.Value)");
            WriteLine("                {");
            foreach (ProjectTable projectTable in entityList)
            {
                string baseName = projectTable.BaseName;
                string methodName = "GetAll" + baseName + "s";
                string entityListClassName = baseName + "EntityList";

                WriteLine("                    case " + dataNamespace + ".EntityType." + baseName + ":");
                WriteLine("                    {");
                WriteLine("                        if ((entityContext != null)&&(autoRetrieve))");
                WriteLine("                        {");
                WriteLine("                            result = entityContext." + methodName + "();");
                WriteLine("                        }");
                WriteLine("                        else");
                WriteLine("                        {");
                WriteLine("                            result = new " + entityListClassName + "();");
                WriteLine("                        }");
                WriteLine("                    }");
                WriteLine("                    break;");
            }
            WriteLine("                }"); /*end switch*/
            WriteLine("                SetBindingList(result);");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("        private PropertyDescriptorCollection propertyDescriptorCollection;");
            WriteLine("        public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)");
            WriteLine("        {");
            WriteLine("            if (listAccessors != null && listAccessors.Length > 0)");
            WriteLine("            {");
            WriteLine("                PropertyDescriptorCollection result = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            else");
            WriteLine("            {");
            WriteLine("                if (propertyDescriptorCollection == null)");
            WriteLine("                {");
            WriteLine("                    propertyDescriptorCollection = GetPropertyDescriptorCollection(EntityType);");
            WriteLine("                }");
            WriteLine("                return propertyDescriptorCollection;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("        public string GetListName(PropertyDescriptor[] listAccessors)");
            WriteLine("        {");
            WriteLine("            string result = \"Default\";");
            WriteLine("            if (EntityType.HasValue)");
            WriteLine("            {");
            WriteLine("                switch (EntityType.Value)");
            WriteLine("                {");
            foreach (ProjectTable projectTable in entityList)
            {
                string baseName = projectTable.BaseName;
                string entityClassName = projectTable.EntityClassName;

                WriteLine("                    case " + dataNamespace + ".EntityType." + baseName + ":");
                WriteLine("                        result = typeof(" + entityClassName + ").Name;");
                WriteLine("                    break;");
            }
            WriteLine("                }"); /*end switch*/
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("        public static PropertyDescriptorCollection GetPropertyDescriptorCollection( EntityType? entityType )");
            WriteLine("        {");
            WriteLine("            PropertyDescriptorCollection result = null;");
            WriteLine("            try");
            WriteLine("            {");
            WriteLine("                if (entityType.HasValue)");
            WriteLine("                {");
            WriteLine("                    switch (entityType.Value)");
            WriteLine("                    {");
            foreach (ProjectTable projectTable in entityList)
            {
                string baseName = projectTable.BaseName;
                string entityClassName = projectTable.EntityClassName;

                WriteLine("                        case " + dataNamespace + ".EntityType." + baseName + ":");
                WriteLine("                            result = TypeDescriptor.GetProperties(typeof(" + entityClassName + "), new Attribute[] { new BrowsableAttribute(true) });");
                WriteLine("                        break;");
            }
            WriteLine("                    }"); /*end switch*/
            WriteLine("                }");
            WriteLine("            }");
            WriteLine("            catch( Exception )");
            WriteLine("            {");
            WriteLine("            }");
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
            WriteLine("    }");
            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