Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Signum Framework Principles

, 25 Jul 2011
Explains the philosophy behind Signum Framework, an ORM with a full LINQ Provider that encourages an entities-first approach.
SignumFramework_1.0_Setup.zip
SignumFramework_1.0_Src.zip
SignumFramework
Signum.Engine
Connection
DynamicQuery
Engine
Linq
ExpressionVisitor
Properties
resxsync.exe
Schema
Schema.cd
SchemaBuilder
Signum.Engine.csproj.user
Signum.Engine.csproj.vspscc
SignumFramework.pfx
Signum.Entities
Attributes.cd
Basics
Diagram.cd
DynamicQuery
Properties
resxsync.exe
Reflection
Signum.Entities.csproj.user
Signum.Entities.csproj.vspscc
SignumFramework.pfx
Signum.Services
Properties
Signum.Services.csproj.vspscc
SignumFramework.pfx
Signum.Utilities
DataStructures
IntervalDictionaries
ExpressionTrees
Properties
resxsync.exe
Reflection
Signum.Utilities.csproj.user
Signum.Utilities.csproj.vspscc
SignumFramework.pfx
Synchronization
Signum.Windows
Basics
Controls
ColorUtils
DateUtils
Converters
DynamicQuery
Facades
Images
admin.png
cancel.png
close.png
entity.png
find.png
ok.png
open.png
pin.png
reload.png
save.png
star.png
Localization
MarkupExtensions
Properties
resxsync.exe
Signum.Windows.csproj.user
Signum.Windows.csproj.vspscc
SignumFramework.pfx
Themes
SignumFramework.suo
SignumFramework.vssscc
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)

About the Author

Olmo del Corral
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...

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 26 Jul 2011
Article Copyright 2009 by Olmo del Corral
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid