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

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.
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.Diagnostics;

namespace BasicSample.Core.Utils
{
    /// <summary>
    /// Design By Contract Checks.
    /// 
    /// Each method generates an exception or
    /// a trace assertion statement if the contract is broken.
    /// </summary>
    /// <remarks>
    /// This example shows how to call the Require method.
    /// <code>
    /// public void Test(int x)
    /// {
    /// 	try
    /// 	{
    ///			Check.Require(x > 1, "x must be > 1");
    ///		}
    ///		catch (System.Exception ex)
    ///		{
    ///			Console.WriteLine(ex.ToString());
    ///		}
    ///	}
    /// </code>
    ///
    /// You can direct output to a Trace listener. For example, you could insert
    /// <code>
    /// Trace.Listeners.Clear();
    /// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
    /// </code>
    /// 
    /// or direct output to a file or the Event Log.
    /// 
    /// (Note: For ASP.NET clients use the Listeners collection
    /// of the Debug, not the Trace, object and, for a Release build, only exception-handling
    /// is possible.)
    /// </remarks>
    /// 
    public sealed class Check
    {
        #region Interface

        /// <summary>
        /// Precondition check - should run regardless of preprocessor directives.
        /// </summary>
        public static void Require(bool assertion, string message) {
            if (UseExceptions) {
                if (!assertion) throw new PreconditionException(message);
            }
            else {
                Trace.Assert(assertion, "Precondition: " + message);
            }
        }

        /// <summary>
        /// Precondition check - should run regardless of preprocessor directives.
        /// </summary>
        public static void Require(bool assertion, string message, Exception inner) {
            if (UseExceptions) {
                if (!assertion) throw new PreconditionException(message, inner);
            }
            else {
                Trace.Assert(assertion, "Precondition: " + message);
            }
        }

        /// <summary>
        /// Precondition check - should run regardless of preprocessor directives.
        /// </summary>
        public static void Require(bool assertion) {
            if (UseExceptions) {
                if (!assertion) throw new PreconditionException("Precondition failed.");
            }
            else {
                Trace.Assert(assertion, "Precondition failed.");
            }
        }

        /// <summary>
        /// Postcondition check.
        /// </summary>
        public static void Ensure(bool assertion, string message) {
            if (UseExceptions) {
                if (!assertion) throw new PostconditionException(message);
            }
            else {
                Trace.Assert(assertion, "Postcondition: " + message);
            }
        }

        /// <summary>
        /// Postcondition check.
        /// </summary>
        public static void Ensure(bool assertion, string message, Exception inner) {
            if (UseExceptions) {
                if (!assertion) throw new PostconditionException(message, inner);
            }
            else {
                Trace.Assert(assertion, "Postcondition: " + message);
            }
        }

        /// <summary>
        /// Postcondition check.
        /// </summary>
        public static void Ensure(bool assertion) {
            if (UseExceptions) {
                if (!assertion) throw new PostconditionException("Postcondition failed.");
            }
            else {
                Trace.Assert(assertion, "Postcondition failed.");
            }
        }

        /// <summary>
        /// Invariant check.
        /// </summary>
        public static void Invariant(bool assertion, string message) {
            if (UseExceptions) {
                if (!assertion) throw new InvariantException(message);
            }
            else {
                Trace.Assert(assertion, "Invariant: " + message);
            }
        }

        /// <summary>
        /// Invariant check.
        /// </summary>
        public static void Invariant(bool assertion, string message, Exception inner) {
            if (UseExceptions) {
                if (!assertion) throw new InvariantException(message, inner);
            }
            else {
                Trace.Assert(assertion, "Invariant: " + message);
            }
        }

        /// <summary>
        /// Invariant check.
        /// </summary>
        public static void Invariant(bool assertion) {
            if (UseExceptions) {
                if (!assertion) throw new InvariantException("Invariant failed.");
            }
            else {
                Trace.Assert(assertion, "Invariant failed.");
            }
        }

