我正在实施对Linq和Nhibrenate的“ iSlike”扩展,如 法比奥的这篇文章.

我有这样的代码:

public class MyLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
    public MyLinqToHqlGeneratorsRegistry()
        : base()
    {
        RegisterGenerator(ReflectionHelper.GetMethodDefinition(() =>
            MyLinqExtensions.IsLike(null, null)),
                          new IsLikeGenerator());
    }
}


public class IsLikeGenerator : BaseHqlGeneratorForMethod
{
    public IsLikeGenerator()
    {
        SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition(() =>
          MyLinqExtensions.IsLike(null, null)) };
    }

    public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
        ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
    {
        return treeBuilder.Like(visitor.Visit(arguments[0]).AsExpression(),
                                visitor.Visit(arguments[1]).AsExpression());
    }
}

public static class MyLinqExtensions
{
    public static bool IsLike(this string source, string pattern)
    {
        pattern = Regex.Escape(pattern);
        pattern = pattern.Replace("%", ".*?").Replace("_", ".");
        pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");

        return Regex.IsMatch(source, pattern);
    }
}

扩展名在配置(第三行)中注册:

    protected void InitializeNHibernateSession()
    {
        NHibernateConfiguration = NHibernateSession.Init(
                               new SimpleSessionStorage(),
                               GetMappingAssemblies(),
                               GetNHibernateConfig());

        NHibernateConfiguration.Properties.Add(
                   Environment.LinqToHqlGeneratorsRegistry, 
                   typeof(MyLinqToHqlGeneratorsRegistry).AssemblyQualifiedName);
        NHibernateSession.RegisterInterceptor(new AuditInterceptor());
    }

但是当我尝试运行查询时,我会得到异常

System.NotSupportedException was unhandled by user code
  Message=Boolean IsLike(System.String, System.String)
  Source=NHibernate
  StackTrace:
       at     NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMethodCallExpression    (MethodCallExpression expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression    (Expression     expression)
       at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(Expression expression, VisitorParameters parameters)
       at NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
       at Remotion.Data.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
       at Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
       at Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel     queryModel)
       at NHibernate.Linq.Visitors.QueryModelVisitor.Visit()
       at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
       at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
       at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
       at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
       at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
       at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
       at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
       at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
       at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
       at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
       at MyProject.Data.Specification.LinqSpecRepository`1.FindAllPaged(Specification`1 specification, Int32 currentPage, Int32 noOfItemsPerPage) in c:\source\MyProject\Specification\LinqSpecRepository.cs:line 47
       at MyProject.Tests.PersonRepositoryTests_UserSearch.FilteredQuery_CanPerformWildCardAtTheEndSearch() in c:\source\MyProject.Tests\PersonRepositoryTests_UserSearch.cs:line 51

就像扩展名未注册或未触发。由于我尝试将其添加到测试本身中的配置中,因此设置了该属性,并获得了键已经存在的例外。

NHIBERNATE组装版本为3.0.0.4000

我可能做错了什么建议?

有帮助吗?

解决方案

在弄清楚敏锐的体系结构的作用并了解了更多有关会话幕后的信息(即无法更改),解决方案是将属性添加到NhibernateSession中。请致电

var configProperties = new Dictionary<string, string> {{
   Environment.LinqToHqlGeneratorsRegistry,
   typeof (MyLinqToHqlGeneratorsRegistry).AssemblyQualifiedName
}};

NHibernateConfiguration = NHibernateSession.Init(
                            new SimpleSessionStorage(),
                            GetMappingAssemblies(), null,
                            GetNHibernateConfig(), configProperties, null);

我尝试将属性添加到配置文件中,但在文件上有验证错误。将其添加到.init()呼叫中,但是可以很好地工作。

其他提示

既然我知道法比奥的榜样 作品, ,它必须是您的包装类中的某些内容,我们没有来源。

nhibernatesession.init做什么?

您什么时候构建SessionFactory?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top