Click here to Skip to main content
15,886,137 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.3K   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 Signum.Entities;
using Signum.Entities.Reflection;

namespace Signum.Engine
{
    public class ExternalPolymorphicDictionary<K, V> where V : class
    {
        Dictionary<Type, Dictionary<K, V>> dictionary = new Dictionary<Type, Dictionary<K, V>>();

        public Dictionary<Type, Dictionary<K, V>> RawDictionary { get { return dictionary; } }

        public V TryGetValue(Type type, K key)
        {
            if (!type.IsIIdentifiable())
                throw new InvalidOperationException("Type {0} has to implement at least {1}".Formato(type, typeof(IIdentifiable)));

            V result = type.FollowC(t => t.BaseType)
                .TakeWhile(t => typeof(IdentifiableEntity).IsAssignableFrom(t))
                .Select(t => dictionary.TryGetC(t).TryGetC(key)).NotNull().FirstOrDefault();

            if (result != null)
                return result;

            List<Type> interfaces = type.GetInterfaces()
                .Where(t => t.IsIIdentifiable() && dictionary.TryGetC(t).TryGetC(key) != null)
                .ToList();

            if (interfaces.Count > 1)
                throw new InvalidOperationException("Ambiguity between interfaces: {0}".Formato(interfaces.ToString(", ")));

            if (interfaces.Count < 1)
                return null;

            return dictionary[interfaces.Single()][key];
        }

        public HashSet<K> GetAllKeys(Type type)
        {
            if (!type.IsIIdentifiable())
                throw new InvalidOperationException("Type {0} has to implement at least {1}".Formato(type, typeof(IIdentifiable)));
            
            HashSet<K> result = type.FollowC(t => t.BaseType)
                    .TakeWhile(t => typeof(IdentifiableEntity).IsAssignableFrom(t))
                    .Select(t => dictionary.TryGetC(t)).NotNull().SelectMany(d => d.Keys).ToHashSet();

            result.UnionWith(type.GetInterfaces()
                .Where(t => t.IsIIdentifiable())
                .Select(t => dictionary.TryGetC(t))
                .NotNull().SelectMany(d => d.Keys));

            return result;
        }

        public HashSet<K> GetAllKeys()
        {
            return dictionary.Values.SelectMany(a => a.Keys).ToHashSet();
        }

        public void Add(Type type, K key, V value)
        {
            dictionary.GetOrCreate(type)[key] = value;
        }

        public Type[] ImplementingTypes(K key)
        {
            return dictionary.Where(o => o.Value.ContainsKey(key)).Select(a => a.Key).ToArray();
        }
    }
}

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