讲师中心 微信公众号
AI工具推荐 视频效率加速

使用 Linq、Criteria API 和 Query Over 扩展 NHibernate 的 ArdalisSpecification

落磊君_3747

落磊君_3747

发布时间:2024-09-16 18:21:08

|

783人浏览过

|

来源于dev.to

转载

使用 linq、criteria api 和 query over 扩展 nhibernate 的 ardalisspecification

ardalis.specification 是一个功能强大的库,支持查询数据库的规范模式,主要是为 entity framework core 设计的,但在这里我将演示如何扩展 ardalis.specification 以将 nhibernate 用作 orm。

这篇博文假设您对 ardalis.specification 有一定的经验,并且希望在使用 nhibernate 的项目中使用它。如果您还不熟悉 ardalis.specification,请参阅文档以了解更多信息。

首先,nhibernate 中有三种不同的内置方法来执行查询

  • linq 查询(使用 iqueryable)
  • 标准 api
  • 查询结束

我将介绍如何扩展 ardalis.specification 来处理所有 3 种方式,但由于 linq to query 也可以像 entity framework core 一样与 iqueryable 配合使用,因此我将首先介绍该选项。

linq 查询

在创建连接关系时,entity framework core 和 nhibernate 之间存在细微差别。在 entity framework core 中,我们在 iqueryable 上有扩展方法: include 和 theninclude (这些也是 ardalis.specification 中使用的方法名称)。

fetch、fetchmany、thenfetch 和 thenfetchmany 是 iqueryable 上进行连接的 nhibernate 特定方法。 ievaluator 为我们提供了使用 nhibernate 时调用正确扩展方法所需的可扩展性。

添加 ievaluator 的实现,如下所示:

public class fetchevaluator : ievaluator
{
   private static readonly methodinfo fetchmethodinfo = typeof(eagerfetchingextensionmethods)
        .gettypeinfo().getdeclaredmethods(nameof(eagerfetchingextensionmethods.fetch))
        .single();

   private static readonly methodinfo fetchmanymethodinfo = typeof(eagerfetchingextensionmethods)
       .gettypeinfo().getdeclaredmethods(nameof(eagerfetchingextensionmethods.fetchmany))
       .single();

   private static readonly methodinfo thenfetchmethodinfo
       = typeof(eagerfetchingextensionmethods)
           .gettypeinfo().getdeclaredmethods(nameof(eagerfetchingextensionmethods.thenfetch))
           .single();

   private static readonly methodinfo thenfetchmanymethodinfo
       = typeof(eagerfetchingextensionmethods)
           .gettypeinfo().getdeclaredmethods(nameof(eagerfetchingextensionmethods.thenfetchmany))
           .single();

    public static fetchevaluator instance { get; } = new fetchevaluator();

    public iqueryable<t> getquery<t>(iqueryable<t> query, ispecification<t> specification) where t : class
    {
        foreach (var includeinfo in specification.includeexpressions)
        {
            query = includeinfo.type switch
            {
                includetypeenum.include => buildinclude<t>(query, includeinfo),
                includetypeenum.theninclude => buildtheninclude<t>(query, includeinfo),
                _ => query
            };
        }

        return query;
    }

    public bool iscriteriaevaluator { get; } = false;

    private iqueryable<t> buildinclude<t>(iqueryable query, includeexpressioninfo includeinfo)
    {
        _ = includeinfo ?? throw new argumentnullexception(nameof(includeinfo));

        var methodinfo = (isgenericenumerable(includeinfo.propertytype, out var propertytype)
            ? fetchmanymethodinfo 
            : fetchmethodinfo);

       var method = methodinfo.makegenericmethod(includeinfo.entitytype, propertytype);

       var result = method.invoke(null, new object[] { query, includeinfo.lambdaexpression });
        _ = result ?? throw new targetexception();

        return (iqueryable<t>)result;
    }

