Click here to Skip to main content
12,505,104 members (64,007 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads

Stats

611.4K views
21.4K downloads
518 bookmarked
Posted

NHibernate Made Simple

, 31 Oct 2007 CPOL
A simple, straightforward tutorial that will get you up to speed on the fundamentals of NHibernate as quickly as possible
NHibernateSimpleDemo
Database
NHibernateSimpleDemo.mdf
NHibernateSimpleDemo_log.ldf
NHibernateSimpleDemo.suo
NHibernateSimpleDemo
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
NHibernateSimpleDemo.exe
NHibernateSimpleDemo.vshost.exe
Controller
Model
BusinessModel.cd
NHibernateSimpleDemo.csproj.user
Persistence
Properties
View
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;

namespace NHibernateSimpleDemo
{
    class Program
    {
        /// <summary>
        /// The main method for the program
        /// </summary>
        /// <remarks>Normally, a Controller class (or classes) would do most of the
        /// work that this method does. The Controller would receive requests from
        /// the UI and manipulate the business model to perform the requested action.</remarks>
        static void Main(string[] args)
        {
            /* Since the Program class represents the UI of the application, it has
             * very limited knowledge of the model--only enough to subscribe to the
             * Populate event. If we wanted to be very strict, we could route that
             * event through the Controller, which would subscribe to the model's
             * event, then fires one of its own to pass the event to the UI. That
             * approach would completely eliminate the UI's knowledge of the model. 
             * In that case, the OrderSystem object would not be a property of the
             * Controller, but a member variable instead. */

            // Initialize 
            Controller controller = new Controller();
            controller.OrderSystem.Populated +=new EventHandler(OrderSystem_Populated);

            // Pause to let the user examine console
            Pause("Completed NHibernate configuration\r\nNext step: Clear database from last run");

            // Clear the database
            controller.ClearDatabase();
            Pause("Completed clearing the database from last run\r\nNext step: Create business objects");

            // Get persistent objects
            controller.CreateBusinessObjects();
            Pause("Completed creating business objects\r\nNext step: Saving business objects to database");

            // Save order system
            controller.SaveBusinessObjects();
            Pause("Completed saving business objects\r\nNext step: Clear business objects from memory");

            // Clear business objects
            controller.ClearBusinessObjects();
            Pause("Completed clearing business objects from memory\r\nNext step: Reload business objects from database");

            // Reload objects
            controller.LoadBusinessObjects();
            Pause("Completed reloading business objects\r\nNext step: Object comparison");

            // Show object comparison
            controller.ShowObjectComparisons();
            Pause("Completed object comparisons\r\nNext step: Deletion test");

            // Perform deletion test
            Notify("Beginning deletion test");
            controller.PerformDeletionTest();
            Pause("Completed deletion test (should be 2 Customers)\r\nApplication run completed");
        }

        #region Event Handlers

        /// <summary>
        /// Prints a customer and order list when the order system is populated.
        /// </summary>
        static void OrderSystem_Populated(object sender, EventArgs e)
        {
            OrderSystem orderSystem = sender as OrderSystem;
            ShowCustomersAndOrders(orderSystem);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Outputs a message to the user on the console.
        /// </summary>
        /// <param name="message">The message to output.</param>
        private static void Notify(string message)
        {
            Console.WriteLine("{0}\r\n", message);
        }

        /// <summary>
        /// Pauses until the user hits Enter.
        /// </summary>
        private static void Pause(string prompt)
        {
            Console.WriteLine("\r\n{0}", prompt);
            Console.Write("\r\nHit the Enter key to continue");
            Console.ReadLine();
        }
        
        /// <summary>
        /// Prints a list of customers and their orders.
        /// </summary>
        /// <param name="m_OrderSystem">The OrderSytem object for the application.</param>
        private static void ShowCustomersAndOrders(OrderSystem orderSystem)
        {
            // Initialize
            Customer customer = null;
            Order order = null;

            // Print header
            Console.WriteLine("Customers and their orders:");
            Console.WriteLine("---------------------------------");

            // Print customers and orders
            for (int i = 0; i < orderSystem.Customers.Count; i++)
            {
                customer = orderSystem.Customers[i];
                Console.WriteLine("Customer #{0}: {1}", i.ToString(), customer.Name);
                for (int j = 0; j < customer.Orders.Count; j++)
                {
                    order = customer.Orders[j];
                    Console.WriteLine("-->Order dated {0}", order.Date);

                }

                for (int k = 0; k < order.OrderItems.Count; k++)
                {
                    Console.WriteLine("---->Order item #{0}: {1}", k.ToString(), order.OrderItems[k].Name);
                }
            }

            // Skip line
            Console.WriteLine();
        }

        #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)

Share

About the Author

David Veeneman
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.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 31 Oct 2007
Article Copyright 2007 by David Veeneman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid