- Recent
- Popular
- Tags (0)
- Subscribers (1)
- Legacy DB and one-to-one relationsNovember 19
-
When dealing with a legacy database one often encounters the situation that the database schema defines one-to-one relations between two entities. A typical example might be the following schema fragment
with a one-to-one relation between person and address, that is: each person can have an address and an address can only belong to a single person. To guarantee that a single person can only have zero or one address the foreign key column PersonId in the Address table is set to be unique.
Such a construct is not straight forward to map in NHibernate! I want to show you one possible solution. The solution works but is not free from certain hick-ups! Let's first model the domain
The domain model
- Lazy loading BLOBS and the like in NHibernateNovember 17
-
[Updated, 2008-11-20 --> see end of post]
One of the questions that is asked again and again in the NHibernate user mailing list is the question about whether NHibernate supports lazy-loading of properties. The answer is NO - at least for the time being. Why is this question reasonable? Well, often we have entities in our domain that contain fields with large amount of data. Some samples are
- a large binary object (BLOB, e.g. an image, a Word document, a PDF, etc.),
- a large text object (CLOB, or nvarchar(max) )
- a cluster of rarely used extra fields
The problem is that we do not always need all this information when loading an entity. Thus we can massively improve the performance of our queries if those fields would only be loaded on demand.
The Model
Let's have a look at the following simplified domain model.
Here the person entity has an associated photo. The photo has been extracted fro
- First and Second Level caching in NHibernateNovember 9
-
I'll try to dive deep into the caching of NHibernate in this article. This post has been inspired by the talk given by Oren Eini (aka Ayende) at the Kaizen Conference in Austin TX.
Caching is a topic that is IMHO only superficially described so far especially regarding the second level cache. Most of the time one finds a lot of information about how to configure a specific cache provider for usage but the real usage (who and when) is not really described. I hope to be able to provide some of the missing pieces with this post.
The full source code used for this post can be found here.
First Level Cache
When using NHibernate the first level cache is automatically enabled as long as one uses the standard session object. We can avoid to use a cache at all when using the stateless session provided by NHibernate though. The stateless session is especially useful for reporting situations or for batch processing. When NHibernate is loading an entity by its unique id from the database then it is automatically put into the so called identity map. This identity map represents the first level cache.
The life-time of the first level cache is coupled to the current session. As soon as the current session is closed the content of the respective first level cache is cleared. Once an entity is in the - The Repository PatternOctober 8
-
Introduction
When accessing data from a data source we have several well documented possibilities. Martin Fowler e.g. describes several of them in his PoEAA book.
- Table data gateway
- Row data gateway
- Active record
- Data mapper
When applying DDD (domain driven design) we often use the so called Repository Pattern to access the data needed by the domain model.
What is a Repository
Martin Fowler writes:
"A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers."
Implementation
In DDD we have the notion
- Value objectsSeptember 17
-
Introduction
Eric Evans writes in his DDD book:
When you care only about the attributes of an element of the model, classify it as a value object. Make it express the meaning of the attributes it conveys and give it related functionality. Treat the value object as immutable. Don't give it any identity and avoid the design complexities necessary to maintain entities.
Many objects have no conceptual identity. These objects describe some characteristic of a thing.
A value object is an object that describes some characteristics or attribute but carries no concept of identity .
Samples
There are many samples where the introduction of a value object is useful. One of the most used value objects in DDD is certainly the Money value object. There is even a pattern called after this value object (the money pattern).
A large proportion of the computers in this wor
