0

0

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

DDD

DDD

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

|

743人浏览过

|

来源于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 getquery(iqueryable query, ispecification specification) where t : class
    {
        foreach (var includeinfo in specification.includeexpressions)
        {
            query = includeinfo.type switch
            {
                includetypeenum.include => buildinclude(query, includeinfo),
                includetypeenum.theninclude => buildtheninclude(query, includeinfo),
                _ => query
            };
        }

        return query;
    }

    public bool iscriteriaevaluator { get; } = false;

    private iqueryable buildinclude(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)result;
    }

    private iqueryable buildtheninclude(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)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 中也能很好地工作。

超级简历WonderCV
超级简历WonderCV

免费求职简历模版下载制作,应届生职场人必备简历制作神器

下载
public class linqtoqueryspecificationevaluator : ispecificationevaluator
{
    private list evaluators { get; } = new list();

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


    public iqueryable getquery(iqueryable query, ispecification 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)specification);

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

    public iqueryable getquery(iqueryable query, ispecification 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 list(ispecification specification) where t : class
    {
        return _specificationevaluator.getquery(_session.query().asqueryable(), specification).tolist();
    }

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

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

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

public class trackbyname : specification
{
    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 : specification
{
    private action? _action;
    public action getcriteria() => _action ?? throw new notsupportedexception("the criteria has not been specified. please use usecriteria() to define the criteria.");
    protected void usecriteria(action action) => _action = action;
}

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

public class queryoverspecification : specification
{
    private action>? _action;
    public action> 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> action) => _action = action;
}

public class queryoverspecification : specification
{
    private func, iqueryover>? _action;
    public func, iqueryover> 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> action) => _action = action;
}

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

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

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

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

public ienumerable list(ispecification specification) where t : class
{

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

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

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

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

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

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

标准规范示例

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

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

查询超规格示例

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

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

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

相关专题

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

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

675

2023.10.12

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

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

319

2023.10.27

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

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

346

2024.02.23

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

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

1084

2024.03.06

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

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

356

2024.03.06

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

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

674

2024.04.07

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

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

566

2024.04.29

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

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

409

2024.04.29

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

62

2025.12.31

热门下载

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

精品课程

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

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号