C# NHibernate:将文件保存在磁盘而不是数据库上的用户类型
目前,我们正在数据库中保存文件(或者更好:将其内容保存为流)C# NHibernate:将文件保存在磁盘而不是数据库上的用户类型,c#,file,nhibernate,iusertype,C#,File,Nhibernate,Iusertype,目前,我们正在数据库中保存文件(或者更好:将其内容保存为流) <class name="File" table="tblFile" lazy="false"> <id name="_id" column="Id" access="field" > <generator class="native"/> </id> <property name ="_businessId" co
<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>
到目前为止,所有这些都很有效,除非存在一些问题:例如,无法保存大于2gb的文件
因此,我们现在正在寻找更好的解决方案。
如果不将文件保存在数据库中,则必须将其保存在磁盘上。
为了不重新实现整个持久化过程,我们考虑实现一个自定义用户类型(IUserType),将文件保存在磁盘上,并将文件名存储在数据库中
<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>
NullSafeSet会将文件保存在光盘上并将位置存储在数据库中,同时NullSafeGet会(延迟地)返回从数据库中的文件名创建的流
<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>
我们在该解决方案中发现了一些优点,但也发现了一些缺点:
优点:
-除了实现UserType和最少更改映射文件外,不对代码进行任何更改
-在光盘上保存文件
-
缺点:
-在何处处置由UserType创建的流
-修复了保存文件的文件夹(到目前为止对我们来说没有问题)
-也许慢一点
-
所以现在我们想知道,将文件持久保存在光盘上的用户类型到底是一个疯狂的想法,还是值得研究以修复缺点