限制收集检索只读实体只是最近条目 [英] Limit collection to retrieve only recent entries for readonly entity

查看:178
本文介绍了限制收集检索只读实体只是最近条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户实体可以有成千上万的USEROPERATIONS。有时候我不想检索​​(唯读实体)所有的人,但只有近10或未完成的。

The User entity can have thousands of UserOperations. Sometimes I don't want to retrieve (for readonly entity) all of them but only "the recent 10 OR not completed".

public class SimpleForm
{
    public class User : EntityBase
    {
        //  ...

        private ISet<UserOperation> _recentOperations = new HashedSet<UserOperation>();
        public virtual ISet<UserOperation> RecentOperations { get { return _recentOperations; } set { _recentOperations = value; } }
    }
}



所以,我怎么能指定吗?我想我可以使用映射覆盖?

So how can I specify it? I think I could use mapping overrides?

我明白我可以用一个单独的查询,让这一点,但可以将它通过实体映射办?

I understand I could make this with a seperate query but can it be done by entity mapping?

此外,我不知道是否有可能做一些非只读实体在哪里可以修改操作?

Also I wonder if there is a possibility to do the some for non-readonly entity where I can modify the collection of operations?

更新

我试图用

DateTime dateTime = (DateTime.UtcNow - TimeSpan.FromDays(15));
mapping.HasMany(x => x.RecentOperations)
       .Where(x => x.EndedAt == null || x.EndedAt < dateTime);



但它说无法表达式转换为SQL。

but it says "Unable to convert expression to SQL".

mapping.HasMany(x => x.RecentOperations)
                .Where(x => x.EndedAt == null);

和现在它抛出里面

вFluentNHibernate.Utils.ExpressionToSql.Convert(对象的值)
вFluentNHibernate.Utils.ExpressionToSql.Convert(常量表达式表达式)
вFluentNHibernate.Utils.ExpressionToSql.Convert [T](Expression`1表达,UnaryExpression体)

в FluentNHibernate.Utils.ExpressionToSql.Convert(Object value) в FluentNHibernate.Utils.ExpressionToSql.Convert(ConstantExpression expression) в FluentNHibernate.Utils.ExpressionToSql.Convert[T](Expression`1 expression, UnaryExpression body)

推荐答案

有2个通用的方式如何筛选映射集合。

There are 2 general ways how to filter mapped collections.

第一个是有点僵化,固定的,定义的映射其中=子句:

The first is a bit rigid, fixed, in a mapping defined where="" clause:


  • 6.2。映射的集合(...用流利的。凡(布尔表达式)。凡(Sql语句字符串)

  • 6.2. Mapping a Collection (...in fluent .Where(bool expr) or .Where(Sql statement string)

第二,也许真的很适合在这种情况下,是动态的版本称为过滤器:

The second and maybe really suitable in this scenario, is dynamic version called filter:

  • 18.1. NHibernate filters

NHibernate的增加了使用预先定义的过滤条件,并在两个类和集合级别附加的过滤器。过滤器条件是定义一个限制条款,现有的where属性的类和各种集合元素。除了这些过滤条件可以非常相似的能力然后进行参数设置。应用程序可以决定在运行时给定的过滤器是否应该启用和他们的参数值应该是什么。过滤器可用于像数据库的看法,但参数化应用....

NHibernate adds the ability to pre-define filter criteria and attach those filters at both a class and a collection level. A filter criteria is the ability to define a restriction clause very similiar to the existing "where" attribute available on the class and various collection elements. Except these filter conditions can be parameterized. The application can then make the decision at runtime whether given filters should be enabled and what their parameter values should be. Filters can be used like database views, but parameterized inside the application....

在流畅的执行将是这样的:

The implementation in fluent would look like this:

public class RecentFilter : FilterDefinition
{
    public RecentFilter()
    {
        WithName("RecentFilter")
            .WithCondition("( :EndedAtDate IS NULL OR EndedAt < :EndedAtDate )")
            .AddParameter("EndedAtDate",NHibernate.NHibernateUtil.DateTime);
    }
}

这是过滤器,这里是它在能说一口流利的映射用法:

this is the filter, and here is its usage in a fluent mapping:

mapping
   .HasMany(x => x.RecentOperations)
   ...
   .ApplyFilter<RecentFilter>();

在运行时,我们可以打开过滤器开/关在ISession的级别:

In runtime, we can turn filter on/off on the ISession level:

session.EnableFilter("RecentFilter")
       .SetParameter("EndedAtDate",DateTime.Now.AddDays(-15));

另请参阅:

  • property filter with fluent nHibernate automapping
  • Syntax to define a NHibernate Filter with Fluent Nhibernate?
  • Is it possible to use NHibernate Filters to filter through references?

这篇关于限制收集检索只读实体只是最近条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