Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Signum Framework Tutorials Part 2 – Southwind Logic

, 15 Nov 2012 LGPL3
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
Northwind.zip
Northwind.bak
SignumFramework2.0-src.zip
signumframework-signumframework-c6436e1
.gitignore
Local.testsettings
README.md
Signum.Engine
ClassDiagram1.cd
Connection
DynamicQuery
Engine
Linq
ExpressionVisitor
Meta
Properties
Schema
Schema.cd
SchemaBuilder
SignumFramework.pfx
Signum.Entities
Attributes.cd
Basics
ClassDiagram1.cd
Diagram.cd
DynamicQuery
Tokens
Patterns
Properties
Reflection
Services
SignumFramework.pfx
Signum.Framework.vsmdi
Signum.Test
ClassDiagram1.cd
LinqProvider
Properties
Settings.settings
Signum.Utilities
DataStructures
IntervalDictionaries
ExpressionTrees
Properties
Reflection
SignumFramework.pfx
Synchronization
Signum.Web
ButtonBar
ControllerAttributes
Facades
HtmlHelpers
JSRenderer
LineHelpers
Lines
Mappings
PortableAreas
Repositories
Properties
Signum
Content
Controllers
Images
asc.gif
calendar.png
desc.gif
lineButtons.gif
sign_error.gif
Models
Scripts
Views
Widgets
SignumFramework.pfx
TypeContext
Widgets
Signum.Windows
Basics
Controls
ColorUtils
Converters
DynamicQuery
EntityControls
Facades
Fluent
Images
admin.png
alert.png
calendar.png
cancel.png
close.png
entity.png
find.png
forbidden.png
newColumn.png
newFilter.png
note.png
ok.png
pin.png
reload.png
save.png
star.png
Localization
MarkupExtensions
Properties
SignumFramework.pfx
Themes
Utilities
Widgets
SignumFrameworkSetup
SignumFrameworkSetup.vdproj
logo.ico
signumFramework2.0.jpg
Snippets
DroidSansMono.ttf
RazorSingleFileGenerator.vsix
Signum.Fonts.vssettings
T4template
entity.snippet
entityWithName.snippet
expressionMethod.snippet
expressionMethodQuery.snippet
expressionProperty.snippet
field.snippet
fieldCreacion.snippet
fieldEuro.snippet
fieldInicioFin.snippet
fieldString.snippet
fiff.snippet
propiedad.snippet
tran.snippet
Template
EntityControlTemplate
MyTemplate.vstemplate
SignumEntityControl.zip
__TemplateIcon.ico
Template
MyTemplate.vstemplate
ProjectName.Entities
MyTemplate.vstemplate
Properties
__TemplateIcon.ico
ProjectName.Load
MyTemplate.vstemplate
Properties
Settings.settings
__TemplateIcon.ico
ProjectName.Logic
MyTemplate.vstemplate
Properties
__TemplateIcon.ico
ProjectName.Web
Code
Content
smoothness
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
MyTemplate.vstemplate
Properties
Settings.settings
Scripts
ServerCapri.svc
Views
Capri
Home
Shared
__TemplateIcon.ico
favicon.ico
ProjectName.Windows
Controls
MyTemplate.vstemplate
Properties
Settings.settings
__TemplateIcon.ico
SignumFramework2ClientServer.zip
__TemplateIcon.ico
WizardProjectName
Test
Properties
Settings.settings
WizardProjectName
Isolated
Properties
SignumFramework.pfx
TraceAndTestImpact.testsettings
SignumFramework2Setup.zip
SignumFramework2Setup.msi
Southwind-Part2-Logic.zip
.gitignore
README.md
Southwind.vssscc
Southwind
Southwind.Entities
Properties
Southwind.Load
Properties
Settings.settings
Southwind.Logic
Properties
Southwind.Web
Code
Content
smoothness
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
Properties
Settings.settings
Scripts
ServerSouthwind.svc
Views
Home
Shared
Southwind
favicon.ico
Southwind.Windows
Controls
Properties
Settings.settings
Southwind.zip
Southwind.bak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters.Binary;
using Signum.Utilities.Properties;
using System.Globalization;
using System.Linq.Expressions;

namespace Signum.Utilities
{
    public static class StringExtensions
    {
        static Expression<Func<string, bool>> HasTextExpression = str => str != null && str != "";

