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
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
Northwind.zip
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
Southwind-Part2-Logic.zip
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Signum.Utilities;
using Signum.Entities.Reflection;
using Signum.Utilities.Reflection;
using Signum.Entities.Properties;
using System.Linq.Expressions;
using Signum.Utilities.ExpressionTrees;

namespace Signum.Entities
{
    [Serializable]
    public class PropertyRoute : IEquatable<PropertyRoute>
    {
        Type type;
        public PropertyRouteType PropertyRouteType { get; private set; } 
        public PropertyInfo PropertyInfo { get; private set;}
        public PropertyRoute Parent { get; private set;}

        public static PropertyRoute Construct<T>(Expression<Func<T, object>> expression)
            where T : IRootEntity
        {
            PropertyRoute result = Root(typeof(T));

            foreach (var mi in Reflector.GetMemberList(expression))
            {
                result = result.Add((PropertyInfo)mi);
            }
            return result;
        }

        public PropertyRoute Add(string propertyName)
        {
            PropertyInfo pi = Type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);

            if (pi == null)
                throw new InvalidOperationException("{0} of {1} does not exist".Formato(propertyName, this));

            return Add(pi);
        }

        public PropertyRoute Add(PropertyInfo propertyInfo)
        {
            if (this.Type.IsIIdentifiable())
            {
                Implementations imp = GetImplementations();

                ImplementedByAttribute ib = imp as ImplementedByAttribute;
                if (ib != null && ib.ImplementedTypes.Length == 1)
                {
                    return new PropertyRoute(Root(ib.ImplementedTypes.Single()), propertyInfo); 
                }

                if (imp != null)
                    throw new InvalidOperationException("Attempt to make a PropertyRoute on a {0}. Cast first".Formato(imp.GetType()));

                return new PropertyRoute(Root(this.Type), propertyInfo);
            }
            return new PropertyRoute(this, propertyInfo);
        }

        PropertyRoute(PropertyRoute parent, PropertyInfo propertyInfo)
        {
            if (propertyInfo == null)
                throw new ArgumentNullException("propertyInfo");

            if (parent == null)
                throw new ArgumentNullException("parent");

            if (!parent.Type.FollowC(a => a.BaseType).Contains(propertyInfo.DeclaringType))
            {
                if (parent.Type.GetInterfaces().Contains(propertyInfo.DeclaringType))
                {
                    var otherProperty = parent.Type.FollowC(a => a.BaseType)
                        .Select(a => a.GetProperty(propertyInfo.Name, BindingFlags.Public | BindingFlags.Instance)).NotNull().First();

                    if (otherProperty == null)
                        throw new ArgumentException("PropertyInfo {0} not found on {1}".Formato(propertyInfo.PropertyName(), parent.Type));

                    propertyInfo = otherProperty;
                }
                else
                {
                    throw new ArgumentException("PropertyInfo {0} not found on {1}".Formato(propertyInfo.PropertyName(), parent.Type));
                }
            }
            if (parent.Type.IsIIdentifiable() && parent.PropertyRouteType != PropertyRouteType.Root)
                throw new ArgumentException("Parent can not be a non-root Identifiable");

            if (Reflector.IsMList(parent.Type))
            {
                if (propertyInfo.Name != "Item")
                    throw new NotSupportedException("PropertyInfo {0} is not supported".Formato(propertyInfo.Name)); 

                PropertyRouteType = PropertyRouteType.MListItems;
            }
            else if (Reflector.IsLite(parent.Type))
            {
                if (propertyInfo.Name != "Entity" && propertyInfo.Name != "EntityOrNull")
                    throw new NotSupportedException("PropertyInfo {0} is not supported".Formato(propertyInfo.Name));

                PropertyRouteType = PropertyRouteType.LiteEntity;
            }
            else if (typeof(ModifiableEntity).IsAssignableFrom(parent.Type))
            {
                PropertyRouteType = PropertyRouteType.Property;
            }
            else
                throw new NotSupportedException("Properties of {0} not supported".Formato(parent.Type));

            this.PropertyInfo = propertyInfo;
            this.Parent = parent;
        }

        public static PropertyRoute Root(Type propertyRouteRoot)
        {
            return new PropertyRoute(propertyRouteRoot);
        }

        PropertyRoute(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (!typeof(IRootEntity).IsAssignableFrom(type))
                throw new ArgumentException("Type must implement IPropertyRouteRoot");

            this.type = type;
            this.PropertyRouteType = PropertyRouteType.Root;
        }

        public Type Type { get { return type ?? PropertyInfo.PropertyType; } }
        public Type RootType { get { return type ?? Parent.RootType; } }

