Click here to Skip to main content
15,891,372 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.5K   3.3K   114  
Adapt NHibernate classes to run in WPF
using System;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace VmWrapperDemo.ViewModel
{
    public class ChangeCustomerNameCommand : ICommand
    {
        #region Fields

        // Member variables
        private MainWindowViewModel m_ViewModel;

        #endregion

        #region Constructor

        public ChangeCustomerNameCommand(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)
        {
            // Set customer name
            var customerNames = new string[]
                                    {
                                        "Sleepy Sonics", "Sneezy Snoozles",
                                        "Dopey Dentists", "Doc Hopper's", "Happy Haven", "Bashful Manor",
                                        "Grumpy Grocers"
                                    };
            var rnd = new Random();
            var n = rnd.Next(0, 6);

            var m = m_ViewModel.SelectedCustomer;
            var currentCustomer = m_ViewModel.Customers[m];
            var oldCustomerName = currentCustomer.Name;
            currentCustomer.Name = customerNames[n];

            // Show results in message box
            var sb = new StringBuilder();
            sb.Append(String.Format("Old customer name: {0}\r\n", oldCustomerName));
            sb.Append(String.Format("New view model customer name: {0}\r\n", currentCustomer.Name));
            sb.Append(String.Format("New domain model customer name: {0}\r\n", m_ViewModel.DomainCustomers[m].Name));
            var message = sb.ToString();
            var caption = "Customer Name Change";
            var buttons = MessageBoxButton.OK;
            var image = MessageBoxImage.Information;
            var result = MessageBox.Show(message, caption, buttons, image);
        }

        #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