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.Controls;
using System.Windows;
using System.ComponentModel;
using Signum.Entities;
using Signum.Utilities;
using Signum.Entities.Reflection;
using System.Windows.Input;

namespace Signum.Windows
{
    public class EntityBase : LineBase
    {
        public static RoutedCommand CreateCommand =
          new RoutedCommand("Create", typeof(EntityLine), new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.N, ModifierKeys.Control, "Create") }));
        public static RoutedCommand ViewCommand =
            new RoutedCommand("View", typeof(EntityLine), new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.G, ModifierKeys.Control, "View") }));
        public static RoutedCommand RemoveCommand =
            new RoutedCommand("Remove", typeof(EntityLine), new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.R, ModifierKeys.Control, "Remove") }));
        public static RoutedCommand FindCommand =
            new RoutedCommand("Find", typeof(EntityLine), new InputGestureCollection(new InputGesture[] { new KeyGesture(Key.F, ModifierKeys.Control, "Find") }));

        public static readonly DependencyProperty EntityProperty =
            DependencyProperty.Register("Entity", typeof(object), typeof(EntityBase), new FrameworkPropertyMetadata(null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d, e) => ((EntityBase)d).OnEntityChanged(e.OldValue, e.NewValue)));
        public object Entity
        {
            get { return (object)GetValue(EntityProperty); }
            set
            {
                SetValue(EntityProperty, null);  //entities have equals overriden
                SetValue(EntityProperty, value);
            }
        }

        protected Implementations safeImplementations;
        public static readonly DependencyProperty ImplementationsProperty =
            DependencyProperty.Register("Implementations", typeof(Implementations), typeof(EntityBase), new UIPropertyMetadata((d, e) => ((EntityBase)d).safeImplementations = (Implementations)e.NewValue));
        public Implementations Implementations
        {
            get { return (Implementations)GetValue(ImplementationsProperty); }
            set { SetValue(ImplementationsProperty, value); }
        }

        public static readonly DependencyProperty EntityTemplateProperty =
           DependencyProperty.Register("EntityTemplate", typeof(DataTemplate), typeof(EntityBase), new UIPropertyMetadata(null));
        public DataTemplate EntityTemplate
        {
            get { return (DataTemplate)GetValue(EntityTemplateProperty); }
            set { SetValue(EntityTemplateProperty, value); }
        }

        public static readonly DependencyProperty CreateProperty =
            DependencyProperty.Register("Create", typeof(bool), typeof(EntityBase), new FrameworkPropertyMetadata(true, (d, e) => ((EntityBase)d).UpdateVisibility()));
        public bool Create
        {
            get { return (bool)GetValue(CreateProperty); }
            set { SetValue(CreateProperty, value); }
        }

        public static readonly DependencyProperty ViewProperty =
            DependencyProperty.Register("View", typeof(bool), typeof(EntityBase), new FrameworkPropertyMetadata(true, (d, e) => ((EntityBase)d).UpdateVisibility()));
        public bool View
        {
            get { return (bool)GetValue(ViewProperty); }
            set { SetValue(ViewProperty, value); }
        }

        public static readonly DependencyProperty ViewButtonsProperty =
            DependencyProperty.Register("ViewButtons", typeof(ViewButtons), typeof(EntityBase), new UIPropertyMetadata(ViewButtons.Ok));
        public ViewButtons ViewButtons
        {
            get { return (ViewButtons)GetValue(ViewButtonsProperty); }
            set { SetValue(ViewButtonsProperty, value); }
        }

        public static readonly DependencyProperty FindProperty =
            DependencyProperty.Register("Find", typeof(bool), typeof(EntityBase), new FrameworkPropertyMetadata(true, (d, e) => ((EntityBase)d).UpdateVisibility()));
        public bool Find
        {
            get { return (bool)GetValue(FindProperty); }
            set { SetValue(FindProperty, value); }
        }

        public static readonly DependencyProperty RemoveProperty =
            DependencyProperty.Register("Remove", typeof(bool), typeof(EntityBase), new FrameworkPropertyMetadata(true, (d, e) => ((EntityBase)d).UpdateVisibility()));
        public bool Remove
        {
            get { return (bool)GetValue(RemoveProperty); }
            set { SetValue(RemoveProperty, value); }
        }

        public static readonly DependencyProperty ViewOnCreateProperty =
            DependencyProperty.Register("ViewOnCreate", typeof(bool), typeof(EntityBase), new UIPropertyMetadata(true));
        public bool ViewOnCreate
        {
            get { return (bool)GetValue(ViewOnCreateProperty); }
            set { SetValue(ViewOnCreateProperty, value); }
        }

        public event Func<object> Creating;
        public event Func<object> Finding;
        public event Func<object, object> Viewing;
        public event Func<object, bool> Removing;

        public event EntityChangedEventHandler EntityChanged;

        static EntityBase()
        {
            LineBase.TypeProperty.OverrideMetadata(typeof(EntityBase), 
                new UIPropertyMetadata((d, e) => ((EntityBase)d).SetType((Type)e.NewValue)));
        }

        public EntityBase()
        {
            this.CommandBindings.Add(new CommandBinding(CreateCommand, btCreate_Click));
            this.CommandBindings.Add(new CommandBinding(FindCommand, btFind_Click));
            this.CommandBindings.Add(new CommandBinding(RemoveCommand, btRemove_Click));
            this.CommandBindings.Add(new CommandBinding(ViewCommand, btView_Click));

          
        }

        public bool dynamicReadOnly;
        public bool DynamicReadOnly
        {
            get { return dynamicReadOnly; }
            set
            {
                if (value != dynamicReadOnly)
                {
                    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(Common.IsReadOnlyProperty, typeof(EntityBase));

                    if (value)
                        dpd.AddValueChanged(this, IsReadOnlyChanged);
                    else
                        dpd.RemoveValueChanged(this, IsReadOnlyChanged);

                    dynamicReadOnly = true;
                }
            }
        }

        void IsReadOnlyChanged(object sender, EventArgs e)
        {
            UpdateVisibility();
        }

        protected internal override DependencyProperty CommonRouteValue()
        {
            return EntityProperty;
        }

        private void SetType(Type type)
        {
            if (type.IsLite())
            {
                CleanLite = true;
                CleanType = Reflector.ExtractLite(type);
            }
            else
            {
                CleanLite = false;
                CleanType = type;
            }
        }

        protected internal Type CleanType { get; private set; }
        protected internal bool CleanLite { get; private set; }

        protected bool isUserInteraction = false;

        protected void SetEntityUserInteraction(object entity)
        {
            try
            {
                isUserInteraction = true;
                Entity = entity;
            }
            finally
            {
                isUserInteraction = false;
            }
        }
        

        public override void OnLoad(object sender, RoutedEventArgs e)
        {
            base.OnLoad(sender, e);

            if (DesignerProperties.GetIsInDesignMode(this))
                return;

            if (this.NotSet(EntityBase.EntityTemplateProperty))
            {
                var type = Type;

                if (this is EntityCombo && !type.IsLite()) //Allways going to be lite
                    type = Reflector.GenerateLite(type);

                EntityTemplate = Navigator.FindDataTemplate(this, type);
            }

            if (this.NotSet(EntityBase.CreateProperty) && Create && Implementations == null)
                Create = Navigator.IsCreable(CleanType, false);

            if (this.NotSet(EntityBase.ViewProperty) && View && Implementations == null)
                View = Navigator.IsViewable(CleanType, false);

            if (this.NotSet(EntityBase.FindProperty) && Find)
            {
                if (Implementations == null)
                    Find = Navigator.IsFindable(CleanType);
                if (Implementations is ImplementedByAllAttribute)
                    Find = false;
            }

            if (this.NotSet(EntityBase.ViewOnCreateProperty) && ViewOnCreate && !View)
                ViewOnCreate = false;

            if (this.NotSet(EntityBase.ViewButtonsProperty) && CleanLite)
                ViewButtons = ViewButtons.Save;

            UpdateVisibility();
        }


        protected virtual void UpdateVisibility()
        {
        }


        protected virtual bool CanRemove()
        {
            return Entity != null && Remove && !Common.GetIsReadOnly(this);
        }

        protected bool CanView()
        {
            return CanView(Entity);
        }

        protected virtual bool CanView(object entity)
        {
            if (entity == null)
                return false;

            if (View && this.NotSet(ViewProperty) && Implementations != null)
            {
                Type runtimeType = CleanLite ? ((Lite)entity).RuntimeType : entity.GetType();

                return Navigator.IsViewable(runtimeType, false);
            }
            else
                return View;
        }

        protected virtual bool CanFind()
        {
            return Entity == null && Find && !Common.GetIsReadOnly(this);
        }

        protected virtual bool CanCreate()
        {
            return Entity == null && Create && !Common.GetIsReadOnly(this);
        }

        protected virtual void btCreate_Click(object sender, RoutedEventArgs e)
        {
            object entity = OnCreate();

            if (entity != null)
                SetEntityUserInteraction(entity);
        }

        protected virtual void btFind_Click(object sender, RoutedEventArgs e)
        {
            object entity = OnFinding();

            if (entity != null)
                SetEntityUserInteraction(entity);
        }

        protected virtual void btView_Click(object sender, RoutedEventArgs e)
        {
            object entity = OnViewing(Entity, false);

            if (entity != null)
                SetEntityUserInteraction(entity);
        }

        protected virtual void btRemove_Click(object sender, RoutedEventArgs e)
        {
            if (OnRemoving(Entity))
                SetEntityUserInteraction(null);
        }

        protected virtual void OnEntityChanged(object oldValue, object newValue)
        {
            if (EntityChanged != null)
                EntityChanged(this, isUserInteraction, oldValue, newValue);

            UpdateVisibility();
        }

        public Type SelectType()
        {
            if (Implementations == null)
                return CleanType;
            else if (Implementations.IsByAll)
                throw new InvalidOperationException("ImplementedByAll is not supported for this operation, override the event");
            else
                return Navigator.SelectType(this.FindCurrentWindow(), ((ImplementedByAttribute)Implementations).ImplementedTypes);
        }

        protected object OnCreate()
        {
            if (!CanCreate())
                return null;

            object value;
            if (Creating == null)
            {
                Type type = SelectType();
                if (type == null)
                    return null;
                   
                object entity = Constructor.Construct(type, this.FindCurrentWindow());

                value = Server.Convert(entity, Type);
            }
            else
                value = Creating();

            if (value == null)
                return null;

            if (ViewOnCreate)
            {
                value = OnViewing(value, true);
            }

            return value;
        }

        protected object OnFinding()
        {
            if (!CanFind())
                return null;

            object value;
            if (Finding == null)
            {
                Type type = SelectType();
                if (type == null)
                    return null;

                value = Navigator.Find(new FindOptions { QueryName = type });
            }
            else
                value = Finding();

            if (value == null)
                return null;

            return Server.Convert(value, Type);
        }

        public virtual PropertyRoute GetEntityTypeContext()
        {
            return Common.GetTypeContext(this);
        }

        protected object OnViewing(object entity, bool creating)
        {
            if (!CanView(entity))
                return null;

            if (Viewing != null)
                return Viewing(entity);

            bool isReadOnly = Common.GetIsReadOnly(this) && !creating;

            if (ViewButtons == ViewButtons.Ok)
            {
                var options = new ViewOptions
                {
                    TypeContext = CleanType.IsEmbeddedEntity() ? GetEntityTypeContext() : null, 
                };

                if (isReadOnly)
                    options.ReadOnly = isReadOnly;

                return Navigator.ViewUntyped(entity, options);
            }
            else
            {
                var options = new NavigateOptions();

                if (isReadOnly)
                    options.ReadOnly = isReadOnly;

                Navigator.NavigateUntyped(entity, options);

                return null;
            }
        }

        protected bool OnRemoving(object entity)
        {
            if (!CanRemove())
                return false;

            return Removing == null ? true : Removing(entity);
        }
    }

    public delegate void EntityChangedEventHandler(object sender, bool userInteraction, object oldValue, object newValue);
}

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