Click here to Skip to main content
15,883,705 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.8K   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 System.Windows;
using Signum.Entities;
using Signum.Utilities.DataStructures;
using System.Collections.ObjectModel;
using Signum.Utilities;
using System.Windows.Controls;
using System.ComponentModel;
using Signum.Entities.Reflection;

namespace Signum.Windows
{
    public class AdminBase:Window
    {
        public virtual object GetEntities() 
        {
            throw new NotImplementedException();
        }

        public virtual void SetEntities(object value)
        {
            throw new NotImplementedException();
        }

        public virtual void RetrieveEntities()
        {
            throw new NotImplementedException();
        }
        
        /// <summary>
        /// Could be called Async
        /// </summary>
        public virtual object SaveEntities(object value)
        {
            throw new NotImplementedException();
        }

        public virtual void UpdateInterface()
        {
            throw new NotImplementedException();
        }

        public virtual Button GetSaveButton()
        {
            throw new NotImplementedException();
        }

        public void Retrieve()
        {
            RetrieveEntities();
            UpdateInterface();
        }

        public virtual bool HasChanges()
        {
            var entidades = (List<IdentifiableEntity>)GetEntities();
            return DirectedGraph<Modifiable>.Union(entidades.Select(e => GraphExplorer.FromRoot(e))).Any(a => a.SelfModified);
        }

        protected void Reload_Click(object sender, RoutedEventArgs e)
        {
            if (!HasChanges() || MessageBox.Show(Properties.Resources.ThereAreChangesContinue, Properties.Resources.ThereAreChanges,
                MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK) == MessageBoxResult.OK)
            {
                Retrieve();
            }
        }

        public bool AssertErrors()
        {
            string error = Errors();
            if (error.HasText())
            {
                MessageBox.Show(Properties.Resources.ImpossibleToSaveIntegrityCheckFailed + error, Properties.Resources.ThereAreErrors, MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
            return true;
        }


        public virtual string Errors()
        {
            var entidades = (List<IdentifiableEntity>)GetEntities();
            return entidades.Select(a => a.FullIntegrityCheck()).Where(e => e.HasText()).ToString("\r\n");
        }


        protected override void OnClosing(CancelEventArgs e)
        {
            if (HasChanges())
            {
                var result = MessageBox.Show(Properties.Resources.SaveChanges, Properties.Resources.ThereAreChanges,
                    MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No);

                if (result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                }
                else if (result == MessageBoxResult.Yes)
                {
                    if (AssertErrors())
                    {
                        SetEntities(SaveEntities(GetEntities()));
                        UpdateInterface();
                    }
                    else
                        e.Cancel = true;
                }
                else if (result == MessageBoxResult.No)
                {
                    return; 
                }
            }

            base.OnClosing(e);
        }

        protected void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        protected void Save_Click(object sender, RoutedEventArgs e)
        {
            if (!HasChanges())
            {
                MessageBox.Show(Properties.Resources.NoChangeHaveBeenFound, Properties.Resources.NoChanges, MessageBoxButton.OK, MessageBoxImage.Hand );
                return;
            }

            if (!AssertErrors())
                return;

            object value = GetEntities();
            Button button = GetSaveButton();

            button.IsEnabled = false;
            Async.Do(this,
               () => value = SaveEntities(value),
               () => { SetEntities(value); UpdateInterface(); },
               () => button.IsEnabled = true);
        }
    }
}

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