        public static bool HasText(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        public static string DefaultText(this string str, string defaultText)
        {
            if (str.HasText()) return str;
            else return defaultText;
        }

        public static string AssertHasText(this string str, string errorMessage)
        {
            if (str.HasText())
                return str;
            else
                throw new ArgumentException(errorMessage);
        }

        public static string Add(this string str, string part, string separator)
        {
            if (str.HasText())
            {
                if (part.HasText())
                    return str + separator + part;
                else
                    return str;
            }
            else
                return part;
        }

        public static string AddLine(this string str, string part)
        {
            return Add(str, part, "\r\n");
        }

        public static string[] Lines(this string str)
        {
            if (str.HasText())
                return str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            else
                return new string[0];
        }

        public static string Left(this string str, int numChars)
        {
            return Left(str, numChars, true);
        }

        public static string Left(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException("String '{0}' is too short".Formato(str));
                return str;
            }

            return str.Substring(0, numChars);
        }

        public static string Right(this string str, int numChars)
        {
            return Right(str, numChars, true);
        }

        public static string Right(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException("String '{0}' is too short".Formato(str));
                return str;
            }

            return str.Substring(str.Length - numChars, numChars);
        }

        public static string RemoveLeft(this string str, int numChars)
        {
            return str.RemoveLeft(numChars, true);
        }

        public static string RemoveLeft(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException("String '{0}' is too short".Formato(str));
                return "";
            }

