Click here to Skip to main content
15,896,154 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.Data;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows;

namespace Signum.Windows
{
    public static class ConverterFactory
    {
        public static Converter<S, T> New<S, T>(Func<S, T> convert)
        {
            return new Converter<S, T> { convert = convert };
        }

        public static DualConvertet<S1, S2, T> New<S1, S2, T>(Func<S1, S2, T> convert)
        {
            return new DualConvertet<S1, S2, T> { convert = convert };
        }

        public static MultiConverter<S, T> NewMulti<S, T>(Func<S[], T> convert)
        {
            return new MultiConverter<S, T> { convert = convert };
        }

        public static Converter<S, T> New<S, T>(Func<S, T> convert, Func<T, S> convertBack)
        {
            return new Converter<S, T> { convert = convert, convertBack = convertBack };
        }

        public static ConverterValidator<S, T> New<S, T>(Func<S, T> convert, Func<T, S> convertBack, Func<S, string> validator)
        {
            return new ConverterValidator<S, T> { convert = convert, convertBack = convertBack, validator = validator };
        }
    }

    public static class BindingExtensions
    {
        public static Binding AddConverterValidator<T>(this Binding binding, T validationConverter) where T : ValidationRule, IValueConverter
        {
            binding.ValidationRules.Add(validationConverter);
            binding.Converter = validationConverter;
            return binding;
        }

        public static Binding AddValidation(this Binding binding, ValidationRule validationRule)
        {
            binding.ValidationRules.Add(validationRule);
            return binding;
        }
    }


    public class Converter<S, T> : IValueConverter
    {
        internal Func<S, T> convert;
        internal Func<T, S> convertBack;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (convert == null)
                throw new NotImplementedException();

            T result = convert((S)value);

            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (convertBack == null)
                throw new NotImplementedException();

            S result = convertBack((T)value);

            return result;
        }
    }

    public class DualConvertet<S1, S2, T> : IMultiValueConverter
    {
        internal Func<S1, S2, T> convert;

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (convert == null)
                throw new NotImplementedException();

            if (values == null || values.Length != 2 || (values[0] == DependencyProperty.UnsetValue) || (values[1] == DependencyProperty.UnsetValue))
                return default(T);

            T result = convert((S1)values[0], (S2)values[1]);

            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MultiConverter<S, T> : IMultiValueConverter
    {
        internal Func<S[], T> convert;

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (convert == null)
                throw new NotImplementedException();

            if (values.Contains(DependencyProperty.UnsetValue))
                return DependencyProperty.UnsetValue;

            T result = convert(values.Cast<S>().ToArray());

            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ConverterValidator<S, T> : ValidationRule, IValueConverter
    {
        internal Func<S, T> convert;
        internal Func<T, S> convertBack;
        internal Func<S, string> validator;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (this.convert == null)
            {
                throw new NotImplementedException();
            }
            return this.convert((S)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (this.convertBack == null)
            {
                throw new NotImplementedException();
            }
            return this.convertBack((T)value);
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string error = null;
            if (this.validator != null)
            {
                error = this.validator((S)value);
            }
            return new ValidationResult(error == null, error);
        }
    }

    public class PipeExtension : MarkupExtension, IValueConverter
    {
        public IValueConverter First { get; set; }
        public IValueConverter Second { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object temp = First.Convert(value, null, null, culture);
            object result = Second.Convert(temp, targetType, null, culture);
            return result; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object temp = Second.ConvertBack(value, null, null, culture);
            object result = First.ConvertBack(temp, targetType, null, culture);
            return result; 
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }
}

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