Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
4.96/5 (322 votes)
11 Jun 2008CPOL60 min read 8.6M   35.9K   1.1K  
This article describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET, generics and unit testing together.
using System;
using System.Collections.Generic;
using EnterpriseSample.Core.DataInterfaces;
using EnterpriseSample.Core.Domain;
using EnterpriseSample.Tests.Data.DaoTestDoubles;
using NUnit.Framework;
using ProjectBase.Utils;

namespace EnterpriseSample.Tests.Domain
{
    [TestFixture]
    public class CustomerTests
    {
        [Test]
        public void CanCreateCustomer() {
            Customer customer = new Customer("Acme Anvil");
            Assert.AreEqual("Acme Anvil", customer.CompanyName);

            customer.CompanyName = "Acme 2";
            Assert.AreEqual("Acme 2", customer.CompanyName);

            Assert.AreEqual("", customer.ContactName);
            customer.ContactName = "Billy";
            Assert.AreEqual("Billy", customer.ContactName);
        }

        [Test]
        [ExpectedException(typeof(PreconditionException))]
        public void CannotCreateCustomerWithoutCompanyName() {
            new Customer("");
        }

        [Test]
        public void CanAddRemoveOrders() {
            Customer customer = new Customer("Acme Anvil");
            Assert.AreEqual(0, customer.Orders.Count);

            Order order = new Order(customer);
            Assert.AreEqual(1, customer.Orders.Count);

            // Was already added via the order's constructor
            customer.AddOrder(order);
            Assert.AreEqual(1, customer.Orders.Count);

            Assert.IsTrue(customer.Orders.Contains(order));
            customer.RemoveOrder(order);
            Assert.IsFalse(customer.Orders.Contains(order));
        }

        [Test]
        public void CanGetOrdersOrderedOnDateUsingStubbedDao() {
            Customer customer = new Customer("Acme Anvils");
            new DomainObjectIdSetter<string>().SetIdOf(customer, "ACME");
            customer.OrderDao = new OrderDaoStub();

            List<Order> matchingOrders = customer.GetOrdersOrderedOn(new DateTime(2005, 1, 11));
            Assert.AreEqual(2, matchingOrders.Count);
        }

        [Test]
        public void CanGetOrdersOrderedOnDateUsingMockedDao() {
            Customer customer = new Customer("Acme Anvils");
            new DomainObjectIdSetter<string>().SetIdOf(customer, "ACME");
            customer.OrderDao = new MockOrderDaoFactory().CreateMockOrderDao();

            IOrderDao orderDao = new MockOrderDaoFactory().CreateMockOrderDao();
            Assert.IsNotNull(orderDao);

            List<Order> matchingOrders = customer.GetOrdersOrderedOn(new DateTime(2005, 1, 11));
            Assert.AreEqual(2, matchingOrders.Count);
        }

        [Test]
        public void CanCompareCustomers() {
            Customer customerA = new Customer("Acme");
            Customer customerB = new Customer("Anvil");
            
            Assert.AreNotEqual(customerA, null);
            Assert.AreNotEqual(customerA, customerB);

            customerA.SetAssignedIdTo("AAAAA");
            customerB.SetAssignedIdTo("AAAAA");

            // Even though the "business value signatures" are different, the persistent IDs 
            // were the same.  Call me crazy, but I put that much trust into IDs.
            Assert.AreEqual(customerA, customerB);

            Customer customerC = new Customer("Acme");

            // Since customerA has an ID but customerC doesn't, their signatures will be compared
            Assert.AreEqual(customerA, customerC);

            customerC.ContactName = "Coyote";

            // Signatures are now different
            Assert.AreNotEqual(customerA, customerC);

            // customerA.Equals(customerB) because they have the same ID.
            // customerA.Equals(customerC) because they have the same signature.
            // ! customerB.Equals(customerC) because we can't compare their IDs, 
            // since customerC is transient, and their signatures are different.
            Assert.AreNotEqual(customerB, customerC);
        }
    }
}

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions