Preface:
In my application, I store raw WAV data in the database as byte[]
. In my domain model there is a class PcmAudioStream
that represents that raw WAV data. I created an implementation of NHibernate's IUserType
to convert between my class and byte[]
.
There are several classes that use the PcmAudioStream
class, all of which are mapped to database tables. To avoid always loading all WAV data when retrieving a row from such a table, I created an implementation of Fluent NHibernate's IUserTypeConvention
that specifies that those properties should always be lazy loaded.
All of this works like a charm.
Question:
Because the content of these PcmAudioStream
s rarely ever changes, I want to put retrieved instances in the second level cache. Now, I know how to activate the second level cache for a complete class, but how do I achieve this only for a lazy loaded property?
The relevant part of my domain model looks like this:
public class User : Entity
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual PcmAudioStream FullNameRecording { get; set; }
// ...
}
The mapping is simple (note: that is not my mapping, I am using a convention, but it is equivalent):
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.FullNameRecording).CustomType<PcmAudioStreamAsByteArray>();
}
}