    private iqueryable<t> buildtheninclude<t>(iqueryable query, includeexpressioninfo includeinfo)
    {
        _ = includeinfo ?? throw new argumentnullexception(nameof(includeinfo));
        _ = includeinfo.previouspropertytype ?? throw new argumentnullexception(nameof(includeinfo.previouspropertytype));

        var method = (isgenericenumerable(includeinfo.previouspropertytype, out var previouspropertytype)
            ? thenfetchmanymethodinfo
            : thenfetchmethodinfo);

        isgenericenumerable(includeinfo.propertytype, out var propertytype);

        var result = method.makegenericmethod(includeinfo.entitytype, previouspropertytype, propertytype)
            .invoke(null, new object[] { query, includeinfo.lambdaexpression });

        _ = result ?? throw new targetexception();

        return (iqueryable<t>)result;
    }

    private static bool isgenericenumerable(type type, out type propertytype)
    {
        if (type.isgenerictype && (type.getgenerictypedefinition() == typeof(ienumerable<>)))
        {
            propertytype = type.generictypearguments[0];

            return true;
        }

        propertytype = type;

        return false;
    }
}

接下来我们需要配置 ispecificationevaluator 以使用我们的 fetchevaluator(和其他评估器)。我们添加一个实现 ispecificationevaluator ,如下所示,并在构造函数中配置评估器。 whereevaluator、orderevaluator 和 paginationevaluator 都在 ardalis.specification 中,并且在 nhibernate 中也能很好地工作。

public class linqtoqueryspecificationevaluator : ispecificationevaluator
{
    private list<ievaluator> evaluators { get; } = new list<ievaluator>();

    public linqtoqueryspecificationevaluator()
    {
        evaluators.addrange(new ievaluator[]
        {
            whereevaluator.instance,
            orderevaluator.instance,
            paginationevaluator.instance,
            fetchevaluator.instance
        });
    }


    public iqueryable<tresult> getquery<t, tresult>(iqueryable<t> query, ispecification<t, tresult> specification) where t : class
    {
        if (specification is null) throw new argumentnullexception(nameof(specification));
        if (specification.selector is null && specification.selectormany is null) throw new selectornotfoundexception();
        if (specification.selector is not null && specification.selectormany is not null) throw new concurrentselectorsexception();

        query = getquery(query, (ispecification<t>)specification);

        return specification.selector is not null
            ? query.select(specification.selector)
            : query.selectmany(specification.selectormany!);
    }

    public iqueryable<t> getquery<t>(iqueryable<t> query, ispecification<t> specification, bool evaluatecriteriaonly = false) where t : class
    {
        if (specification is null) throw new argumentnullexception(nameof(specification));

        var evaluators = evaluatecriteriaonly ? evaluators.where(x => x.iscriteriaevaluator) : evaluators;

        foreach (var evaluator in evaluators)
           query = evaluator.getquery(query, specification);

        return query;
    }
}

现在我们可以在我们的存储库中创建对 linqtoqueryspecificationevaluator 的引用,可能如下所示:

public class repository : irepository
{
    private readonly isession _session;
    private readonly ispecificationevaluator _specificationevaluator;

    public repository(isession session)
    {
        _session = session;
        _specificationevaluator = new linqtoqueryspecificationevaluator();
    } 

    ... other repository methods

    public ienumerable<t> list<t>(ispecification<t> specification) where t : class
    {
        return _specificationevaluator.getquery(_session.query<t>().asqueryable(), specification).tolist();
    }

    public ienumerable<tresult> list<t, tresult>(ispecification<t, tresult> specification) where t : class
    {    
        return _specificationevaluator.getquery(_session.query<t>().asqueryable(), specification).tolist();
    }

    public void dispose()
    {
        _session.dispose();
    }
}

就是这样。现在,我们可以在规范中使用 linq to query,就像我们通常使用 ardalis 一样。规范:

public class trackbyname : specification<core.entitites.track>
{
    public trackbyname(string trackname)
    {
        query.where(x => x.name == trackname);
    }
}

现在我们已经介绍了基于 linq 的查询,让我们继续处理 criteria api 和 query over,这需要不同的方法。

在 nhibernate 中混合 linq、criteria 和 query over

