简体   繁体   English

对代码和 NHibernate QueryOver 使用相同的规范<t></t>

[英]Using same specification for code and NHibernate QueryOver<T>

Scenario设想

I'm working on an application that has a little section dedicated to displaying statistics about the current state of the system.我正在开发一个应用程序,该应用程序有一小部分专门用于显示有关系统当前 state 的统计信息。 This includes such things as how many items are overdue, count of all pending items, count of all items without filters.这包括诸如过期项目的数量、所有待处理项目的计数、没有过滤器的所有项目的计数等。 When the user opens the application they must select which item labels they wish to work on before seeing any items in the UI.当用户打开应用程序时,他们必须 select 在查看 UI 中的任何项目之前他们希望处理哪些项目标签。 Once that is done the database gets queried and returns a list back to the user.一旦完成,数据库就会被查询并返回一个列表给用户。 Each item has an associated label.每个项目都有一个关联的 label。 The item label is auto-generated before the users see it. label 项目是在用户看到它之前自动生成的。 One of these labels could be "Overdue", which indicates to the user that it should be completed first.其中一个标签可能是“过期”,它向用户表明它应该首先完成。

Problem问题

I'm struggling to find a good way to centralize the logic that determines if an item is overdue.我正在努力寻找一种集中确定项目是否过期的逻辑的好方法。

The way I do this now is simple create a Future NHibernate query and do all my statistic counts in one database transaction.我现在这样做的方法很简单,创建一个Future NHibernate 查询并在一个数据库事务中完成我的所有统计计数。 The issue is that once I retrieve the items from the database (which is already assigned a label, but if an item is overdue the system is supposed to override the label).问题是,一旦我从数据库中检索到项目(已经分配了 label,但如果项目过期,系统应该覆盖标签)。 So once the data is retrieved (after the users select their labels) I parse it into a tree and need to check each item whether it's overdue or not.因此,一旦检索到数据(在用户 select 他们的标签之后),我将其解析为一棵树,并且需要检查每个项目是否过期。 The logic is quite simple but located in 2 places because two different parts of the system need it for different purposes.逻辑非常简单,但位于两个地方,因为系统的两个不同部分需要它用于不同的目的。

My view model bound to my UI contains this:我的视图 model 绑定到我的 UI 包含以下内容:

public bool IsOverdue
{
    get
    {
        return
            LabelName == "Please Advise" &&
            DateTimeProvider.Current.DateTime.AddDays(2) > WorkToBeginDateTime;
    }
}

public void TakeActionIfOverdue()
{
    if(IsOverdue)
    {
        LabelName = "Overdue";
        LabelBackground = "#123456";
    }
}

I call TakeActionIfOverdue() whenever I'm building my tree, which overrides the label name if required.每当我构建我的树时,我都会调用TakeActionIfOverdue() ,如果需要,它会覆盖 label 名称。

And my NHibernate query looks like this:我的 NHibernate 查询如下所示:

Domain.Locate locateAlias = null;
Status statusAlias = null;

var overdueCountQuery = _session.QueryOver(() => locateAlias)
    .Select(Projections.RowCount())
    .JoinAlias(() => locateAlias.Status, () => statusAlias)
    .Where(() => statusAlias.Name == "Please Advise")
    .Where(() => locateAlias.WorkToBeginDateTime
        .IsBetween(DateTimeProvider.Current.DateTime.Subtract(new TimeSpan(48, 0, 0)))
        .And(DateTimeProvider.Current.DateTime))
    .Where(() => locateAlias.IsComplete == false)
    .FutureValue<int>();

As you can see the logic determining if an item is overdue is in two places.如您所见,确定项目是否过期的逻辑在两个地方。

My TreeView (which gets parsed and displayed once the user selects which label they'd like to see) looks like this.我的TreeView (一旦用户选择他们想要查看的 label 就会被解析和显示)看起来像这样。

- Some Heading
    - Item #1 (Foo Label)
    - Item #3 (Bar Label)
- Another Heading
    - Item #2 (Bar Label)
    - Item #4 (Bar Label)
    - Item #6 (Overdue)

In this scenario the user selected label Foo Label and Bar Label .在这种情况下,用户选择了 label Foo LabelBar Label For the sake of this example, let's pretend Item #6 had label Bar Label .为了这个例子,让我们假设Item #6有 label Bar Label However, the UI is displaying that it has label Overdue because it was due 2 days ago.但是,UI 显示它有 label Overdue ,因为它是 2 天前到期的。

And my little statistics box looks like this.我的小统计框看起来像这样。

Items Overdue: 4
Pending Items: 45
All Items: 49

Question问题

How can I centralize this logic?我怎样才能集中这个逻辑?

Use the QueryOver library to build the your Specifications classes without performance issues and with fluent features of the QueryOver.使用 QueryOver 库来构建您的 Specifications 类,而不会出现性能问题并具有 QueryOver 的流畅特性。 The only issue is the dependency with the NHibernate, since the LINQ should be the default model to execute queries.唯一的问题是与 NHibernate 的依赖关系,因为 LINQ 应该是执行查询的默认 model。

        var list =
            repository.Find(courseWithVotes.By().And(courseByExample.By(new Course() { Name = "ava" })));

Examples: http://queryoverspec.codeplex.com/示例: http://queryoverspec.codeplex.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM