Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET

Signum Framework Principles

Rate me:
Please Sign up or sign in to vote.
4.74/5 (27 votes)
25 Jul 2011CPOL18 min read 98.9K   1.1K   86  
Explains the philosophy behind Signum Framework, an ORM with a full LINQ Provider that encourages an entities-first approach.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Signum.Utilities;
using System.Windows;
using Signum.Entities;
using Signum.Entities.Basics;
using System.Reflection;
using Signum.Entities.Reflection;
using Signum.Entities.DynamicQuery;

namespace Signum.Windows
{
    public static class Constructor
    {
        public static ConstructorManager ConstructorManager = new ConstructorManager() {  Constructors = new Dictionary<Type,Func<Window,object>>()};

        public static object Construct(Type type, Window window)
        {
            return ConstructorManager.Construct(type, window); 
        }

        public static T Construct<T>(Window window)
        {
            return (T)Construct(typeof(T), window);
        }
    }
    
    public class ConstructorManager
    {
        public Dictionary<Type, Func<Window, object>> Constructors;

        public virtual object Construct(Type type, Window window)
        {
            Func<Window, object> c = Constructors.TryGetC(type);

            object result = c != null ? c(window) : DefaultContructor(type, window); 

            return result;
        }

        public static object DefaultContructor(Type type, Window window)
        {
            object result = Activator.CreateInstance(type);

            AddFilterProperties(window, result);

            return result;
        }

        public static object AddFilterProperties(Window window, object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("result");

            Type type = obj.GetType();

            if (window is SearchWindow)
            {
                var pairs = from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                            join fo in ((SearchWindow)window).CurrentFilters().Where(fo => fo.Operation == FilterOperation.EqualTo) on pi.Name equals fo.Column.Name
                            where Server.CanConvert(fo.Value, pi.PropertyType) && fo.Value != null
                            select new { pi, fo };

                foreach (var p in pairs)
                {
                    p.pi.SetValue(obj, Server.Convert(p.fo.Value, p.pi.PropertyType), null);
                }
            }

            return obj; 
        }
    }

    public static class NotesProvider
    {
        public static NotesProviderManager Manager; 
    }

    public class NotesProviderManager
    {
        public Func<IdentifiableEntity, INoteDN> CreateNote;

        /// <summary>
        /// Null means hide notes
        /// </summary>
        public Func<IdentifiableEntity, List<Lazy<INoteDN>>> RetrieveNotes;
    }
}

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 Code Project Open License (CPOL)


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