Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:58:36+00:00 2026-05-26T21:58:36+00:00

Preface : In my application, I store raw WAV data in the database as

  • 0

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 PcmAudioStreams 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>();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:58:37+00:00Added an answer on May 26, 2026 at 9:58 pm

    You could use a private static cache to accomplish this. It’s a little more work to set up but doesn’t require an additional class or public changes to your domain model. A big drawback is that entries are not removed from the cache, but you could use a custom collection or a “global” cache that limits the number of entries.

    public class Entity
    {
        public virtual int Id { get; protected set; }
    }
    
    public class PcmAudioStream
    {}
    
    public class User : Entity
    {
        private static readonly IDictionary<int, PcmAudioStream> _fullNameRecordingCache;
    
        private PcmAudioStream _fullNameRecording;
    
        static User()
        {
            _fullNameRecordingCache = new Dictionary<int, PcmAudioStream>();
        }
    
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual PcmAudioStream FullNameRecording
        {
            get
            {
                if (_fullNameRecordingCache.ContainsKey(Id))
                {
                    return _fullNameRecordingCache[Id];
                }
                // May need to watch for proxies here
                _fullNameRecordingCache.Add(Id, _fullNameRecording);
                return _fullNameRecording;
            }
            set
            {
                if (_fullNameRecordingCache.ContainsKey(Id))
                {
                    _fullNameRecordingCache[Id] = value;
                }
                _fullNameRecording = value;
            }
        }
        // ...
    }
    

    Mapping:

    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>()
                .Access.CamelCaseField(Prefix.Underscore);
        }
    }
    

    Edited in response to comments:

    I don’t see that it’s possible to achieve this in a user type because the IDataReader is already open in NullSafeGet. I think you could do it in a listener implementing IPreLoadEventListener but that doesn’t allow you to invalidate the cache. I don’t think either option is viable.

    After thinking about it some more I still think my original solution (or a variant) is the best option. I understand (and share) your desire for a clean domain model but sometimes compromises are necessary and my solution does not change the public members of the model or require any additional references. Another justification is that the object is the first to know that the recording has changed and needs to be replaced in or added to the cache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Preface . I wrote a small application in C that plays a video file
Preface: I'm developing an application that quizzes users on multiple topics. I wish to
Let me preface this by saying that I'm a very raw iPhone developer. I'm
Let me preface this by saying that I know the ContactCenter sample is just
I'll preface this by saying this is my first Mac application, though I've been
I'll preface this question with the note that I have looked at this similar
I'll preface this by saying that I usually work in C#/.Net. Normally, I use
I have to preface this with the fact that I love jQuery as a
Let me preface this question by stating that i don't usually condone the practice
preface note: I'm just starting to learn Grails, so I'm sure there are many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.