Click here to Skip to main content
15,878,945 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.2K   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.Linq;
using System.Text;
using Signum.Utilities;
using System.Windows;
using Signum.Entities;
using Signum.Entities.Basics;
using System.Reflection;
using Signum.Entities.Reflection;
using Signum.Entities.DynamicQuery;

namespace Signum.Windows
{
    public static class Constructor
    {
        public static ConstructorManager ConstructorManager;

        public static void Start(ConstructorManager constructorManager)
        {
            ConstructorManager = constructorManager;
            constructorManager.Initialize();
        }

        public static object Construct(Type type, Window window)
        {
            return ConstructorManager.Construct(type, window); 
        }

        public static T Construct<T>(Window window)
        {
            return (T)Construct(typeof(T), window);
        }

        public static void Register<T>(Func<Window, T> constructor) 
            where T:class
        {
            ConstructorManager.Constructors.Add(typeof(T), constructor);
        }
    }
    
    public class ConstructorManager
    {
        public event Func<Type, Window, object> GeneralConstructor; 

        public Dictionary<Type, Func<Window, object>> Constructors;

        internal void Initialize()
        {
            if (Constructors == null)
                Constructors = new Dictionary<Type, Func<Window, object>>();
        }

        public virtual object Construct(Type type, Window window)
        {
            Func<Window, object> c = Constructors.TryGetC(type);
            if (c != null)
            {
                object result = c(window);
                return result;
            }

            if (GeneralConstructor != null)
            {
                object result = GeneralConstructor(type, window);
                if (result != null)
                    return result;
            }

            return DefaultContructor(type, window);
        }

        public static object DefaultContructor(Type type, Window window)
        {
            object result = Activator.CreateInstance(type);

            AddFilterProperties(window, result);

            return result;
        }

        public static object AddFilterProperties(Window window, object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("result");

            Type type = obj.GetType();

            if (window is SearchWindow)
            {
                var filters = ((SearchWindow)window).FilterOptions.Where(fo => fo.Operation == FilterOperation.EqualTo && fo.Token is ColumnToken);

                var pairs = from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                            join fo in filters on pi.Name equals fo.Token.Key
                            where Server.CanConvert(fo.Value, pi.PropertyType) && fo.Value != null
                            select new { pi, fo };

                foreach (var p in pairs)
                {
                    p.pi.SetValue(obj, Server.Convert(p.fo.Value, p.pi.PropertyType), null);
                }
            }

            return obj; 
        }
    }
}

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