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

Model View Presenter with ASP.NET

, 16 Oct 2007
This article describes using the Model-View-Presenter pattern within ASP.NET 2.0 to encourage proper separation of concerns between presentation and business logic
amosttrivialexample.zip
AMostTrivialExample
App_Code
Solution Items
nunit.framework.dll
enterpriseapplicationmvp.zip
EnterpriseApplicationMvp
MvpSample.Core
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
MvpSample.Core.dll
MvpSample.Core.pdb
NHibernate.dll
NHibernate.Generics.dll
DataInterfaces
Domain
Properties
MvpSample.Data
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
MvpSample.Core.dll
MvpSample.Core.pdb
MvpSample.Data.dll
MvpSample.Data.pdb
NHibernate.dll
NHibernate.Generics.dll
Properties
MvpSample.Presenters
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
MvpSample.Core.dll
MvpSample.Core.pdb
MvpSample.Presenters.dll
MvpSample.Presenters.pdb
NHibernate.dll
NHibernate.Generics.dll
Properties
ViewInterfaces
MvpSample.Tests
bin
Debug
Castle.DynamicProxy.dll
Iesi.Collections.dll
log4net.dll
MvpSample.Core.dll
MvpSample.Core.pdb
MvpSample.Data.dll
MvpSample.Data.pdb
MvpSample.Presenters.dll
MvpSample.Presenters.pdb
MvpSample.Tests.dll
MvpSample.Tests.pdb
NHibernate.dll
NHibernate.Generics.dll
Data
Domain
MockDao
Presenters
Properties
MvpSample.Web
App_Code
Bin
Castle.DynamicProxy.dll
Castle.MicroKernel.dll
Castle.MicroKernel.dll.refresh
Castle.Model.dll
Castle.Model.dll.refresh
Castle.Windsor.dll
Castle.Windsor.dll.refresh
Iesi.Collections.dll
Iesi.Collections.dll.refresh
log4net.dll
log4net.dll.refresh
MvpSample.Core.dll
MvpSample.Core.pdb
MvpSample.Data.dll
MvpSample.Data.pdb
MvpSample.Presenters.dll
MvpSample.Presenters.pdb
NHibernate.dll
NHibernate.Generics.dll
Config
Global.asax
Views
Solution Items
Castle.MicroKernel.dll
Castle.Model.dll
Castle.Windsor.dll
Iesi.Collections.dll
log4net.dll
NHibernate.dll
NHibernate.Generics.dll
nunit.framework.dll
eventhandlingmvp.zip
EventHandlingMvp
App_Code
Solution Items
nunit.framework.dll
Views
using System;
using System.Web;
using MvpSample.Data;

namespace MvpSample.Web
{
    /// <summary>
    /// Implements the Open-Session-In-View pattern using <see cref="NHibernateSessionManager" />.
    /// Assumes that each HTTP request is given a single transaction for the entire page-lifecycle.
    /// Inspiration for this class came from Ed Courtenay at 
    /// http://sourceforge.net/forum/message.php?msg_id=2847509.
    /// </summary>
    public class NHibernateSessionModule : IHttpModule
    {
        public void Init(HttpApplication context) {
            context.BeginRequest += new EventHandler(BeginTransaction);
            context.EndRequest += new EventHandler(CommitAndCloseSession);
        }

        /// <summary>
        /// Opens a session within a transaction at the beginning of the HTTP request.
        /// This doesn't actually open a connection to the database until needed.
        /// </summary>
        private void BeginTransaction(object sender, EventArgs e) {
            NHibernateSessionManager.Instance.BeginTransaction();
        }

        /// <summary>
        /// Commits and closes the NHibernate session provided by the supplied <see cref="NHibernateSessionManager"/>.
        /// Assumes a transaction was begun at the beginning of the request; but a transaction or session does
        /// not *have* to be opened for this to operate successfully.
        /// </summary>
        private void CommitAndCloseSession(object sender, EventArgs e) {
            try {
                NHibernateSessionManager.Instance.CommitTransaction();
            }
            finally {
                NHibernateSessionManager.Instance.CloseSession();
            }
        }

        public void Dispose() { }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Billy McCafferty
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 16 Oct 2007
Article Copyright 2006 by Billy McCafferty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid