我使用nHibernates sql-interceptor解決了這個問題。不是最強大的解決方案,但這樣我可以爲PostGis和Oracle設置不同的攔截器。
我還研究了nHibernate.Spatial及其幾何類型。但我不想使用NetTopologySuite,因此我需要編寫更多代碼。
下面是代碼的一部分作爲一個例子:
public class PostGisGeometrySqlInterceptor : IInterceptor
{
...
/// <summary>
/// Modifies all the select statements with geometry so that
/// xxx.geometry -> ST_AsText(xxx.geometry)
/// </summary>
/// <param name="sql">The original sql string.</param>
/// <returns>The modified sql string.</returns>
/// <remarks>
/// All the geometry fields must be called 'geometry'.
/// Works only for one geometry in the sql string.
/// </remarks>
public virtual SqlString OnPrepareStatement(SqlString sql)
{
if (sql.StartsWithCaseInsensitive("SELECT") && sql.ToString().Contains("geometry"))
{
var indexOfGeometry = sql.IndexOfCaseInsensitive("geometry");
var indexOfSpace = sql.Substring(0, indexOfGeometry).LastIndexOfCaseInsensitive(" ") + 1;
var oldValue = sql.ToString(indexOfSpace, indexOfGeometry - indexOfSpace + "geometry".Length);
var newValue = string.Format("ST_AsText({0})", oldValue);
sql = sql.Replace(oldValue, newValue);
}
else if(...)
...
return sql;
}