Click here to Skip to main content
15,868,016 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 EnterpriseSample.Core.Domain;
using EnterpriseSample.Presenters;
using EnterpriseSample.Presenters.ViewInterfaces;
using EnterpriseSample.Web;
using ProjectBase.Utils;

public partial class Views_EditCustomerView : BaseUserControl, IEditCustomerView
{
    public EventHandler UpdateCompleted;
    public EventHandler UpdateCancelled;
    
    public void AttachPresenter(EditCustomerPresenter presenter) {
        this.presenter = presenter;
    }

    public Customer Customer {
        set {
            Check.Require(value != null, "Customer may not be null");
            ShowCustomerDetails(value);
        }
    }

    private void ShowCustomerDetails(Customer customer) {
        // Instead of using a hidden input, you could also use ViewState...but it's often best
        // to disable ViewState for your entire website
        hidCustomerID.Value = customer.ID;

        lblCustomerID.Text = customer.ID;
        txtCompanyName.Text = customer.CompanyName;
        txtContactName.Text = customer.ContactName;
    }

    /// <summary>
    /// Required by <see cref="IEditCustomerView" /> so that the presenter
    /// can have the view set the values of the customer.
    /// </summary>
    public void UpdateValuesOn(Customer customer) {
        // Changes to the customer object will be automatically committed at the end of the HTTP request
        customer.CompanyName = txtCompanyName.Text;
        customer.ContactName = txtContactName.Text;
    }

    protected void btnUpdate_OnClick(object sender, EventArgs e) {
        presenter.Update(hidCustomerID.Value);

        // The view itself shouldn't be handling any redirects, so simply raise an event letting 
        // whomever know that the action has completed
        if (UpdateCompleted != null) {
            UpdateCompleted(this, EventArgs.Empty);
        }
    }

    protected void btnCancel_OnClick(object sender, EventArgs e) {
        if (UpdateCancelled != null) {
            UpdateCancelled(this, EventArgs.Empty);
        }
    }

    private EditCustomerPresenter presenter;
}

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