Click here to Skip to main content
15,895,709 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.5K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace WizardProjectName
{
    public partial class TypeDialog : Form, IDisposable
    {
        AppDomain realDomain;
        MyLoader myLoader;     

        public TypeDialog()
        {
            InitializeComponent();

            realDomain = AppDomain.CreateDomain("RealDomain", null, new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.BaseDirectory });

            myLoader = (MyLoader)realDomain.CreateInstanceAndUnwrap(typeof(MyLoader).Assembly.FullName, typeof(MyLoader).FullName);
        }

        public string WPFEntityControl { get; set; }
        
        public string AssemblyName { get; set; }
        public string TypeFullName { get; set; }
        public string TypeName { get; set; }
        public string TypeNamespace { get; set; }

        public string FileImputName { get; set; }

        string fileName = "LastAssemblies.txt";

        private void TypeDialog_Load(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(fileName))
                {
                    string[] assemblies = File.ReadAllLines(fileName);
                    foreach (var item in assemblies)
                    {
                        cbAssembly.Items.Add(item);
                    }

                    cbAssembly.SelectedItem = assemblies.FirstOrDefault(); 
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error reading assemblies file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cbAssembly_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cbType.Items.Clear();
                if (cbAssembly.SelectedItem != null)
                { 
                    string path = (string)cbAssembly.SelectedItem;

                    myLoader.LoadAndSetAssembly(path);
                    AssemblyName = myLoader.GetAssemblyName(); 
                    string[] types = myLoader.CompatibleTypeNames();

                    foreach (var item in types)
                    {
                        cbType.Items.Add(item); 
                    }

                    string type = FileImputName == null ? types.FirstOrDefault() :
                                                       MostSimilar(types, t => t, FileImputName);
                    cbType.SelectedItem = type;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Opening the Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static T MostSimilar<T>(IEnumerable<T> collection, Func<T, string> stringSelector, string pattern)
        {
            StringDistance sd = new StringDistance();
            return WithMin(collection, item => sd.Distance(stringSelector(item), pattern));
        }

        public static T WithMin<T, V>(IEnumerable<T> collection, Func<T, V> valueSelector)
            where V : IComparable<V>
        {
            T result = default(T);
            bool hasMin = false;
            V min = default(V);
            foreach (var item in collection)
            {
                V val = valueSelector(item);
                if (!hasMin || val.CompareTo(min) < 0)
                {
                    hasMin = true;
                    min = val;
                    result = item;
                }
            }

            return result;
        }

        private void btOpenAssembly_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog(); 
                ofd.Title = "Open Entity Assembly";  
                ofd.Filter = "Class Library (*.dll)|*.dll|Application (*.exe)|*.exe";
                ofd.FilterIndex = 0;
                if(ofd.ShowDialog() == DialogResult.OK)
                {
                    if (!cbAssembly.Items.Contains(ofd.FileName))
                        cbAssembly.Items.Remove(ofd.FileName);

                    cbAssembly.Items.Insert(0, ofd.FileName);
                    cbAssembly.SelectedItem = ofd.FileName;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Opening the Assembly", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void btOk_Click(object sender, EventArgs e)
        {
            if (TypeFullName == null)
            {
                MessageBox.Show("Choose a Type First");
                return;
            }

            try
            {

                string currentAssembly = (string)cbAssembly.SelectedItem;

                List<string> assemblies = File.Exists(fileName) ? File.ReadAllLines(fileName).ToList() : new List<string>();
                if (assemblies.Contains(currentAssembly))
                    assemblies.Remove(currentAssembly);

                assemblies.Insert(0, currentAssembly);

                File.WriteAllLines(fileName, assemblies.ToArray());

                string entityName = TypeFullName.Split('.').Last();
                if (entityName.EndsWith("DN"))
                    entityName =  entityName.Substring(0, entityName.Length -2);

                if (!FileImputName.StartsWith(entityName))
                {
                    if (FileImputName == "Entity" || MessageBox.Show(string.Format("Change item '{0}' to '{1}' to match {2}", FileImputName, entityName, TypeName),
                        "Change Name?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        FileImputName = entityName;
                    }
                }

                WPFEntityControl = myLoader.Render(TypeFullName); 

                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error writing assembly file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            if (myLoader != null)
                myLoader = null; 

            if (realDomain != null)
                AppDomain.Unload(realDomain);

            base.Dispose();
        }

        #endregion

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            TypeFullName = (string)cbType.SelectedItem;
            TypeName = myLoader.GetTypeName(TypeFullName);
            TypeNamespace = myLoader.GetTypeNamespace(TypeFullName); 
        }
    }
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions