Click here to Skip to main content
15,880,405 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.7K   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.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Signum.Entities;
using System.Windows.Threading;
using Signum.Utilities.DataStructures;
using Signum.Utilities;
using System.Linq;
using System.Collections.Generic;
using Signum.Entities.Reflection;
using System.Windows.Input;

namespace Signum.Windows
{
	public partial class NormalWindow
	{
        public static readonly DependencyProperty MainControlProperty =
            DependencyProperty.Register("MainControl", typeof(Control), typeof(NormalWindow), new UIPropertyMetadata((d,e)=>((NormalWindow)d).MainControlChanged(e)));
        public Control MainControl
        {
            get { return (Control)GetValue(MainControlProperty); }
            set { SetValue(MainControlProperty, value); }
        }
        
        public static readonly DependencyProperty ButtonsProperty =
            DependencyProperty.Register("Buttons", typeof(ViewButtons), typeof(NormalWindow), new FrameworkPropertyMetadata(ViewButtons.OkCancel, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(ButtonsChanged)));
        public ViewButtons Buttons
        {
            get { return (ViewButtons)GetValue(ButtonsProperty); }
            set { SetValue(ButtonsProperty, value); }
        }

		public NormalWindow()
		{
			this.InitializeComponent();

            OnButtonsChanged();
            this.DataContextChanged+=new DependencyPropertyChangedEventHandler(NormalWindow_DataContextChanged);
            RefreshEnabled();

            this.Loaded += new RoutedEventHandler(NormalWindow_Loaded);
		}

        void NormalWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.btSave.IsEnabled = !Common.GetIsReadOnly(this);
            this.Width = this.ActualWidth;
            this.Height = this.ActualHeight;
            this.SizeToContent = SizeToContent.Manual;
        }

        private void MainControlChanged(DependencyPropertyChangedEventArgs e)
        {
            leftNavigationPanel.IHasQuickLinks = e.NewValue as IHaveQuickLinks;
        }

        void NormalWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
             RefreshEnabled();
        }

        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.S && (e.KeyboardDevice.Modifiers & ModifierKeys.Control) != 0 && Buttons == ViewButtons.SaveClose)
            {
                Save(); 
            }
        }

        public static void ButtonsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((NormalWindow) d).OnButtonsChanged();
        }

        private void OnButtonsChanged()
        {
            ViewButtons b = this.Buttons;
            this.btOk.Visibility = b == ViewButtons.OkCancel ? Visibility.Visible : Visibility.Collapsed;
            this.btOkSaving.Visibility = b == ViewButtons.OkCancelSaving ? Visibility.Visible : Visibility.Collapsed;
            this.btCancel.Visibility = (b == ViewButtons.OkCancel || b == ViewButtons.OkCancelSaving) ? Visibility.Visible : Visibility.Collapsed;
            this.btClose.Visibility = b == ViewButtons.SaveClose ? Visibility.Visible : Visibility.Collapsed;
            this.btSave.Visibility = b == ViewButtons.SaveClose ? Visibility.Visible : Visibility.Collapsed;
        }

        private void OkSaving_Click(object sender, RoutedEventArgs e)
        {
            if (!AssertErrors())
                return;

            btOkSaving.IsEnabled = false;
            IdentifiableEntity ei = (IdentifiableEntity)base.DataContext;
            IdentifiableEntity nueva = null;
            Async.Do(this,
                () => nueva = Server.Save(ei),
                () => { base.DataContext = null; base.DataContext = nueva; base.DialogResult = true; this.Close(); },
                () => btOkSaving.IsEnabled = true);
            
        }

        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = false;
        }


        private void Save_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void Save()
        {
            if (!HasChanges())
            {
                MessageBox.Show(Properties.Resources.NoChanges);

                return;
            }

            if (!AssertErrors())
                return;

            btSave.IsEnabled = false;
            IdentifiableEntity ei = (IdentifiableEntity)base.DataContext;
            IdentifiableEntity nueva = null;
            Async.Do(this,
                () => nueva = Server.Save(ei),
                () => { base.DataContext = null; base.DataContext = nueva; },
                () => btSave.IsEnabled = true);
        }

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

        bool HasChanges()
        {
            IdentifiableEntity ei = (IdentifiableEntity)base.DataContext;
            return GraphExplorer.FromRoot(ei).Any(a => a.SelfModified);
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
           
            base.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            if (Buttons == ViewButtons.SaveClose && HasChanges())
            {
                var result = MessageBox.Show(Properties.Resources.SaveChanges, Properties.Resources.ThereAreChanges,
                    MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.No);

                if (result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                    return;
                }

                if (result == MessageBoxResult.Yes)
                    DataContext = Server.Save((IdentifiableEntity)DataContext);
            }
        }

        void RefreshEnabled()
        {
            btReload.IsEnabled = (DataContext as IdentifiableEntity).TryCS(ei => !ei.IsNew) ?? false; 
        }

        private void btRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (!HasChanges() || MessageBox.Show(Properties.Resources.ThereAreChangesContinue, Properties.Resources.ThereAreChanges,
                MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK) == MessageBoxResult.OK)
            {
                IdentifiableEntity ei = (IdentifiableEntity)DataContext;
                DataContext = null;  // por la definicion de equal no captura los cambios sino
                DataContext = Server.Retrieve(ei.GetType(), ei.Id); 
            }
        }

	}
}

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