Click here to Skip to main content
15,891,933 members
Articles / Desktop Programming / WPF

The WPF-NHibernate Toolkit

Rate me:
Please Sign up or sign in to vote.
4.98/5 (23 votes)
16 Jan 2010CPOL28 min read 159.7K   3.3K   114  
Adapt NHibernate classes to run in WPF
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Input;
using VmWrapperExpress.Services;
using VmWrapperExpress.Utility;

namespace VmWrapperExpress.ViewModel
{
    public class OpenSettingsFile : ICommand
    {
        #region Fields

        // Member variables
        private MainWindowViewModel m_ViewModel;

        #endregion

        #region Constructor

        /// <summary>
        /// The default constructor for this object.
        /// </summary>
        /// <param name="viewModel">The ViewModel parent of this command.</param>
        public OpenSettingsFile(MainWindowViewModel viewModel)
        {
            m_ViewModel = viewModel;
        }

        #endregion

        #region ICommand Members

        /// <summary>
        /// Whether this command can be executed.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Fires when the CanExecute status of this command changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Invokes this command to perform its intended task.
        /// </summary>
        public void Execute(object parameter)
        {
            // Initialize
            var dialogCaption = "Open Setting File";
            var fileFilter = "VM Wrapper Express settings files (*.vmwe)|*.vmwe|All files (*.*)|*.*";

            // Get settings file path
            var filePath = CommonDialogs.GetFileToOpen(dialogCaption, fileFilter);
            if (String.IsNullOrEmpty(filePath)) return;

            // Load app settings
            AppSettings appSettings;
            Stream stream = File.Open(filePath, FileMode.Open);
            var formatter = new BinaryFormatter();
            appSettings = (AppSettings) formatter.Deserialize(stream);
            stream.Close();

            // Load assembly
            AssemblyLoader.LoadAssembly(m_ViewModel, appSettings.AssemblyPath);
            var assemblyDM = m_ViewModel.DomainObject;

            // Set wrapper class namespace
            m_ViewModel.WrapperClassNamespace = appSettings.WrapperClassNamespace;

            // Set selections
            foreach (var namespaceDM in assemblyDM.Namespaces)
            {
                // Set namespace selection
                var targetNamespace = String.Format("namespace:{0}", namespaceDM.Name);
                namespaceDM.IsSelected = appSettings.Selections.Contains(targetNamespace);

                // Set selections for classes in this namespace
                foreach (var classDM in namespaceDM.Classes)
                {
                    // Set class selection
                    var targetClass = String.Format("class:{0}", classDM.Name);
                    classDM.IsSelected = appSettings.Selections.Contains(targetClass);

                    // Set selections for properties in this class
                    foreach (var propertyDM in classDM.Properties)
                    {
                        // Set property selection
                        var targetProperty = String.Format("property:{0}", propertyDM.Name);
                        propertyDM.IsSelected = appSettings.Selections.Contains(targetProperty);
                    }
                }
            }
        }

        #endregion
    }
}

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) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions