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

NHibernate Best Practices with ASP.NET, 1.2nd Ed.

, 11 Jun 2008 CPOL
This article describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET, generics and unit testing together.
basicsample.zip
BasicSample
BasicSample.Core
DataInterfaces
Domain
Properties
Utils
BasicSample.Data
Properties
BasicSample.resharper
BasicSample.resharper.user
BasicSample.Tests
Data
DaoTestDoubles
Domain
Properties
TestFactories
Web
BasicSample.Web
App_Code
Bin
BasicSample.Core.dll
BasicSample.Core.pdb
BasicSample.Data.dll
BasicSample.Data.pdb
Iesi.Collections.dll
log4net.dll
log4net.dll.refresh
NHibernate.dll
NHibernate.dll.refresh
Solution Items
Iesi.Collections.dll
log4net.dll
NHibernate.dll
nunit.framework.dll
NUnitAsp.dll
Rhino.Mocks.dll
enterprisesample.zip
EnterpriseSample
EnterpriseSample.Core
DataInterfaces
Domain
Dto
EnterpriseSample.Core.csproj.user
Properties
EnterpriseSample.Data
Properties
EnterpriseSample.Presenters
Properties
ViewInterfaces
EnterpriseSample.resharper
EnterpriseSample.resharper.user
EnterpriseSample.Tests
Data
DaoTestDoubles
Domain
Dto
Presenters
Properties
TestFactories
Web
EnterpriseSample.Web
App_Code
PageControllers
Bin
Castle.DynamicProxy.dll
Castle.MicroKernel.dll
Castle.Model.dll
Castle.Windsor.dll
Castle.Windsor.dll.refresh
EnterpriseSample.Core.dll
EnterpriseSample.Core.pdb
EnterpriseSample.Data.dll
EnterpriseSample.Data.pdb
EnterpriseSample.Presenters.dll
EnterpriseSample.Presenters.pdb
Iesi.Collections.dll
log4net.dll
log4net.dll.refresh
NHibernate.dll
NHibernate.dll.refresh
ProjectBase.Data.dll
ProjectBase.Data.pdb
ProjectBase.Utils.dll
ProjectBase.Utils.pdb
Config
EnterpriseSample.log
Global.asax
Views
ProjectBase.Data
NHibernateSessionMgmt
Properties
ProjectBase.Utils
Properties
Web
Solution Items
Castle.Core.dll
Castle.DynamicProxy.dll
Castle.Facilities.AutomaticTransactionManagement.dll
Castle.MicroKernel.dll
Castle.Model.dll
Castle.Services.Transaction.dll
Castle.Windsor.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
nunit.framework.dll
NUnitAsp.dll
Rhino.Mocks.dll
nhibernatebestpractices_src.zip
NHibernateSample
NHibernateSample.Core
bin
DataInterfaces
Domain
Properties
NHibernateSample.Data
bin
Properties
NHibernateSample.Tests
bin
Data
Domain
MockDao
Properties
NHibernateSample.Web
App_Code
Bin
Castle.DynamicProxy.dll
Iesi.Collections.dll
Iesi.Collections.dll.refresh
log4net.dll
log4net.dll.refresh
NHibernate.dll
NHibernate.Generics.dll
NHibernateSample.Core.dll
NHibernateSample.Data.dll
Solution Items
Iesi.Collections.dll
log4net.dll
NHibernate.dll
NHibernate.Generics.dll
nunit.framework.dll
using System;
using System.Collections.Generic;
using EnterpriseSample.Core.DataInterfaces;
using ProjectBase.Utils;

namespace EnterpriseSample.Core.Domain
{
    public class Customer : DomainObject<string>, IHasAssignedId<string>
    {
        #region Constructors

        /// <summary>
        /// Needed by ORM for reflective creation.
        /// </summary>
        private Customer() {}

        public Customer(string companyName) {
            // Set the public setter instead of the private member directly so that any business 
            // rules will be carried out
            CompanyName = companyName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Provides an accessor for injecting an IOrderDao so that this class does 
        /// not have to create one itself.  Can be set from a controller, using 
        /// IoC, or from another business object.  As a rule-of-thumb, I do not like
        /// domain objects to use DAOs directly; but there are exceptional cases; 
        /// therefore, this shows a way to do it without having a concrete dependency on the DAO.
        /// </summary>
        public IOrderDao OrderDao {
            get {
                if (orderDao == null) {
                    throw new MemberAccessException("OrderDao has not yet been initialized");
                }

                return orderDao;
            }
            set {
                orderDao = value;
            }
        }

        public string CompanyName {
            get { return companyName; }
            set {
                Check.Require(!string.IsNullOrEmpty(value), "A valid company name must be provided");
                companyName = value;
            }
        }

        public string ContactName {
            get { return contactName; }
            set { contactName = value; }
        }

        public IList<Order> Orders {
            get { return new List<Order>(orders).AsReadOnly(); }
            protected set { orders = value; }
        }

        public void AddOrder(Order order) {
            if (order != null && !orders.Contains(order)) {
                orders.Add(order);
            }
        }

        public void RemoveOrder(Order order) {
            if (order != null && orders.Contains(order)) {
                orders.Remove(order);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// To get all the orders ordered on a particular date, we could loop through 
        /// each item in the Orders collection.  But if a customer has thousands of 
        /// orders, we don't want all the orders to have to be loaded from the database.  
        /// Instead, we can let the data layer do the filtering for us.
        /// </summary>
        public List<Order> GetOrdersOrderedOn(DateTime orderedDate) {
            Order exampleOrder = new Order(this);
            exampleOrder.OrderDate = orderedDate;

            // Make sure you use "OrderDao" and not "orderDao" so it'll be checked for proper initialization;
            // otherwise, you may get the oh-so-fun-to-track-down "object reference" exception.
            List<Order> allMatchingOrders = OrderDao.GetByExample(exampleOrder);

            // One downside to "GetByExample" is that the NHibernate "example fetcher" is rather shallow;
            // it'll only match on primitive properties - so even though Order.OrderedBy is set, it won't
            // match on it.  So we have to go through each of the returned results looking for any that
            // were orderd by this customer.  For situations like this, it would be better to expose a 
            // more specialized IOrderDao method, but this'll work for the demonstration at hand.
            List<Order> matchingOrdersForThisCustomer = new List<Order>();

            foreach (Order matchingOrder in allMatchingOrders) {
                if (matchingOrder.OrderedBy.ID == ID) {
                    matchingOrdersForThisCustomer.Add(matchingOrder);
                }
            }

            return matchingOrdersForThisCustomer;
        }

        public void SetAssignedIdTo(string assignedId) {
            Check.Require(! string.IsNullOrEmpty(assignedId), "assignedId may not be null or empty");

            // As an alternative to Check.Require, which throws an exception, the 
            // Validation Application Block could be used to validate the following
            Check.Require(assignedId.Trim().Length == 5, "assignedId must be exactly 5 characters");

            ID = assignedId.Trim().ToUpper();
        }

        /// <summary>
        /// Hash code should ONLY contain the "business value signature" of the object and not the ID
        /// </summary>
        public override int GetHashCode() {
            return (GetType().FullName + "|" +
                    CompanyName + "|" +
                    ContactName).GetHashCode();
        }

        #endregion

        #region Members

        private IOrderDao orderDao;
        private string companyName = "";
        private string contactName = "";
        private IList<Order> orders = new List<Order>();

        #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

Billy McCafferty
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 11 Jun 2008
Article Copyright 2006 by Billy McCafferty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid