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
This article describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET, generics and unit testing together.
Prize winner in Competition "ASP.NET Feb 2006"
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 BasicSample.Core.DataInterfaces;
using BasicSample.Data;
using BasicSample.Core.Domain;

public partial class AddCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            lblMessage.Text = "Use this form to add a new customer.";
        }
    }

    protected void btnAdd_OnClick(object sender, EventArgs e) {
        if (txtCustomerID.Text.Trim().Length == 5) {
            Customer newCustomer = new Customer(txtCompanyName.Text);
            newCustomer.SetAssignedIdTo(txtCustomerID.Text);
            newCustomer.ContactName = txtContactName.Text;

            IDaoFactory daoFactory = new NHibernateDaoFactory();
            ICustomerDao customerDao = daoFactory.GetCustomerDao();

            if (!IsDuplicateOfExisting(newCustomer, customerDao)) {
                customerDao.Save(newCustomer);
                Response.Redirect("ListCustomers.aspx?action=added");
            }
            else {
                lblMessage.Text =
                    "<span style=\"color:red\">The ID you provided is already in use.</span><br />Please change the ID and try again.";
            }
        }
        else {
            lblMessage.Text =
                "<span style=\"color:red\">The ID you provide must be exactly 5 characters long.</span><br />Please change the ID and try again.";
        }
    }

    /// <summary>
    /// Checks if a customer already exists with the same customer ID.
    /// </summary>
    private bool IsDuplicateOfExisting(Customer newCustomer, ICustomerDao customerDao) {
        // Whenever possible, I *really* don't like using assigned IDs.  I think they 
        // should only be used when working with a legacy database.  Among other ugliness, 
        // assigned IDs force us to try/catch when checking for duplicates because NHibernate 
        // will throw an ObjectNotFoundException if no entity with the provided ID is found.
        // Consequently, we also have to have a reference to the NHibernate assembly from within
        // our business object.
        // To overcome these drawbacks, I'd recommend adding a DoesEntityExist(string assignedId) 
        // method to the DAO to check for the existence of entities by its assigned ID.  This would remove the
        // ugly try/catch from this method and it would also remove the local dependency on the NHibernate
        // assembly.  I've chosen not to go ahead and do this because my assumption is that
        // the use of assigned IDs will be the exception rather than the norm...so I want to keep
        // the generic DAO as clean as possible for the example demo.
        try {
            Customer duplicateCustomer = customerDao.GetById(newCustomer.ID, false);
            return duplicateCustomer != null;
        }
        // Only catch ObjectNotFoundException, throw everything else.
        catch (NHibernate.ObjectNotFoundException) {
            // Since the duplicate we were looking for wasn't found, then, through difficult 
            // logical deduction, this object isn't a duplicat
            return false;
        }
    }

    protected void btnCancel_OnClick(object sender, EventArgs e) {
        Response.Redirect("ListCustomers.aspx");
    }
}

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
Web03 | 2.8.140814.1 | Last Updated 11 Jun 2008
Article Copyright 2006 by Billy McCafferty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid