Click here to Skip to main content
15,884,821 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections;
using Signum.Utilities.Reflection;
using Signum.Entities;
using Signum.Utilities;

namespace Signum.Windows
{
    public class EntityListBase : EntityBase
    {
        public static readonly DependencyProperty EntitiesProperty =
          DependencyProperty.Register("Entities", typeof(IList), typeof(EntityListBase), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d, e) => ((EntityListBase)d).EntitiesChanged(e)));
        public IList Entities
        {
            get { return (IList)GetValue(EntitiesProperty); }
            set { SetValue(EntitiesProperty, value); }
        }

        public static readonly DependencyProperty EntitiesTypeProperty =
          DependencyProperty.Register("EntitiesType", typeof(Type), typeof(EntityListBase), new UIPropertyMetadata(null, (d, e) => ((EntityListBase)d).EntitiesTypeChanged((Type)e.NewValue)));

        public Type EntitiesType
        {
            get { return (Type)GetValue(EntitiesTypeProperty); }
            set { SetValue(EntitiesTypeProperty, value); }
        }

        private void EntitiesTypeChanged(Type type)
        {
 	        Type = type.ElementType().ThrowIfNullC("EntitiesType must be a collection type");
        }

        public new event Func<object> Finding;
        
        protected internal override DependencyProperty CommonRouteValue()
        {
            return EntitiesProperty;
        }

        protected internal override DependencyProperty CommonRouteType()
        {
            return EntitiesTypeProperty;
        }

        protected override bool CanFind()
        {
            return Find && !Common.GetIsReadOnly(this);
        }

        protected override bool CanCreate()
        {
            return Create && !Common.GetIsReadOnly(this);
        }

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

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

                value = Navigator.FindMany(new FindManyOptions { QueryName = type });
            }
            else
                value = Finding();

            if (value == null)
                return null;

            if (value is IEnumerable)
                return ((IEnumerable)value).Cast<object>().Select(o => Server.Convert(o, Type)).ToArray();
            else
                return Server.Convert(value, Type);
        }

        public override PropertyRoute GetEntityTypeContext()
        {
            PropertyRoute tc = base.GetEntityTypeContext();
            if (tc == null)
                return null;

            return tc.Add("Item");
        }

        public IList EnsureEntities()
        {
            if (Entities == null)
                Entities = (IList)Activator.CreateInstance(EntitiesType);
            return Entities;
        }

        public virtual void EntitiesChanged(DependencyPropertyChangedEventArgs e)
        {

        }
    }
}

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)


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

Comments and Discussions