FluentNHibernate配置LinqToHqlGeneratorsRegistry和Custom Linq STDistance Spatial SQL Server 2008

时间:2013-07-15 17:23:03

标签: nhibernate fluent-nhibernate linq-to-nhibernate

我想使用LinqToHqlGeneratorsRegistry创建一个新的Linq自定义方法,以使用SQL Server 2008 STDistance地理位置功能,我该怎么做:

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注册,我在FluentNHibernate中找不到类似LinqToHqlGeneratorsRegistry<LinqToSqlGeneratorRegistrypublic >()的方法

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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