        public PropertyInfo[] Properties
        {
            get
            {
                return this.FollowC(a => a.Parent).Select(a => a.PropertyInfo).Reverse().Skip(1).ToArray();
            }
        }

        public override string ToString()
        {
            switch (PropertyRouteType)
            {
                case PropertyRouteType.Root:
                    return "({0})".Formato(type.Name);
                case PropertyRouteType.Property:
                    return Parent.ToString() + (Parent.PropertyRouteType == PropertyRouteType.MListItems ? "" : ".") + PropertyInfo.Name;
                case PropertyRouteType.MListItems:
                    return Parent.ToString() + "/";
                case PropertyRouteType.LiteEntity:
                    return Parent.ToString() + ".Entity";
            }
            throw new InvalidOperationException();
        }

        public string PropertyString()
        {
            switch (PropertyRouteType)
            {
                case PropertyRouteType.Root:
                    throw new InvalidOperationException("Root has no PropertyString");
                case PropertyRouteType.Property:
                    switch (Parent.PropertyRouteType)
                    {
                        case PropertyRouteType.Root: return PropertyInfo.Name;
                        case PropertyRouteType.Property: return Parent.PropertyString() + "." + PropertyInfo.Name;
                        case PropertyRouteType.MListItems: return Parent.PropertyString() + PropertyInfo.Name;
                        default: throw new InvalidOperationException();
                    }
                case PropertyRouteType.MListItems:
                    return Parent.PropertyString() + "/";
            }
            throw new InvalidOperationException();
        }


        public static PropertyRoute Parse(Type type, string route)
        {
            PropertyRoute result = PropertyRoute.Root(type);

            foreach (var part in route.Replace("/", ".Item.").Split('.'))
            {
                result = result.Add(part);
            }

            return result;
        }

        public static void SetFindImplementationsCallback(Func<PropertyRoute, Implementations> findImplementations)
        {
            FindImplementations = findImplementations;
        }

        static Func<PropertyRoute, Implementations> FindImplementations;

        public Implementations GetImplementations()
        {
            if (FindImplementations == null)
                throw new InvalidOperationException("PropertyRoute.FindImplementations not set");

            return FindImplementations(this);
        }

        public static void SetIsAllowedCallback(Func<PropertyRoute, bool> isAllowed)
        {
            IsAllowedCallback = isAllowed;
        }

        static Func<PropertyRoute, bool> IsAllowedCallback;
        
        public bool IsAllowed()
        {
            if (IsAllowedCallback != null)
                return IsAllowedCallback(this);

            return true;
        }


        public static List<PropertyRoute> GenerateRoutes(Type type)
        {
            PropertyRoute root = PropertyRoute.Root(type);
            List<PropertyRoute> result = new List<PropertyRoute>();

            foreach (var pi in Reflector.PublicInstancePropertiesInOrder(type))
            {
                PropertyRoute property = root.Add(pi);
                result.Add(property);

                if (Reflector.IsEmbeddedEntity(pi.PropertyType))
                    result.AddRange(GenerateEmbeddedProperties(property));

                if (Reflector.IsMList(pi.PropertyType))
                {
                    Type colType = pi.PropertyType.ElementType();
                    if (Reflector.IsEmbeddedEntity(colType))
                        result.AddRange(GenerateEmbeddedProperties(property.Add("Item")));
                }
            }

            return result;
        }

        static List<PropertyRoute> GenerateEmbeddedProperties(PropertyRoute embeddedProperty)
        {
            List<PropertyRoute> result = new List<PropertyRoute>();
            foreach (var pi in Reflector.PublicInstancePropertiesInOrder(embeddedProperty.Type))
            {
                PropertyRoute property = embeddedProperty.Add(pi);
                result.AddRange(property);

                if (Reflector.IsEmbeddedEntity(pi.PropertyType))
                    result.AddRange(GenerateEmbeddedProperties(property));
            }

            return result;
        }

        public bool Equals(PropertyRoute other)
        {
            if (other.PropertyRouteType != this.PropertyRouteType)
                return false;

            if (Type != other.Type)
                return false;

            if (!ReflectionTools.PropertyEquals(PropertyInfo, other.PropertyInfo))
                return false;

            return object.Equals(Parent, other.Parent); 
        }

        public override int GetHashCode()
        {
            if (PropertyInfo != null)
                return PropertyInfo.Name.GetHashCode();

            return type.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            PropertyRoute other = obj as PropertyRoute;

            if (obj == null)
                return false;

            return Equals(other);
        }
    }

    public interface IImplementationsFinder
    {
        Implementations FindImplementations(PropertyRoute route);
    }

    public enum PropertyRouteType
    {
        Root,
        Property,
        LiteEntity, 
        MListItems,
    }

   
}

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)

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