Tigraine
Daniel Hoelbling talks about .NET

Storing binary data in NHibernate / ActiveRecord

August 19th, 2009 . by Daniel Hölbling

I believe the simplest way to store binary data is to just put in the database. Whenever I’ve agreed to throw data to a disk I’ve had issues with deployment, administration or disaster recovery.

Simply put: Once you have a dependency from your database to your file system, you no longer have the luxury of only thinking about recovering the database. You now need to keep two pieces of your system “safe”, both requiring a completely different toolset than the other.

Besides the obvious second point of headache for backup/recovery, you also bring yourself into a world of hurt for deployment / maintenance scenarios.
Filesystem access rights can be a huge pain in the ass, and having to set them right (and keep them that way) is usually a time-bomb waiting to go off.

So, storing your binary data in the db solves many problems, but some new ones arise. Mostly implementation details, but I’d like to show you some things to keep in mind when writing binary data to db.

NHibernate supports no lazy loading of instance fields

image While with conventional ADO.NET I’d just put the binary data as a column inside the table it belongs to, NHibernate requires you to do things different. If you map your data like that NHibernate will fetch it whenever you read objects from that table, meaning that you’ll be querying large binary data fields for no reason, causing you application performance to significantly degrade over time.

 

What you want is to have NHibernate fetch that field only if it is accessed (lazy load it), and that’s not possible for fields inside a class, but it is possible for references. So your database schema should look like this:

image

And your mapping will look similar to this (I’ll use ActiveRecord for easier understanding):

[ActiveRecord]
public class Invoice : ActiveRecordBase<Invoice>
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo(Lazy = FetchWhen.OnInvoke, Cascade = CascadeEnum.SaveUpdate)]
    public BinaryData ScannedInvoice { get; set; }
}

[ActiveRecord]
public class BinaryData : ActiveRecordBase<BinaryData>
{
    [PrimaryKey]
    public int Id { get; set; }

    [Property(ColumnType = "BinaryBlob", SqlType = "IMAGE", NotNull = true)]
    public byte[] Data { get; set; }
}

Now whenever your Invoice is saved/inserted NHibernate will also check if BinaryData has to be updated/inserted, while only loading the binary field if you actually access the Invoices.ScannedInvoice field.


View Comments to “Storing binary data in NHibernate / ActiveRecord”

  1. comment number 1 by: Craig Neuwirt

    Shouldn’t the BinaryData class be marked as lazy too via [ActiveRecord(Lazy = true)]?

  2. comment number 2 by: Daniel Hölbling

    Isn’t the (Lazy = FetchWhen.OnInvoke) on the property enough?

  3. comment number 3 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  4. comment number 4 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  5. comment number 5 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  6. comment number 6 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  7. comment number 7 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  8. comment number 8 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  9. comment number 9 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  10. comment number 10 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  11. comment number 11 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  12. comment number 12 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  13. comment number 13 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  14. comment number 14 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  15. comment number 15 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  16. comment number 16 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  17. comment number 17 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  18. comment number 18 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  19. comment number 19 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  20. comment number 20 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  21. comment number 21 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  22. comment number 22 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  23. comment number 23 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  24. comment number 24 by: Anonymous

    thanks … looking forward your another articles.rnweb tasaru0131m izmir

  25. comment number 25 by: microdatam

    thanks … looking forward your another articles.
    web tasarım izmir

Leave a Reply

Name

Mail (never published)

Website

blog comments powered by Disqus