Web Application Architectures

I continue to be interested in the architecture of the plain, standard, web application. Each of the web application frameworks defines an architecture for the plain web application. Think of Rails and Django (MVC + Active Record), or sharp architecture and Codebetter.Canvas (Asp.net MVC + NHibernate + DDD). This is a starting point from which more advanced and specialised web applications can grow.

Damian Maclennan, or as these guys might say (2:50) 'some guy',  presented a simple framework for web applications at Sydney Alt.Net. Damian's example mixes Asp.Net MVC, Fluent NHibernate and Structure Map. 

The Good Parts

My favourite part is the way that NHibernate is configured with the IoC container. The author inherits from the Structure Map Registry class and creates a registry called NHibernateRegistry

internal class NHibernateRegistry : Registry
    {
        public NHibernateRegistry()
        {
            ForRequestedType<ISessionFactory>()
                .CacheBy(InstanceScope.Singleton)
                .TheDefault.Is.ConstructedBy(() => new NHibernateSessionFactory().GetSessionFactory());

            ForRequestedType<ISession>()
                .CacheBy(InstanceScope.Hybrid)
                .TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());
        }
    }

I think this is a neat way to group IoC registrations. The NHibernateRegistry registers NHibernateSessionFactory as a singleton, which is important because NHibernate requires that the session factory is at application scope. The scope for ISession is InstanceScope.Hybrid which means that the scope of an ISession is one HTTP request, exactly what is required for normal NHibernate usage. 

The Missing Parts

It is usually desirable to begin a transaction for each ISession and close the transaction prior to the ISession being closed. If the example is extended to include database writes it will fail without transactions. NHProf recommends that transactions be used even for read-only sessions.

Security can be a challenge in Asp.Net MVC. I would like to see how Damian prefers to secure controllers. 

The Repository<T> class currently only supports querying by Id. If a more complicated query is required where should it go? The two popular options are: specialised repositories (PostRepository : IRepository<Post>) with query methods and query objects where each query is encapsulated in an object. 

Fluent NHibernate is nice but I wonder why auto mapping was not used. It would work nicely for the domain in the example. 

Jerry's Final Thoughts

It's good to see the production of this kind of useful guidance. Someone new to the technologies could easily use Damian's example as a starting point for a well designed Asp.Net MVC application. It is certainly a much more helpful starting point than the Visual Studio template. 

Until next time, take care of yourselves and each other.


Comments

Comments are closed