Chapter 8. Using NHibernate in a Real-world Application

We have learned a lot of concepts and features of NHibernate. But we have learned all of them in isolation so far. While I enjoyed talking about them, it would be more interesting to see how to actually make use of NHibernate capabilities in a real-life application. And that is what I intend to do in this chapter.

There are two main objectives of this chapter. First one is to understand how NHibernate-based data access layer fits into the overall architecture of a web application and how it interacts with other components of an architecture. Second objective is to look closely at concepts such as repository pattern, unit of work, and session context. Repository pattern is a very commonly used pattern for abstracting away complex data access concepts and making the working with ORMs such as NHibernate easy. Unit of work is another time tested concept around executing related work items together as a unit. Session context is something specific to NHibernate and aids in management of session objects created by the application. On surface these concepts look simple and I want to show you that they are indeed simple.

This chapter uses an ASP.NET MVC web application as a primary example for walkthrough. I choose this as I believe that majority of software development happens in web space these days. Even desktop applications are becoming more of an UI wrapper powered by web service backends containing all the domain logic. In any of these scenarios, the data access layer operates under web context. So I believe it is important to understand how NHibernate fits into a web scenario. At the same time, also remember that most NHibernate features are generic and not specific for a particular type of application being built. Where things are different for a desktop application versus a web application, I will try to provide as much information as I can.

The approach we are going to take for this chapter is similar to a hands-on workshop. We will try to work through couple of functional requirements. While we progressively build these features, we will introduce new concepts such as unit of work, repository patterns, and session context at appropriate times. This approach, in my opinion, is more effective in understanding what problems are solved by these features. We will begin by spending some time talking about what kind of application we are going to build. We will then discuss some architectural principles that we will use while building these features. Once we have a common understanding of what approach we are going to take, we will jump into the actual coding of features.

What kind of application are we building?

As I just said, we will be building a web application. But we will try to lay out some details beyond that. So the web application that we are building will be based on ASP.NET MVC. We will have conceptual layers in this application such as business/domain layer, data access layer, presentation, and so on. But these layers will all be in the same process at runtime. We will not have each layer running in its own process like a standard 2-tier or 3-tier application. However, everything that we discuss here can be extended and applied without many changes to 2-tier or 3-tier applications separated by process boundaries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.64