NHibernate: UserType to save files on disk instead of database

93 Views Asked by At

At the moment we are saving files (or better: their content as stream) in the database.

<class name="File"
     table="tblFile"
     lazy="false">
 <id name="_id"
    column="Id"
    access="field" >
  <generator class="native"/>
 </id>
 <property name ="_businessId"
          column="BusinessId"
          not-null="true"
          access="field" />
 <property name="_name"
          column="Filename"
          not-null="true"
          access="field" />
 <property name="_mimeType"
          column="Mime"
          not-null="true"
          access="field" />
 <property name="_content"
          column="Content"
          length="2147483647"
          not-null="true"
          access="field" />
 <property name="_fileType"
      column="FileType"
      not-null="true"
      access="field" />
</class>

All in all this worked well so far, unless there are some issues: for example that you cannot save files >2gb.

So we are now searching for a better solution. If you dont save the file in the database, you have to save them on disk. To not reimplement the total persistence-process, we thought of implementing a custom UserType (IUserType) that saves the file on disk and stores the filename in the database.

The NullSafeSet would save the file on disc and store the location in database, whilest the NullSafeGet would (lazily) return a stream created from the filename in the database.

We found some pros but also some cons in that solution:

pros: - no changes to code except implementing the UserType and minimaly changing the mapping file - saving files on disc - ...

cons: - where to dispose the stream created by the UserType - fixed folder where files are saved (no a problem for us so far) - maybe slow down nhibernate - ...

So now we wonder if the UserType to persist files on disc is a mad idea at all or if it is worth researching to fix the cons?

0

There are 0 best solutions below