Click here to Skip to main content
15,896,606 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.7M   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.Collections.Generic;
using System.Web.Services;
using EnterpriseSample.Core.Domain;
using EnterpriseSample.Core.Dto;
using EnterpriseSample.Web;

/// <summary>
/// Summary description for GetCustomer
/// </summary>
[WebService(Namespace = "http://localhost/EnterpriseNHibernateSample")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetCustomer : BaseWebService
{
    public GetCustomer() {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// A major problem with integrating NHibernate with web services is that lazily loaded collections
    /// aren't accessible after the result has been sent to the client, since the result is no longer 
    /// tied to the NHibernate session.  As an alternative to returning mapped domain objects, return 
    /// DTOs with all their available properties and collections already initialized.  This adds a 
    /// little work to forcefully load all the applicable collections, but also
    /// sends a "complete" result without having to worry about lazy loading issues.
    /// NOTE:  This should be seen more as an "idea" than a best practice.  I don't have a lot of experience
    /// with web services and so there may be a better approach to this.
    /// </summary>
    [WebMethod]
    public CustomerDto GetCustomerBy(string companyName) {
        if (!string.IsNullOrEmpty(companyName)) {
            Customer exampleCustomer = new Customer(companyName);

            // You could also use GetUniqueByExample, but that would thrown an exception if more than one item matched
            List<Customer> matchingCustomers = DaoFactory.GetCustomerDao().GetByExample(exampleCustomer);

            // Even if more than one customer matched, just return the first result.  This doesn't make much business sense, 
            // but since I'm my own client for developing this example application, I can make it do whatever I want it to do.  ;)
            if (matchingCustomers.Count >= 1) {
                return new CustomerDto(matchingCustomers[0]);
            }
        }

        return null;
    }

}

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