由于 criteria api 和 query over 有自己的实现来生成 sql,并且不使用 iqueryable,因此它们与 ievaluator 接口不兼容。我的解决方案是在这种情况下避免对这些方法使用 ievaluator 接口,而是关注规范模式的好处。但我也希望能够混搭
我的解决方案中包含 linq to query、criteria 和 query over(如果您只需要其中一种实现,您可以根据您的最佳需求进行挑选)。

为了能够做到这一点,我添加了四个继承specification或specification的新类

注意: 定义这些类的程序集需要对 nhibernate 的引用,因为我们为 criteria 和 queryover 定义操作,这可以在 nhibernate
中找到

public class criteriaspecification<t> : specification<t>
{
    private action<icriteria>? _action;
    public action<icriteria> getcriteria() => _action ?? throw new notsupportedexception("the criteria has not been specified. please use usecriteria() to define the criteria.");
    protected void usecriteria(action<icriteria> action) => _action = action;
}

public class criteriaspecification<t, tresult> : specification<t, tresult>
{
    private action<icriteria>? _action;
    public action<icriteria> getcriteria() => _action ?? throw new notsupportedexception("the criteria has not been specified. please use usecriteria() to define the criteria.");
    protected void usecriteria(action<icriteria> action) => _action = action;
}

public class queryoverspecification<t> : specification<t>
{
    private action<iqueryover<t, t>>? _action;
    public action<iqueryover<t, t>> getqueryover() => _action ?? throw new notsupportedexception("the query over has not been specified. please use the usequeryover() to define the query over.");
    protected void usequeryover(action<iqueryover<t, t>> action) => _action = action;
}

public class queryoverspecification<t, tresult> : specification<t, tresult>
{
    private func<iqueryover<t, t>, iqueryover<t, t>>? _action;
    public func<iqueryover<t, t>, iqueryover<t, t>> getqueryover() => _action ??  throw new notsupportedexception("the query over has not been specified. please use the usequeryover() to define the query over.");
    protected void usequeryover(func<iqueryover<t, t>, iqueryover<t, t>> action) => _action = action;
}

然后我们可以在存储库中使用模式匹配来更改使用 nhibernate 进行查询的方式

public ienumerable<t> list<t>(ispecification<t> specification) where t : class
{
    return specification switch
    {
        criteriaspecification<t> criteriaspecification => 
            _session.createcriteria<t>()
                .apply(query => criteriaspecification.getcriteria().invoke(query))
                .list<t>(),

        queryoverspecification<t> queryoverspecification => 
            _session.queryover<t>()
                .apply(queryover => queryoverspecification.getqueryover().invoke(queryover))
                .list<t>(),

        _ => _specificationevaluator.getquery(_session.query<t>().asqueryable(), specification).tolist()
    };
}

public ienumerable<tresult> list<t, tresult>(ispecification<t, tresult> specification) where t : class
{

    return specification switch
    {
        criteriaspecification<t, tresult> criteriaspecification => 
            _session.createcriteria<t>()
                .apply(query => criteriaspecification.getcriteria().invoke(query))
                .list<tresult>(),

        queryoverspecification<t, tresult> queryoverspecification =>
            _session.queryover<t>()
                .apply(queryover => queryoverspecification.getqueryover().invoke(queryover))
                .list<tresult>(),

        _ => _specificationevaluator.getquery(_session.query<t>().asqueryable(), specification).tolist()
    };
}

上面的apply()方法是一个扩展方法,它将查询简化为一行:

public static class queryextensions
{
    public static t apply<t>(this t obj, action<t> action)
    {
        action(obj);
        return obj;
    }

    public static tresult apply<t, tresult>(this t obj, func<t, tresult> func)
    {
        return func(obj);
    }
}

标准规范示例

注意: 定义这些类的程序集需要对 nhibernate 的引用,因为我们定义 criteria 的操作,可以在 nhibernate
中找到

public class trackbynamecriteria : criteriaspecification<track>
{
    public trackbynamecriteria(string trackname)
    {
        this.usecriteria(criteria => criteria.add(restrictions.eq(nameof(track.name), trackname)));
    }
}

