Click here to Skip to main content
15,891,431 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Data.SqlClient;
using System.Diagnostics;
using Signum.Utilities.Reflection;
using Signum.Utilities.DataStructures;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Engine;
using System.Data;
using Signum.Entities;

namespace Signum.Engine.Linq
{

    /// <summary>
    /// Stateless query provider 
    /// </summary>
    public class DbQueryProvider : QueryProvider
    {
        public static readonly DbQueryProvider Single = new DbQueryProvider();

        private DbQueryProvider()
        {
        }
    
        public override string GetQueryText(Expression expression)
        {
            return this.Translate(expression, tr => tr.CleanCommandText());
        }
        
        public override object Execute(Expression expression)
        {
            using (HeavyProfiler.Log("DB"))
                return this.Translate(expression, tr => tr.Execute());
        }

        T Translate<T>(Expression expression, Func<ITranslateResult, T> continuation) //For debugging purposes
        {   
            using (Alias.NewGenerator())
            {
                Expression cleaned = Clean(expression);
                Expression filtered = QueryFilterer.Filter(cleaned);

                BinderTools tools = new BinderTools();

                ProjectionExpression binded = (ProjectionExpression)QueryBinder.Bind(filtered, tools);
                ProjectionExpression optimized = (ProjectionExpression)Optimize(binded, tools);

                ProjectionExpression flat = ChildProjectionFlattener.Flatten(optimized);

                ITranslateResult result = TranslatorBuilder.Build(flat);
                return continuation(result);
            }
        }

        public static Expression Clean(Expression expression)
        {
            Expression clean = ExpressionCleaner.Clean(expression);
            Expression simplified = OverloadingSimplifier.Simplify(clean);
            return simplified;
        }

        internal static Expression Optimize(Expression binded, BinderTools tools)
        {
            Expression rewrited = AggregateRewriter.Rewrite(binded);
            Expression completed = EntityCompleter.Complete(rewrited, tools);
            Expression orderRewrited = OrderByRewriter.Rewrite(completed);

            Expression rebinded = QueryRebinder.Rebind(orderRewrited);

            Expression replaced = AliasProjectionReplacer.Replace(rebinded);
            Expression columnCleaned = UnusedColumnRemover.Remove(replaced);
            Expression rowFilled = RowNumberFiller.Fill(columnCleaned);
            Expression subqueryCleaned = RedundantSubqueryRemover.Remove(rowFilled);

            Expression rewriteConditions = ConditionsRewriter.Rewrite(subqueryCleaned);
            return rewriteConditions;
        }

        internal int Delete(IQueryable query)
        {
            using (Alias.NewGenerator())
            {
                Expression cleaned = Clean(query.Expression);
                Expression filtered = QueryFilterer.Filter(cleaned);

                BinderTools tools = new BinderTools();
                CommandExpression delete = new QueryBinder(tools).BindDelete(filtered);
                CommandExpression deleteOptimized = (CommandExpression)Optimize(delete, tools);
                CommandResult cr = TranslatorBuilder.BuildCommandResult(deleteOptimized);

                return cr.Execute();
            }
        }

        internal int Update<T>(IQueryable<T> query, Expression<Func<T, T>> set)
        {
            using (Alias.NewGenerator())
            {
                Expression cleaned = Clean(query.Expression);
                Expression filtered = QueryFilterer.Filter(cleaned);

                BinderTools tools = new BinderTools();
                CommandExpression update = new QueryBinder(tools).BindUpdate(filtered, set);
                CommandExpression updateOptimized = (CommandExpression)Optimize(update, tools);
                CommandResult cr = TranslatorBuilder.BuildCommandResult(updateOptimized);

                return cr.Execute();
            }
        }
    }
}

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