Click here to Skip to main content
15,894,180 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 1 – Southwind Entities

Rate me:
Please Sign up or sign in to vote.
4.50/5 (12 votes)
14 Nov 2012LGPL315 min read 41.5K   2K   52  
Tutorial focused in writing the entities using Signum Framework, a Win/Web LINQ-enabled framework for writing data-centric applications.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using System.Data;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using Signum.Utilities.DataStructures;
using Signum.Utilities;
using Signum.Utilities.Reflection;
using System.Reflection;
using Signum.Entities.Reflection;
using System.ComponentModel;  
using System.Collections;

namespace Signum.Entities
{
    [AttributeUsage(AttributeTargets.Field)]
    public sealed class UniqueIndexAttribute : Attribute
    {
        public bool AllowMultipleNulls { get; set; }
    }

    [Serializable]
    public abstract class Implementations : Attribute
    {
        public abstract bool IsByAll { get; }
        public abstract bool ImplementedBy(Type type);
    }

    [Serializable, AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Interface)]
    public sealed class ImplementedByAttribute : Implementations
    {
        Type[] implementedTypes;

        public Type[] ImplementedTypes
        {
            get { return implementedTypes; }
        }

        public ImplementedByAttribute(params Type[] types)
        {
            implementedTypes = types;
        }

        public override bool IsByAll
        {
            get { return false; }
        }

        public override bool ImplementedBy(Type type)
        {
            return implementedTypes.Contains(type); 
        }
    }

    [Serializable, AttributeUsage(AttributeTargets.Field | AttributeTargets.Class | AttributeTargets.Interface)]
    public sealed class ImplementedByAllAttribute : Implementations
    {
        public ImplementedByAllAttribute()
        {
        }

        public override bool IsByAll
        {
            get { return true; }
        }

        public override bool ImplementedBy(Type type)
        {
            return false;
        }
    }


    [AttributeUsage(AttributeTargets.Field)]
    public sealed class IgnoreAttribute : Attribute
    {
    }


    [AttributeUsage(AttributeTargets.Field)]
    public sealed class NotNullableAttribute : Attribute
    {
    }

    [AttributeUsage(AttributeTargets.Field)]
    public sealed class NullableAttribute : Attribute
    {
    }


    [AttributeUsage(AttributeTargets.Field)]
    public sealed class SqlDbTypeAttribute : Attribute
    {
        SqlDbType? type;
        int? size;
        int? scale;

        public SqlDbType SqlDbType
        {
            get { return type.Value; }
            set { type = value; }
        }

        public bool HasSqlDbType
        {
            get { return type.HasValue; }
        }

        public int Size
        {
            get { return size.Value; }
            set { size = value; }
        }

        public bool HasSize
        {
            get { return size.HasValue; }
        }

        public int Scale
        {
            get { return scale.Value; }
            set { scale = value; }
        }

        public bool HasScale
        {
            get { return scale.HasValue; }
        }
    }

    [AttributeUsage(AttributeTargets.Field)]
    public sealed class NotifyCollectionChangedAttribute : Attribute
    {

    }

    [AttributeUsage(AttributeTargets.Field)]
    public sealed class NotifyChildPropertyAttribute : Attribute
    {
        
    }

    [AttributeUsage(AttributeTargets.Field)]
    public sealed class ValidateChildPropertyAttribute : Attribute
    {

    }

    [AttributeUsage(AttributeTargets.Field)]
    public class ForceForeignKey : Attribute
    {

    }

    //Used by NotifyCollectionChangedAttribute, NotifyChildPropertyAttribute, ValidateChildPropertyAttribute
    internal static class AttributeManager<T>
        where T : Attribute
    {
        //Consider using ImmutableAVLTree instead
        readonly static Dictionary<Type, TypeAttributePack> fieldAndProperties = new Dictionary<Type, TypeAttributePack>();
       
        static TypeAttributePack GetFieldsAndProperties(Type type)
        {
            lock (fieldAndProperties)
            {
                return fieldAndProperties.GetOrCreate(type, () =>
                {
                    var list = Reflector.InstanceFieldsInOrder(type).Where(fi=>fi.HasAttribute<T>()).ToList();

                    if (list.Count == 0)
                        return null;

                    return new TypeAttributePack
                    {
                        Fields = list.Select(fi => ReflectionTools.CreateGetterUntyped(type, fi)).ToArray(),
                        PropertyNames = list.Select(fi => Reflector.FindPropertyInfo(fi).Name).ToArray()
                    };
                });
            }
        }

        public static bool FieldContainsAttribute(Type type, PropertyInfo pi)
        {
            TypeAttributePack pack = GetFieldsAndProperties(type);

            if(pack == null)
                return false;

            return pack.PropertyNames.Contains(pi.Name);
        }

        readonly static object[] EmptyArray = new object[0];

        public static object[] FieldsWithAttribute(ModifiableEntity entity)
        {
            TypeAttributePack pack = GetFieldsAndProperties(entity.GetType());

            if (pack == null)
                return EmptyArray;

            return pack.Fields.Select(f=>f(entity)).ToArray();
        }

        public static string FindPropertyName(ModifiableEntity entity, object fieldValue)
        {
            TypeAttributePack pack = GetFieldsAndProperties(entity.GetType());

            if (pack == null)
                return null;

            int index = pack.Fields.IndexOf(f => f(entity) == fieldValue);

            if (index == -1)
                return null;

            return pack.PropertyNames[index];
        }
    }

    internal class TypeAttributePack
    {
        public Func<object, object>[] Fields;
        public string[] PropertyNames; 
    }
}

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