April 2008 Entries

Create and Update Database Schema

Introduction When developing an application using DDD one starts by trying to define a model of the domain for/in which the application should be used. At the same time you try to establish the so called ubiquitous language. At some point you might need to store and or retrieve data into or from a data source. Very often this data source is a relational database. But it's not necessarily always the case. It could as well be a web service or a XML document. That leads me to the notion that "the database is just an implementation detail of...

posted @ Monday, April 28, 2008 9:52 AM | Feedback (734)

NHibernate and Castle Active Record (Part 1)

Introduction The Castle ActiveRecord project is an implementation of the ActiveRecord pattern for .NET. The ActiveRecord pattern consists on instance properties representing a record in the database, instance methods acting on that specific record and static methods acting on all records. Castle ActiveRecord is built on top of NHibernate, but its attribute-based mapping free the developer of writing XML for database-to-object mapping, which is needed when using NHibernate directly. You can find the home page of the Castle project here. How will we use it? My intent is not to implement the ActiveRecord pattern...

posted @ Saturday, April 26, 2008 7:33 AM | Feedback (521)

NHibernate and the Unit of Work pattern (Part 3)

Introduction In the previous two posts which you can find here and here I introduced the Unit of Work pattern and developed a working version based on NHibernate. One weak point of the shown implementation is that it is strictly non thread safe. In NHibernate the session object takes the role of the Unit of Work container. The session object is not thread safe! That is a session instance may not be accessed from different threads. If your application is running on multiple threads (typically an application running on the server) then you have to open a new session...

posted @ Saturday, April 26, 2008 7:04 AM | Feedback (569)

NHibernate and the Unit of Work Pattern (Part 2)

In the first part of this series I have started to implement the Unit of Work pattern. In this post I'll finish this implementation (for non-thread safe version) and in a third post I'll discuss how to make this implementation thread safe. Design and Implementation The Unit Of Work Factory The creation of a unit of work instance is a complex process and as such is a good candidate for a factory. Since a UoW (Unit of Work) is basically a wrapper around a NHibernate session object I'll need to open such a session whenever...

posted @ Sunday, April 13, 2008 10:16 PM | Feedback (524)

NHibernate and the Unit of Work Pattern

Introduction Martin Fowler writes: "When you're pulling data in and out of a database, it's important to keep track of what you've changed; otherwise, that data won't be written back into the database. Similarly you have to insert new objects you create and remove any objects you delete." and "A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work." In NHibernate we have...

posted @ Thursday, April 10, 2008 9:51 PM | Feedback (950)

Soft Deletes

[Updated] What can I do if instead of physically delete a record in the database I just want to mark it as deleted? There are at least two possibilities to achieve the desired result put the necessary logic into the repository Write and register a DeleteEvent-Listener for NHibernate The Domain Model Let's assume a simple order entry system with just two entities Order and OrderLine Note: the implementation of the IdentityFieldProvider class I have discussed here. The business requirements are such...

posted @ Tuesday, April 08, 2008 9:57 PM | Feedback (560)

Eager loading aggregate with many child collections

In a comment to my post on lazy loading versus eager loading which you can find here I have been asked whether NHibernate 2.0 can now eagerly load an aggregate where the root has more than one child collection. The answer is yes but... Let's discuss it with a sample. Imagine having the following (academic) domain model. I have identified a Blog-aggregate. This aggregate has the Blog entity as root and has two child collections Posts and Readers. Additionally it has an Author child. If I want to eager load this aggregate with NHibernate I...

posted @ Sunday, April 06, 2008 10:11 AM | Feedback (588)

How to setup a new solution

Introduction In this post I'll present a possible setup for a .NET solution which respects (as much as possible) the following aspects, patterns and software engineering practices Continuous Integration (CI) Deploy often (--> see Agile development) Domain Driven Design (DDD) Test Driven Development (TDD) Separation of Concern (SoC) In most of the samples presented in this blog I'll adhere to this kind of solution setup (e.g. here and here)! The solution should have as few...

posted @ Sunday, April 06, 2008 8:18 AM | Feedback (411)

Identity Field, Equality and Hash Code

In this post I'll describe a possible base class for domain entities which implements a surrogate key as identity field and provides equality and hash code. Introduction Martin Fowler writes in his PoEAA book: "The identity field saves a database ID field in an object to maintain identity between an in-memory object and a database row." And further he states: "The first concern is whether to use meaningful or meaningless keys. A meaningful key is something like the U.S. Social Security Number... A meaningless key is essentially a random number the database dreams up that's never intended...

posted @ Friday, April 04, 2008 2:28 AM | Feedback (599)

Lazy Loading - Eager Loading

In this article I want to discuss the lazy loading mechanism provided by NHibernate. It is recommended for maximum flexibility to define all relations in your domain as lazy loadable. This is the default behavior of NHibernate since version 1.2. But this can lead to some undesired effects if querying your data. Let's discuss these effects and how to avoid them. In my previous posts I showed how to prepare your system for NHibernate and how to implement a first NHibernate base application. This post is based on those two articles. The Domain Let's first define a...

posted @ Thursday, April 03, 2008 11:22 PM | Feedback (440)

How to log only the queries that are executed

It is possible to have log4net log only the queries that are are executed by NHibernate.  In this sample, all queries generated by NHibernate will be stored in the file log.txt.  In your app.config, you will need to add the following sections to your app.config: <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="log.txt" /> <param name="AppendToFile" value="true" /> <param name="DatePattern" value="yyyy.MM.dd" /> <layout type="log4net.Layout.PatternLayout,log4net"> ...

posted @ Thursday, April 03, 2008 3:09 PM | Feedback (238)

Your first NHibernate based application

In a previous article I showed how to setup a developer machine to start using NHibernate as an ORM tool during the development of an application. I advocated a domain driven design (DDD) approach and a test driven development (TDD) style. This is the second article in a series of introductory chapters. Define the Domain Lets start by defining a very simple domain. For the moment it consists of one entity called Product. The product has 3 properties Name, Category and Discontinued. Add a folder Domain to the FirstSample project of your solution. Add a new class Product.cs to this folder. The...

posted @ Tuesday, April 01, 2008 12:29 AM | Feedback (1116)