Click here to Skip to main content
15,891,375 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;
using VmWrapperDemo.Domain;

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

        // Member variables
        private readonly MainWindowViewModel m_ViewModel;

        #endregion

        #region Constructor

        public AddCustomerCommand(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)
        {
            // Create new customer
            var newCustomer = new Customer("Moe Construction, Inc.", "Bill Moe");
            newCustomer.Orders.Add(new Order(new DateTime(2009, 04, 04), 3000));
            newCustomer.Orders.Add(new Order(new DateTime(2009, 04, 18), 2450));

            // Add customer to the collection
            var newWrapperObject = new CustomerVM(newCustomer);
            m_ViewModel.Customers.Add(newWrapperObject);

            // Show results in message box
            var n = m_ViewModel.Customers.Count - 1;
            var sb = new StringBuilder();
            sb.Append(String.Format("New customer count: {0}\r\n", m_ViewModel.Customers.Count));
            sb.Append(String.Format("New view model customer name: {0}\r\n", m_ViewModel.Customers[n].Name));
            sb.Append(String.Format("New domain model customer name: {0}\r\n", m_ViewModel.DomainCustomers[n].Name));
            var message = sb.ToString();
            var caption = "Add Customer";
            var buttons = MessageBoxButton.OK;
            var image = MessageBoxImage.Information;
            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