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

namespace Signum.Entities.DynamicQuery
{
    [Serializable]
    public class ExtensionToken : QueryToken
    {
        public ExtensionToken(QueryToken parent, string key, Type type, string unit, string format, Implementations implementations, bool isAllowed): base(parent)
        {
            if (!typeof(IIdentifiable).IsAssignableFrom(parent.Type.CleanType()))
                throw new InvalidOperationException("Extensions only allowed over {0}".Formato(typeof(IIdentifiable).Name)); 

            this.key= key;
            this.type = type;
            this.unit = unit;
            this.format = format;
            this.implementations = implementations;
            this.isAllowed = isAllowed;         
        }

        public string DisplayName { get; set; }

        public override string ToString()
        {
            return DisplayName;
        }

        public override string NiceName()
        {
            return DisplayName + Resources.Of + Parent.ToString();
        }

        string format;
        public override string Format { get { return format; } }

        string unit;
        public override string Unit { get { return unit; } }

        Type type;
        public override Type Type { get { return BuildLite(type).Nullify(); } }

        string key;
        public override string Key { get { return key; } }

        protected override QueryToken[] SubTokensInternal()
        {
            return base.SubTokensBase(type, implementations);  
        }

        public static Func<Type, string, Expression, Expression> BuildExtension;

        protected override Expression BuildExpressionInternal(BuildExpressionContext context)
        {
            if (BuildExtension == null)
                throw new InvalidOperationException("ExtensionToken.BuildExtension not set");

            var result = BuildExtension(Parent.Type.CleanType(), Key, ExtractEntity(Parent.BuildExpression(context), false));

            return BuildLite(result).Nullify();
        }

        public override PropertyRoute GetPropertyRoute()
        {
            return null;
        }

        public Implementations implementations;
        public override Implementations Implementations()
        {
            return implementations;
        }

        bool isAllowed; 
        public override bool IsAllowed()
        {
            return isAllowed && Parent.IsAllowed();
        }

        public override QueryToken Clone()
        {
            return new ExtensionToken(this.Parent.Clone(), key, type, unit, format, implementations, isAllowed); 
        }
    }
}

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