            return str.Substring(numChars);
        }

        public static string RemoveRight(this string str, int numChars)
        {
            return str.RemoveRight(numChars, true);
        }

        public static string RemoveRight(this string str, int numChars, bool throws)
        {
            if (numChars > str.Length)
            {
                if (throws)
                    throw new InvalidOperationException("String '{0}' is too short".Formato(str));
                return "";
            }

            return str.Substring(0, str.Length - numChars);
        }


        public static string PadChopRight(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(0, length) : str.PadRight(length);
        }

        public static string PadChopLeft(this string str, int length)
        {
            str = str ?? "";
            return str.Length > length ? str.Substring(str.Length - length, length) : str.PadLeft(length);
        }

        public static string VerticalEtc(this string str, int maxLines)
        {
            return str.VerticalEtc(maxLines, "(...)");
        }

        public static string VerticalEtc(this string str, int maxLines, string etcString)
        {
            if (str.HasText() && (str.Contains("\r\n")))
            {
                string[] arr = str.Split(new string[] { "\r\n" }, maxLines, StringSplitOptions.None);
                string res = arr.ToString("\r\n");
                if (res.Length < str.Length)
                    res += etcString;
                str = res;
            }
            return str;
        }

        public static string Etc(this string str, int max, string etcString)
        {
            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;
            return str;
        }

        public static string Etc(this string str, int max)
        {
            return str.Etc(max, "(...)");
        }

        public static string EtcLines(this string str, int max, string etcString)
        {
            if (!str.HasText())
                return str;

            int pos = str.IndexOfAny(new[] { '\n', '\r' });
            if (pos != -1 && pos + etcString.Length < max)
                max = pos + etcString.Length;

            if (str.HasText() && (str.Length > max))
                return str.Left(max - (etcString.HasText() ? etcString.Length : 0)) + etcString;
            return str;
        }

        public static string EtcLines(this string str, int max)
        {
            return str.EtcLines(max, "(...)");
        }

        public static bool ContinuesWith(this string str, string subString, int pos)
        {
            return str.IndexOf(subString, pos) == pos;
        }

        public static string RemoveChars(this string str, params char[] chars)
        {
            StringBuilder sb = new StringBuilder(str.Length);
            for (int i = 0; i < str.Length; i++)
            {
                if (!chars.Contains(str[i]))
                    sb.Append(str[i]);
            }
            return sb.ToString();
        }

        public static string Formato(string format, object arg0)
        {
            return string.Format(format, arg0);
        }

        public static string Formato(string format, object arg0, object arg1)
        {
            return string.Format(format, arg0, arg1);
        }

        public static string Formato(string format, object arg0, object arg1, object arg2)
        {
            return string.Format(format, arg0, arg1, arg2);
        }

        public static string Formato(this string pattern, params object[] parameters)
        {
            return string.Format(pattern, parameters);
        }

        public static string Formato(this string format, IFormatProvider provider, params object[] args)
        {
            return string.Format(provider, format, args);
        }

        public static string Replace(this string str, Dictionary<string, string> replacements)
        {
            return replacements.Aggregate(str, (s, kvp) => s.Replace(kvp.Key, kvp.Value));
        }

        public static string Indent(this string str, int numChars)
        {
            return Indent(str, numChars, ' ');
        }

        public static string Indent(this string str, int numChars, char indentChar)
        {
            string space = new string(indentChar, numChars);
            StringBuilder sb = new StringBuilder();
            using (StringReader sr = new StringReader(str))
            {
                for (string line = sr.ReadLine(); line != null; line = sr.ReadLine())
                {
                    sb.Append(space);
                    sb.AppendLine(line);
                }
            }

            string result = sb.ToString(0, str.EndsWith("\r\n") ? sb.Length : Math.Max(sb.Length - 2, 0));

            return result;
        }

        public static string NiceName(this string memberName)
        {
            return memberName.Contains('_') ?
              memberName.Replace('_', ' ') :
              memberName.SpacePascal();
        }

        public static string SpacePascal(this string pascalStr)
        {
            return SpacePascal(pascalStr, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en");
        }

        public static string SpacePascal(this string pascalStr, bool preserveUppercase)
        {
            
            StringBuilder sb = new StringBuilder();
            if (pascalStr.Length > 0)
                sb.Append(pascalStr[0]); 

            for (int i = 1; i < pascalStr.Length; i++)
            {
                char a = pascalStr[i];
                if (char.IsUpper(a) && !char.IsUpper(pascalStr[i - 1]))
                {
                    if (sb.Length > 0)
                        sb.Append(' ');
                    if (preserveUppercase || i == 0 || (i < pascalStr.Length - 1 && char.IsUpper(pascalStr[i + 1])))
                        sb.Append(a);
                    else
                        sb.Append(char.ToLower(a));
                }
                else
                    sb.Append(a);
            }

            return sb.ToString();
        }

        public static string FirstUpper(this string str)
        {
            if (str.HasText())
                return char.ToUpper(str[0]) + str.Substring(1);
            return str;
        }

        public static string Replicate(this string str, int times)
        {
            if (times < 0)
                throw new ArgumentException("times");

            StringBuilder sb = new StringBuilder(str.Length * times);
            for (int i = 0; i < times; i++)
                sb.Append(str);
            return sb.ToString();
        }

        public static string Reverse(this string str)
        {
            char[] arr = new char[str.Length];
            int len = str.Length;
            for (int i = 0; i < len; i++)
                arr[i] = str[len - 1 - i];
            return new string(arr);
        }

        // like has an optional ESCAPE not available
        public static bool Like(this string str, string pattern)
        {
            pattern = Regex.Escape(pattern);
            pattern = pattern.Replace("%", ".*").Replace("_", ".");
            pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
            return Regex.IsMatch(str, pattern);
        }

        public static string RemoveDiacritics(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s; 

            string normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                char c = normalizedString[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);
            }

            return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
        }

        public static string ToComputerSize(this long value)
        {
            return ToComputerSize(value, false);
        }

        static string[] abbreviations = new[] { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
        static string[] magnitudes = new[] { "Bytes", "KBytes", "MBytes", "GBytes", "TBytes", "PBytes", "EBytes", "ZBytes", "YBytes" };
        public static string ToComputerSize(this long value, bool useAbbreviations)
        {
            double valor = value;
            long i;
            for (i = 0; i < magnitudes.Length && valor >= 1024; i++)
                valor /= 1024.0;

            return "{0:#,###.00} {1}".Formato(valor, (useAbbreviations ? abbreviations : magnitudes)[i]);
        }


        public static string Combine(this string separator, params object[] elements)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in elements)
            {
                if (item != null)
                {
                    sb.Append(item.ToString());
                    sb.Append(separator);
                }
            }
            return sb.ToString(0, Math.Max(0, sb.Length - separator.Length));  // Remove at the end is faster
        }

        public static string CombineIfNotEmpty(this string separator, params object[] elements)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in elements)
            {
                if (item != null && item.ToString().HasText())
                {
                    sb.Append(item.ToString());
                    sb.Append(separator);
                }
            }
            return sb.ToString(0, Math.Max(0, sb.Length - separator.Length));  // Remove at the end is faster
        }
    }
}

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)

Share

About the Author

Olmo del Corral
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...

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 15 Nov 2012
Article Copyright 2011 by Olmo del Corral
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid