Click here to Skip to main content
12,294,775 members (65,740 online)
Click here to Skip to main content

Stats

15.3K views
953 downloads
21 bookmarked
Posted

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.bak
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
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
__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
Scripts
ServerCapri.svc
Views
Capri
Home
Shared
__TemplateIcon.ico
favicon.ico
ProjectName.Windows
Controls
MyTemplate.vstemplate
Properties
__TemplateIcon.ico
SignumFramework2ClientServer.zip
__TemplateIcon.ico
WizardProjectName
Test
Properties
WizardProjectName
Isolated
Properties
SignumFramework.pfx
TraceAndTestImpact.testsettings
SignumFramework2Setup.msi
.gitignore
README.md
Southwind.vssscc
Southwind
Southwind.Entities
Properties
Southwind.Load
Properties
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
Scripts
ServerSouthwind.svc
Views
Home
Shared
Southwind
favicon.ico
Southwind.Windows
Controls
Properties
Southwind.bak
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Signum.Utilities
{
    public class WikiSettings
    {
        public WikiSettings(bool format)
        {
            Strong = Em = Underlined = Strike = Lists = Titles = format; 
            AllowRawHtml = false;
            LineBreaks = MaxTwoLineBreaks = true;
        }

        public Func<string, string> TokenParser;
        public bool Strong { get; set; }
        public bool Em { get; set; }
        public bool Underlined { get; set; }
        public bool Strike { get; set; }
        public bool Lists { get; set; }
        public bool Titles { get; set; }
        public bool LineBreaks { get; set; }

        public bool AllowRawHtml { get; set; }
        public bool MaxTwoLineBreaks { get; set; }

    }

    public static class WikiParserExtensions
    {
        public static string HtmlSubstitute = "||HTML{0}||";

        public static string WikiParse(this string content, WikiSettings settings)
        {
            string result;

            Dictionary<string, string> htmlFragments = null;

            if (settings.AllowRawHtml)
            {
                htmlFragments = new Dictionary<string, string>();

                content = Regex.Replace(content, @"<!\[CDATA\[(?<html>.*?)\]\]>", m =>
                {
                    var key = HtmlSubstitute.Formato(htmlFragments.Count);
                    htmlFragments.Add(key, m.Groups["html"].Value);
                    return key;
                }, RegexOptions.Singleline);
            }

            //1: Process tokens
            result = ProcessTokens(content, settings);

            //2: Process format
            result = ProcessFormat(result, settings);

            if (settings.AllowRawHtml)
                result = Regex.Replace(result, @"\|\|(?<key>HTML\d+)\|\|", m => htmlFragments[m.Value]);

            return result.Trim();
        }

        static string ProcessTokens(string content, WikiSettings settings)
        {
            StringBuilder sb = new StringBuilder();
            int firstIndex = 0;

            Match m = Regex.Match(content, @"\[(.+?)\]");
            while (m.Success)
            {
                string text = m.Value;
                try
                {
                    text = settings.TokenParser
                    .GetInvocationList()
                    .Cast<Func<string, string>>()
                    .Select(a => a(m.Value)).NotNull().First();
                }
                catch (Exception)
                {
                }

                sb.Append(content.Substring(firstIndex, m.Index - firstIndex) + ((text != null) ? text : ""));
                firstIndex = m.Index + m.Length;
                m = m.NextMatch();
            }
            sb.Append(content.Substring(firstIndex, content.Length - firstIndex));
            return sb.ToString();
        }

        static string ProcessFormat(string content, WikiSettings settings)
        {
            // Replacing both
                content = Regex.Replace(content,
                "(?<begin>'''''{1})(?<content>.+?)(?<end>'''''{1})",
                (settings.Strong && settings.Em) ? "<strong><em>${content}</em></strong>" : "${content}",
                RegexOptions.Compiled);

            // Replacing bolds
                content = Regex.Replace(content,
                "(?<begin>'''{1})(?<content>.+?)(?<end>'''{1})",
                settings.Strong ? "<strong>${content}</strong>" : "${content}",
                RegexOptions.Compiled);

            // Replacing italics
                content = Regex.Replace(content,
                "(?<begin>''{1})(?<content>.+?)(?<end>''{1})",
                settings.Em ? "<em>${content}</em>" : "${content}",
                RegexOptions.Compiled);

            // Replacing underlined
                content = Regex.Replace(content,
                "(?<begin>__{1})(?<content>.+?)(?<end>__{1})",
                settings.Underlined ? "<u>${content}</u>" : "${content}",
                RegexOptions.Compiled);

            // Replacing strike
            content = Regex.Replace(content,
            "(?<begin>\\-\\-{1})(?<content>.+?)(?<end>\\-\\-{1})",
            settings.Strike ? "<s>${content}</s>" : "${content}",
            RegexOptions.Compiled);

            // Replacing lists
            content = Regex.Replace(content,
     "^\\s*(?<begin>\\*{1}[ ]?)(?<content>.+)(?<end>[^*]?)[\\n]*",
     settings.Lists ? "<li>${content}</li>" : "${content} ",
     RegexOptions.Compiled | RegexOptions.Multiline);

            content = Regex.Replace(content,
     "^\\s*(?<begin>\\#{1}[ ]?)(?<content>.+)(?<end>[^#]?)[\\n]*",
     settings.Lists ? "<oli>${content}</oli>" : "${content} ",
     RegexOptions.Compiled | RegexOptions.Multiline);

            content = Regex.Replace(content,
            "(?<content>\\<li\\>{1}.+\\<\\/li\\>)",
            settings.Lists ? "<ul>${content}</ul>" : "${content} ",
            RegexOptions.Compiled);

            content = Regex.Replace(content,
       "(?<content>\\<oli\\>{1}.+\\<\\/oli\\>)",
        settings.Lists ? "<ol>${content}</ol>" : "${content} ",
       RegexOptions.Compiled);

            content = Regex.Replace(content,
       "(?<content>oli\\>{1})",
       "li>",
       RegexOptions.Compiled);

            // Assign the replace method to the MatchEvaluator delegate.
            MatchEvaluator meTitle = new MatchEvaluator(ReplaceTitle);

            // Replacing titles
            if (settings.Titles)
                content = Regex.Replace(content,
                "(?<begin>={2,})(?<content>[^\\n]+?)(?<end>={2,})[\\n]*",
                meTitle,
                RegexOptions.Compiled);
            else
                content = Regex.Replace(content,
                "(?<begin>={2,})(?<content>[^\\n]+?)(?<end>={2,})[\\n]*",
                "${content}. ",
                RegexOptions.Compiled);

            //Remove multiple breakline  
            if (settings.MaxTwoLineBreaks)
            {
                content = Regex.Replace(content,
                    "(?<content>\n{3,})","\n\n", 
                    RegexOptions.Compiled);
            }
      
            content = Regex.Replace(content,
                "(?<content>\n)", settings.LineBreaks ? "<br/>" : ". ",
                RegexOptions.Compiled);

            content = Regex.Replace(content,
                "(?<content>\r)", "",
                RegexOptions.Compiled);
            return content;
        }

        public static string ReplaceTitle(Match m)
        {
            return "<h" + m.Groups["begin"].Length + ">" + m.Groups["content"].ToString() + "</h" + m.Groups["end"].Length + ">";
        }
    }

    public static class FormatHtmlExtensions
    {
        public static string AddHtml(this string content, string firstTag, string lastTag, string regex)
        {
            //Do not add tags inside the content of other tags

            Regex rTags = new Regex("<[^>]*>[^<]*</[^>]>");

            var matchesTags = rTags.Matches(content);

            Regex r = new Regex(regex.RemoveDiacritics(), RegexOptions.IgnoreCase);

            int lastIndex = 0;
            StringBuilder sb = new StringBuilder(content.Length);

            foreach (Match m in r.Matches(content.RemoveDiacritics()))
            {
                bool skip = false;
                foreach (Match mt in matchesTags)
                {
                    if (mt.Index <= m.Index && mt.Index + mt.Length > m.Index + m.Length)
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                    sb.Append(m.Value);
                else
                {
                    sb.Append(content.Substring(lastIndex, m.Index - lastIndex));
                    sb.Append(firstTag);
                    sb.Append(content.Substring(m.Index, m.Length));
                }
                sb.Append(lastTag);
                lastIndex = m.Index + m.Length;
            }
            sb.Append(content.Substring(lastIndex, content.Length - lastIndex));
            return sb.ToString();
        }
    }
}

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...

You may also be interested in...

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