8

If I have a class declared as:

public class MyPersistentClass
{
     public int ID  { get; set; } 
     public Stream MyData  {get;set; }
}

How can I use NHibernate's mappings to persist the MyData property to and from the database?

1 Answer 1

13

You could use a Stream using a custom type and map it according to your storage needs. But there are some issues with using the Stream object as I mention in my blog series about lazy streaming of BLOBs and CLOBs with NHibernate.

What you really need is a Blob object that in turn can create a Stream to read data from. Since Stream contains information about the position you're reading from and expects to be closed and disposed of it can create some issues when used directly in a domain model.

I would suggest that you take a look at the blog series as well as the source code of the NHibernate.Lob project. It includes various mapping options for just such a problem. A little scarcely documented so far but more is coming.

3
  • 5
    FYI the NHibernate.Lob project is a little incomplete and hasn't recieved an update in a long time. Having attempted to use this on a recent project I found a number of issues (i.e. garbage collection not implemented, doesn't work with second level cache etc.) - so I decided to fork it and fix them up - you can find my fork here: github.com/bittercoder/Lob Mar 7, 2011 at 4:11
  • @Bittercoder : Thanks for your effort into fixing it up. I am trying to get it to work NH 3.2 .
    – dan_l
    Jan 10, 2012 at 3:42
  • 2
    Sidenote: project seems abandoned for 7yrs now, and the "blog series" actually reached stage part-2-of-5, where not really much is presented except for some more-or-less general information. GitHub projects seems just abandoned with somewhat chaotic project structure many and empty zero-size files.. it's makes it hard to even estimate if anything there is usable or not (not that I update my blog more often, but hey) :) May 18, 2018 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.