Click here to Skip to main content
15,891,943 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 1 – Southwind Entities

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
14 Nov 2012LGPL315 min read 41.4K   2K   52  
Tutorial focused in writing the entities using Signum Framework, a Win/Web LINQ-enabled framework for writing data-centric applications.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Controls;
using Signum.Utilities;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Signum.Utilities.DataStructures;
using Signum.Utilities.ExpressionTrees;
using System.Windows;
using System.Windows.Media;
using Signum.Entities;
using Signum.Entities.Basics;
using Signum.Entities.DynamicQuery;
using Signum.Windows.Properties;
using Signum.Entities.Reflection;

namespace Signum.Windows
{
    public static class Converters
    {
        public static readonly IValueConverter Identity =
            ConverterFactory.New((object v) => v, (object v) => v);

        public static readonly IValueConverter ToLite =
           ConverterFactory.New((IIdentifiable ei) => ei == null? null : Lite.Create(ei.GetType(), (IdentifiableEntity)ei));

        public static readonly IValueConverter Retrieve =
                   ConverterFactory.New((Lite lite) => lite == null ? null : Server.Retrieve(lite));

        public static readonly IValueConverter NullableEnumConverter =
            ConverterFactory.New((object v) => v == null ?  VoidEnum.Instance : v, (object v) => v.Equals(VoidEnum.Instance) ? null : v);

        public static readonly IValueConverter EnumDescriptionConverter =
            ConverterFactory.New((object v) => v is Enum? ((Enum)v).NiceToString(): "");

        public static readonly IValueConverter ErrorListToToolTipString =
            ConverterFactory.New((IEnumerable<ValidationError> err) => err.Select(e => DoubleListConverter.CleanErrorMessage(e)).FirstOrDefault());

        public static readonly IValueConverter ErrorListToErrorCount =
            ConverterFactory.New((string[] str) => str == null ? null :
                                                 new Switch<int, string>(str.Length)
                                                 .Case(0, Properties.Resources.NoDirectErrors)
                                                 .Case(1, v => Properties.Resources._1Error.Formato(str[0]))
                                                 .Default(v => Properties.Resources._0Errors1.Formato(v, str[0])));

        public static readonly IValueConverter ErrorListToBool =
            ConverterFactory.New((string[] str) => str != null && str.Length > 0);

        public static readonly IValueConverter ErrorToInt =
            ConverterFactory.New((string str) => str.HasText() ? 1 : 0);

        public static readonly IValueConverter BoolToInt =
            ConverterFactory.New((bool b) => b ? 1 : 0);

        public static readonly IValueConverter BoolToBold =
            ConverterFactory.New((bool b) => b ? FontWeights.Bold : FontWeights.Normal);

        public static readonly IValueConverter CollapseStringEmpty =
            ConverterFactory.New((string s) => s == "" ? null : s);

        public static readonly IValueConverter BoolToVisibility =
            ConverterFactory.New((bool b) => b ? Visibility.Visible : Visibility.Collapsed);

        public static readonly IValueConverter NotBoolToVisibility =
            ConverterFactory.New((bool b) => b ? Visibility.Collapsed : Visibility.Visible);

        public static readonly IValueConverter NullToVisibility =
            ConverterFactory.New((object o) => o != null ? Visibility.Visible : Visibility.Collapsed);

        public static readonly IValueConverter NotNullToVisibility =
           ConverterFactory.New((object o) => o != null ? Visibility.Collapsed : Visibility.Visible);

        public static readonly IValueConverter IsNull =
            ConverterFactory.New((object o) => o == null);

        public static readonly IValueConverter IsNotNull =
            ConverterFactory.New((object o) => o != null);

        public static readonly IValueConverter ToInt =
            ConverterFactory.New((int? val) => val.TryToString(), (string str) => str.ToInt());

        public static readonly IValueConverter BoolToSelectionMode =
            ConverterFactory.New((bool b) => b ? SelectionMode.Extended : SelectionMode.Single);

        public static readonly IValueConverter Not = ConverterFactory.New((bool b) => !b, (bool b) => !b);

        public static readonly IValueConverter TypeContextName =
            ConverterFactory.New((FrameworkElement b) => b.TryCC(fe => Common.GetTypeContext(fe)).TryCC(c => c.Type).TryCC(t => t.NiceName()) ?? "??");

        public static readonly IValueConverter NiceName =
            ConverterFactory.New((Type type) => type.TryCC(t => t.NiceName()) ?? "??");

        public static readonly IValueConverter TypeImage =
            ConverterFactory.New((Type type) => type.TryCC(t => Navigator.Manager.GetEntityIcon(type, true)));

        public static readonly IValueConverter ThicknessToCornerRadius =
            ConverterFactory.New((Thickness b) => new CornerRadius
            {
                BottomLeft = 2 * Math.Max(b.Bottom, b.Left),
                BottomRight = 2 * Math.Max(b.Bottom, b.Right),
                TopLeft = 2 * Math.Max(b.Top, b.Left),
                TopRight = 2 * Math.Max(b.Top, b.Right)
            });

        public static readonly IValueConverter ToStringConverter = ConverterFactory.New(
            (object d) => d.TryCC(a => a.ToString()));

        public static readonly IValueConverter TokenOperations = ConverterFactory.New(
            (QueryToken token) => token == null ? null : QueryUtils.GetFilterOperations(QueryUtils.GetFilterType(token.Type)));

        static readonly ColorConverter cc = new ColorConverter();
        public static readonly IValueConverter ColorConverter = ConverterFactory.New(
            (ColorDN c) => c == null ? null : (Color?)(System.Windows.Media.ColorConverter.ConvertFromString(c.Hex)),
            (Color? c) => c == null ? null : new ColorDN { Hex = cc.ConvertToString(c) });


        public static readonly IMultiValueConverter And = ConverterFactory.New(
            (bool a, bool b) => a && b);

        public static readonly IMultiValueConverter AndToVisibility = ConverterFactory.New(
            (bool a, bool b) => a && b ? Visibility.Visible : Visibility.Collapsed);

        public static readonly IMultiValueConverter Or = ConverterFactory.New(
            (bool a, bool b) => a || b);

        public static readonly IMultiValueConverter OrToVisibility = ConverterFactory.New(
                (bool a, bool b) => a || b ? Visibility.Visible : Visibility.Collapsed);
    }

    public static class ColorExtensions
    {
        public static Color Lerp(Color a, float coef, Color b)
        {
            return Color.FromScRgb(
                (1 - coef) * a.ScA + coef * b.ScA,
                (1 - coef) * a.ScR + coef * b.ScR,
                (1 - coef) * a.ScG + coef * b.ScG,
                (1 - coef) * a.ScB + coef * b.ScB);

        }

        public static Color Lerp(Color a, float coef, Color b, float alpha)
        {
            return Color.FromScRgb(
                alpha,
                (1 - coef) * a.ScR + coef * b.ScR,
                (1 - coef) * a.ScG + coef * b.ScG,
                (1 - coef) * a.ScB + coef * b.ScB);
        }
    }

}

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