Click here to Skip to main content
15,897,371 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.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Signum.Utilities.Reflection;
using System.Reflection;
using System.Linq.Expressions;
using System.Windows.Data;
using Signum.Utilities.ExpressionTrees;
using Signum.Entities;

namespace Signum.Windows
{
    public static class Fluent
    {
        public static T Bind<T>(this T bindable, DependencyProperty property, BindingBase binding) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, binding);
            return bindable;
        }

        public static T Bind<T>(this T bindable, DependencyProperty property, string sourcePath) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(sourcePath));
            return bindable;
        }

        public static T Bind<T, S>(this T bindable, DependencyProperty property, Expression<Func<S, object>> sourcePath) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(RouteVisitor.GetRoute(sourcePath)));
            return bindable;
        }

        public static T Bind<T>(this T bindable, DependencyProperty property, object source, string sourcePath) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(sourcePath) { Source = source });
            return bindable;
        }

        public static T Bind<T, S>(this T bindable, DependencyProperty property, S source, Expression<Func<S, object>> sourcePath) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(RouteVisitor.GetRoute(sourcePath)) { Source = source });
            return bindable;
        }

        public static T Bind<T>(this T bindable, DependencyProperty property, string sourcePath, IValueConverter converter) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(sourcePath) { Converter = converter });
            return bindable;
        }

        public static T Bind<T, S>(this T bindable, DependencyProperty property, Expression<Func<S, object>> sourcePath, IValueConverter converter) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(RouteVisitor.GetRoute(sourcePath)) { Converter = converter });
            return bindable;
        }

        public static T Bind<T>(this T bindable, DependencyProperty property, object source, string sourcePath, IValueConverter converter) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(sourcePath) { Source = source, Converter = converter });
            return bindable;
        }

        public static T Bind<T, S>(this T bindable, DependencyProperty property, S source, Expression<Func<S, object>> sourcePath, IValueConverter converter) where T : DependencyObject
        {
            BindingOperations.SetBinding(bindable, property, new Binding(RouteVisitor.GetRoute(sourcePath)) { Source = source, Converter = converter });
            return bindable;
        }

        public static T Set<T>(this T depObj, DependencyProperty prop, object value) where T : DependencyObject
        {
            depObj.SetValue(prop, value);
            return depObj;
        }

        public static T Handle<T>(this T uiElement, RoutedEvent routedEvent, RoutedEventHandler handler) where T : UIElement
        {
            uiElement.AddHandler(routedEvent, handler);
            return uiElement;
        }

        public static T ResourceReference<T>(this T frameworkElement, DependencyProperty prop, object value) where T : FrameworkElement
        {
            frameworkElement.SetResourceReference(prop, value);
            return frameworkElement;
        }
     
        public static T Hide<T>(this T uiElement) where T : UIElement
        {
            uiElement.Visibility = Visibility.Hidden;
            return uiElement;
        }

        public static T Collapse<T>(this T uiElement) where T : UIElement
        {
            uiElement.Visibility = Visibility.Collapsed;
            return uiElement;
        }

        public static T Visible<T>(this T uiElement) where T : UIElement
        {
            uiElement.Visibility = Visibility.Visible;
            return uiElement;
        }

        public static T ReadOnly<T>(this T uiElement) where T : UIElement
        {
            Common.SetIsReadOnly(uiElement, true);
            return uiElement;
        }

        public static T Editable<T>(this T uiElement) where T : UIElement
        {
            Common.SetIsReadOnly(uiElement, false);
            return uiElement;
        }

        public static DataTemplate GetDataTemplate(System.Linq.Expressions.Expression<Func<FrameworkElement>> constructor)
        {
            return new DataTemplate
            {
                VisualTree = FrameworkElementFactoryGenerator.Generate(constructor)
            };
        }

        public static FrameworkElementFactory GetFrameworkElementFactory(System.Linq.Expressions.Expression<Func<FrameworkElement>> constructor)
        {
            return FrameworkElementFactoryGenerator.Generate(constructor);
        }
    }

    class RouteVisitor : SimpleExpressionVisitor
    {
        StringBuilder sb = new StringBuilder();

        public static string GetRoute(LambdaExpression expression)
        {
            RouteVisitor v = new RouteVisitor();
            v.Visit(expression.Body);
            return v.sb.ToString();
        }

        protected override System.Linq.Expressions.Expression VisitMemberAccess(MemberExpression m)
        {
            var result = base.VisitMemberAccess(m);
            if (sb.Length != 0)
                sb.Append(".");
            sb.Append(m.Member.Name);
            return result;
        }

        protected override System.Linq.Expressions.Expression VisitMethodCall(MethodCallExpression m)
        {
            var result = base.VisitMethodCall(m);
            if (m.Method.DeclaringType == typeof(MListExtensions) && m.Method.Name == "Element")
            {
                sb.Append("/");
            }

            return result;
        }

        protected override System.Linq.Expressions.Expression VisitBinary(BinaryExpression b)
        {
            var result = base.VisitBinary(b);

            if (result.NodeType == ExpressionType.ArrayIndex)
            {
                int value = (int)ExpressionEvaluator.Eval(b.Left);
                sb.Append("[" + value + "]");
            }

            return result;
        }
    }
}

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