2013-07-15 52 views
1

我想創建一個新的LINQ自定義方法與LinqToHqlGeneratorsRegistry,使用SQL Server 2008 STDistance地理功能,我該怎麼辦:FluentNHibernate配置LinqToHqlGeneratorsRegistry和自定義的LINQ STDistance空間SQL Server 2008的

1)地圖我的高級別方法(Coordinate.DistanceTo(other)實例方法)在SQL Server中由geoPoint.STDistance(otherGeoPoint)交換?我嘗試此:

public class DistanceMethodGenerator : BaseHqlGeneratorForMethod 
{ 
    public DistanceMethodGenerator() 
    { 
     base.SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<ICoordinate>(coordinate => coordinate.DistanceTo(null)) }; 
    } 
    public override HqlTreeNode BuildHql(MethodInfo method, System.Linq.Expressions.Expression targetObject, ReadOnlyCollection<System.Linq.Expressions.Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) 
    { 

     //what to do here to tranform: coord.DistanceTo(otherCoord) in sql  //coord.STDistance(otherCoord)? 
     return ???? 
    } 
} 
public sealed class LinqToSqlGeneratorRegistrypublic : DefaultLinqToHqlGeneratorsRegistry 
{ 
    public LinqToSqlGeneratorRegistrypublic() 
    { 
     RegisterGenerator(ReflectionHelper.GetMethodDefinition<ICoordinate>(coordinate => coordinate.DistanceTo(null)), 
          new DistanceMethodGenerator()); 
    } 
} 

2)如何註冊與fluentNHibernate,我無法找到像LinqToHqlGeneratorsRegistry<LinqToSqlGeneratorRegistrypublic >()的方法在FluentNHibernate

謝謝!

回答

1

試試這個:

.ExposeConfiguration(c => 
{ 
    c.Properties.Add(NHibernate.Cfg.Environment.LinqToHqlGeneratorsRegistry, typeof(LinqToSqlGeneratorRegistrypublic).AssemblyQualifiedName) 
} 
+0

這種配置已經存在於我的代碼,一點就是:如何實現BuildHql方法時,數據庫函數調用就像是「coord1.STDistance(coord2)」,而不是「dbo.distanceTo( coord1,coord2)「。 –