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?

plaureano
  • 3,139
  • 6
  • 30
  • 29

1 Answers1

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.

vgru
  • 49,838
  • 16
  • 120
  • 201
Sebastian Markbåge
  • 3,275
  • 1
  • 18
  • 9
  • 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: https://github.com/bittercoder/Lob – Bittercoder Mar 07 '11 at 04: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 '12 at 03: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) :) – quetzalcoatl May 18 '18 at 13:24