查询超规格示例

注意: 定义这些类的程序集需要对 nhibernate 的引用,因为我们定义 queryover 的操作,可以在 nhibernate
中找到

public class TrackByNameQueryOver : QueryOverSpecification<Track>
{
    public TrackByNameQueryOver(string trackName)
    {
        this.UseQueryOver(queryOver => queryOver.Where(x => x.Name == trackName));
    }
}

通过扩展 nhibernate 的 ardalis.specification,我们解锁了在单个存储库模式中使用 linq to query、criteria api 和 query over 的能力。这种方法为 nhibernate 用户提供了高度适应性和强大的解决方案

热门AI工具

更多
LibLibAI
LibLibAI Hot

一款AI视频创作工具,主要用于国内领先的AI创意平台,以海量模型、低门槛操作与“创作-分享-商业化”生态,让小白与专业创作者都能高效实现图文乃至视频创意表达,适合需要提升相关任务效率的用户。

UP简历
UP简历 Hot

一款AI办公效率工具,主要用于基于AI技术的免费在线简历制作工具,适合需要提升相关任务效率的用户。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

Laper
Laper Hot

Laper是专为编剧、导演和制片人推出的 AI 原生剧本创作工具。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

Seko
Seko Hot

一款AI视频创作工具,主要用于商汤科技推出的创编一体的AI短视频创作Agent,适合需要提升相关任务效率的用户。

SkildArt
SkildArt Hot

SkildArt是一款AI文本写作工具,一站式 AI 视觉创作平台。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

相关专题

更多
数据分析工具有哪些
数据分析工具有哪些

数据分析工具有Excel、SQL、Python、R、Tableau、Power BI、SAS、SPSS和MATLAB等。详细介绍:1、Excel,具有强大的计算和数据处理功能;2、SQL,可以进行数据查询、过滤、排序、聚合等操作;3、Python,拥有丰富的数据分析库;4、R,拥有丰富的统计分析库和图形库;5、Tableau,提供了直观易用的用户界面等等。

3803

2023.10.12

SQL中distinct的用法
SQL中distinct的用法

SQL中distinct的语法是“SELECT DISTINCT column1, column2,...,FROM table_name;”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

811

2023.10.27

SQL中months_between使用方法
SQL中months_between使用方法

在SQL中,MONTHS_BETWEEN 是一个常见的函数,用于计算两个日期之间的月份差。想了解更多SQL的相关内容,可以阅读本专题下面的文章。

989

2024.02.23

SQL出现5120错误解决方法
SQL出现5120错误解决方法

SQL Server错误5120是由于没有足够的权限来访问或操作指定的数据库或文件引起的。想了解更多sql错误的相关内容,可以阅读本专题下面的文章。

5601

2024.03.06

sql procedure语法错误解决方法
sql procedure语法错误解决方法

sql procedure语法错误解决办法:1、仔细检查错误消息;2、检查语法规则;3、检查括号和引号;4、检查变量和参数;5、检查关键字和函数;6、逐步调试;7、参考文档和示例。想了解更多语法错误的相关内容,可以阅读本专题下面的文章。

2583

2024.03.06

oracle数据库运行sql方法
oracle数据库运行sql方法

运行sql步骤包括:打开sql plus工具并连接到数据库。在提示符下输入sql语句。按enter键运行该语句。查看结果,错误消息或退出sql plus。想了解更多oracle数据库的相关内容,可以阅读本专题下面的文章。

5600

2024.04.07

sql中where的含义
sql中where的含义

sql中where子句用于从表中过滤数据,它基于指定条件选择特定的行。想了解更多where的相关内容,可以阅读本专题下面的文章。

7321

2024.04.29

sql中删除表的语句是什么
sql中删除表的语句是什么

sql中用于删除表的语句是drop table。语法为drop table table_name;该语句将永久删除指定表的表和数据。想了解更多sql的相关内容,可以阅读本专题下面的文章。

990

2024.04.29

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

80

2026.09.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn