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

Signum Framework Tutorials Part 2 – Southwind Logic

, 15 Nov 2012
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.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Signum.Utilities;
using Signum.Entities.DynamicQuery;
using Signum.Entities;

namespace Signum.Windows
{
    /// <summary>
    /// Interaction logic for LinksWidget.xaml
    /// </summary>
    public partial class LinksWidget : UserControl, IWidget
    {
        public Control Control { get; set; }
        public event Action ForceShow;

        public LinksWidget()
        {
            InitializeComponent();

            this.AddHandler(Button.ClickEvent, new RoutedEventHandler(QuickLink_MouseDown));
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(LinksWidget_DataContextChanged);
        }

        void LinksWidget_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            IdentifiableEntity ident = e.NewValue as IdentifiableEntity;
            
            List<QuickLink> links = ident != null && !ident.IsNew ? Links.GetForEntity(ident, Control) : new List<QuickLink>();

            lvQuickLinks.ItemsSource = links;

            if (links.Count == 0)
                Visibility = Visibility.Collapsed;
            else
            {
                Visibility = Visibility.Visible;
                if (ForceShow != null)
                    ForceShow(); 
            }
        }

        private void QuickLink_MouseDown(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is Button) //Not to capture the mouseDown of the scrollbar buttons
            {
                Button b = (Button)e.OriginalSource;
                ((QuickLink)b.Tag).Execute();
            }
        }

        public static void Start()
        {
            WidgetPanel.GetWidgets += (obj, mainControl) => new LinksWidget() { Control = mainControl } ;
        }
    }

    public static class Links
    {
        static Dictionary<Type, List<Delegate>> entityLinks = new Dictionary<Type, List<Delegate>>();
        static List<Func<IdentifiableEntity, Control, QuickLink[]>> globalLinks = new List<Func<IdentifiableEntity, Control, QuickLink[]>>();

        public static void RegisterEntityLinks<T>(Func<T, Control, QuickLink[]> getQuickLinks)
            where T : IdentifiableEntity
        {
            entityLinks.GetOrCreate(typeof(T)).Add(getQuickLinks);
        }

        public static void RegisterGlobalLinks(Func<IdentifiableEntity, Control, QuickLink[]> getQuickLinks)
        {
            globalLinks.Add(getQuickLinks);
        }

        public static List<QuickLink> GetForEntity(IdentifiableEntity ident, Control control)
        {
            List<QuickLink> links = new List<QuickLink>();

            links.AddRange(globalLinks.SelectMany(a => a(ident, control).NotNull()));

            List<Delegate> list = entityLinks.TryGetC(ident.GetType());
            if (list != null)
                links.AddRange(list.SelectMany(a => (QuickLink[])a.DynamicInvoke(ident, control)));

            links = links.Where(l => l.IsVisible).ToList();

            return links;
        }

    }

    ///// <summary>
    ///// Controls must implement this interface to have the left navigation panel
    ///// </summary>
    //public interface IHaveQuickLinks
    //{
    //    List<QuickLink> QuickLinks();
    //}

    /// <summary>
    /// Represents an item of the left navigation panel
    /// </summary>
    public abstract class QuickLink
    {
        protected QuickLink() { }

        public string Label { get; set; }

        public bool IsVisible { get; set;}

        public string ToolTip { get; set; }

        public ImageSource Icon { get; set; }

        public abstract void Execute();
    }

    public class QuickLinkAction : QuickLink
    {
        Action action; 
        public QuickLinkAction(string label, Action action)
        {
            this.Label = label;
            this.action = action;
            this.IsVisible = true;
        }

        public override void Execute()
        {
            action();
        }
    }

    public class QuickLinkExplore : QuickLink
    {
        public ExploreOptions Options {get; set;} 

        public QuickLinkExplore(object queryName, string columnName, object value, bool hideColumn):
            this(new ExploreOptions(queryName)
            { 
                ShowFilters = false,
                SearchOnLoad = true,
                ColumnOptionsMode = hideColumn ? ColumnOptionsMode.Remove: ColumnOptionsMode.Add,
                ColumnOptions = hideColumn ? new List<ColumnOption>{new ColumnOption(columnName)}: new List<ColumnOption>(),
                FilterOptions = new List<FilterOption>
                {
                    new FilterOption(columnName, value),
                }
            })
        {  
        }

        public QuickLinkExplore(ExploreOptions options)
        {
            Options = options;
            Label = QueryUtils.GetNiceName(Options.QueryName);
            Icon = Navigator.Manager.GetFindIcon(Options.QueryName, false);
            IsVisible = Navigator.IsFindable(Options.QueryName);
        }

        public override void Execute()
        {
 	       Navigator.Explore(Options); 
        }
    }

    public class QuickLinkNavigate<T> : QuickLink
        where T:IdentifiableEntity
    {
        public NavigateOptions NavigateOptions {get; set;}

        public FindUniqueOptions FindUniqueOptions { get; set; }

        public QuickLinkNavigate(string columnName, object value)
            : this(typeof(T), columnName, value, UniqueType.Single)
        {
        }

        public QuickLinkNavigate(object queryName, string columnName, object value, UniqueType unique): 
            this(new FindUniqueOptions(queryName)
            {
                 UniqueType = unique,
                 FilterOptions = new List<FilterOption>()
                 {
                     new FilterOption(columnName, value)
                 }
            })
        {
        }

        public QuickLinkNavigate(FindUniqueOptions options)
        {
            FindUniqueOptions = options;
            Label = typeof(T).NiceName();
            Icon = Navigator.Manager.GetEntityIcon(typeof(T), false);
            IsVisible = Navigator.IsFindable(FindUniqueOptions.QueryName) && Navigator.IsViewable(typeof(T), false);
        }

        public override void Execute()
        {
            Lite<T> lite = Navigator.FindUnique<T>(FindUniqueOptions);

            if (lite == null)
                return;

            if (NavigateOptions != null)
                Navigator.Navigate(lite, NavigateOptions);
            else
                Navigator.Navigate(lite);
        }
    }
}

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
Web01 | 2.8.140916.1 | Last Updated 15 Nov 2012
Article Copyright 2011 by Olmo del Corral
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid