Click here to Skip to main content
15,881,248 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.Reflection;
using System.Linq.Expressions;
using Signum.Utilities;
using Signum.Entities.Reflection;
using Signum.Entities;
using Signum.Engine;
using System.Reflection;

namespace Signum.Web
{
    public class TypeContextUtilities
    {
        public static IEnumerable<TypeElementContext<S>> TypeElementContext<S>(TypeContext<MList<S>> typeContext)
        {
            for (int i = 0; i < typeContext.Value.Count; i++)
            {
                var econtext = new TypeElementContext<S>(typeContext.Value[i], typeContext, i);
                yield return econtext;
            }
        }

        static MethodInfo miRetrieve = ReflectionTools.GetMethodInfo((Lite<TypeDN> l) => l.Retrieve()).GetGenericMethodDefinition();
        public static TypeContext CleanTypeContext(TypeContext typeContext)
        {
            if (typeContext == null)
                throw new ArgumentNullException("typeContext");

            if (typeContext.UntypedValue == null || 
                (!typeContext.Type.IsLite() && typeContext.Type == typeContext.UntypedValue.GetType()))
                return typeContext;

            ParameterExpression pe = Expression.Parameter(typeContext.Type, "p");

            Expression body = null;

            if (typeContext.Type.IsLite())
            {
                Lite lite = typeContext.UntypedValue as Lite;
                Type liteType = Reflector.ExtractLite(typeContext.Type);

                body = Expression.Call(miRetrieve.MakeGenericMethod(liteType), pe);
                if (lite.RuntimeType != liteType)
                    body = Expression.Convert(body, lite.RuntimeType);
            }
            else
            {
                body = Expression.Convert(pe, typeContext.UntypedValue.GetType());
            }

            LambdaExpression lambda = Expression.Lambda(body, pe);
            return Common.UntypedWalkExpression(typeContext, lambda);
        }

        public static TypeContext UntypedNew(IRootEntity entity, string controlID)
        {
            return (TypeContext)Activator.CreateInstance(typeof(TypeContext<>).MakeGenericType(entity.GetType()), entity, controlID);
        }

        public static string Compose(string prefix, string nameToAppend)
        {
            return prefix.Add(nameToAppend, TypeContext.Separator);
        }

        public static string Compose(string prefix, params string[] namesToAppend)
        {
            return Compose(prefix, (IEnumerable<string>)namesToAppend);
        }

        public static string Compose(string prefix, IEnumerable<string> namesToAppend)
        {
            return Compose(prefix, namesToAppend.ToString(TypeContext.Separator));
        }
    }
}

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