        /// <summary>
        /// Assertion check.
        /// </summary>
        public static void Assert(bool assertion, string message) {
            if (UseExceptions) {
                if (!assertion) throw new AssertionException(message);
            }
            else {
                Trace.Assert(assertion, "Assertion: " + message);
            }
        }

        /// <summary>
        /// Assertion check.
        /// </summary>
        public static void Assert(bool assertion, string message, Exception inner) {
            if (UseExceptions) {
                if (!assertion) throw new AssertionException(message, inner);
            }
            else {
                Trace.Assert(assertion, "Assertion: " + message);
            }
        }

        /// <summary>
        /// Assertion check.
        /// </summary>
        public static void Assert(bool assertion) {
            if (UseExceptions) {
                if (!assertion) throw new AssertionException("Assertion failed.");
            }
            else {
                Trace.Assert(assertion, "Assertion failed.");
            }
        }

        /// <summary>
        /// Set this if you wish to use Trace Assert statements 
        /// instead of exception handling. 
        /// (The Check class uses exception handling by default.)
        /// </summary>
        public static bool UseAssertions {
            get {
                return useAssertions;
            }
            set {
                useAssertions = value;
            }
        }

        #endregion // Interface

        #region Implementation

        // No creation
        private Check() { }

        /// <summary>
        /// Is exception handling being used?
        /// </summary>
        private static bool UseExceptions {
            get {
                return !useAssertions;
            }
        }

        // Are trace assertion statements being used? 
        // Default is to use exception handling.
        private static bool useAssertions = false;

        #endregion // Implementation

    } // End Check

    #region Exceptions

    /// <summary>
    /// Exception raised when a contract is broken.
    /// Catch this exception type if you wish to differentiate between 
    /// any DesignByContract exception and other runtime exceptions.
    ///  
    /// </summary>
    public class DesignByContractException : ApplicationException
    {
        protected DesignByContractException() { }
        protected DesignByContractException(string message) : base(message) { }
        protected DesignByContractException(string message, Exception inner) : base(message, inner) { }
    }

    /// <summary>
    /// Exception raised when a precondition fails.
    /// </summary>
    public class PreconditionException : DesignByContractException
    {
        /// <summary>
        /// Precondition Exception.
        /// </summary>
        public PreconditionException() { }
        /// <summary>
        /// Precondition Exception.
        /// </summary>
        public PreconditionException(string message) : base(message) { }
        /// <summary>
        /// Precondition Exception.
        /// </summary>
        public PreconditionException(string message, Exception inner) : base(message, inner) { }
    }

    /// <summary>
    /// Exception raised when a postcondition fails.
    /// </summary>
    public class PostconditionException : DesignByContractException
    {
        /// <summary>
        /// Postcondition Exception.
        /// </summary>
        public PostconditionException() { }
        /// <summary>
        /// Postcondition Exception.
        /// </summary>
        public PostconditionException(string message) : base(message) { }
        /// <summary>
        /// Postcondition Exception.
        /// </summary>
        public PostconditionException(string message, Exception inner) : base(message, inner) { }
    }

    /// <summary>
    /// Exception raised when an invariant fails.
    /// </summary>
    public class InvariantException : DesignByContractException
    {
        /// <summary>
        /// Invariant Exception.
        /// </summary>
        public InvariantException() { }
        /// <summary>
        /// Invariant Exception.
        /// </summary>
        public InvariantException(string message) : base(message) { }
        /// <summary>
        /// Invariant Exception.
        /// </summary>
        public InvariantException(string message, Exception inner) : base(message, inner) { }
    }

    /// <summary>
    /// Exception raised when an assertion fails.
    /// </summary>
    public class AssertionException : DesignByContractException
    {
        /// <summary>
        /// Assertion Exception.
        /// </summary>
        public AssertionException() { }
        /// <summary>
        /// Assertion Exception.
        /// </summary>
        public AssertionException(string message) : base(message) { }
        /// <summary>
        /// Assertion Exception.
        /// </summary>
        public AssertionException(string message, Exception inner) : base(message, inner) { }
    }

    #endregion // Exception classes
}

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