如何在ORM中传播联接参数 [英] How to propagate a parameter for joins in a ORM

查看:120
本文介绍了如何在ORM中传播联接参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前问过的问题的续集。到目前为止,我正在努力实施的架构演进问题的策略是慢慢变化的维度。我现在看到的问题如下。考虑以下数据模型:

This is a sequel of a question I asked before. So far the strategy for Schema Evolution problem I'm trying to implement is Slowly changing dimension. The problem that I have now looks as following. Consider the following data model:

class Bar
{
    int id;
    int param;

    double x;
    double y;
}

class Foo
{
    int id;
    int param;

    string name;
    string description;

    collection<Bar> bars;
}

class Delta
{
    int id;
    int param;

    double rating;
}

class RootEntity
{
    int id;
    int param;

    collection<Foo> foos;
    collection<Delta> deltas;
}

请注意,所有实体都有 int param 属性。它具有相同的名称,但它不是外键。
这是很简单的模型,就像一个例子。考虑到集合是巨大的,而对象树的方式更加复杂。在EntityFramework技术中,
我可以执行以下操作:

Note, that all entities have int param attribute. It has the same name but it is not foreign key. This is quite simplified model, just as an example. Consider that collections are huge and object tree is way more sophisticated. In EntityFramework technology I can do something like:

  class MyDbContext : DbContext
  {
        public DbSet<RootEntity> rootEntities { get; set; }
  }

我想做的是将选择限制为 PARAM 。这可以如下

What I want to do is limit the selection by param. That can be as following

dbContext.rootEntities.Where(e => e.param == some_value);

问题是只有dbContext.rootEntities会在生成的SQL代码中进行过滤。其余的(与Foo,Bar和Delta连接)不会被过滤。
因此是一个问题:如何传播 .Where(e => e.param == some_value)到底层对象选择的所有连接?

The problem is that only dbContext.rootEntities will be filtered in generated SQL code. The rest (joins with Foo, Bar and Delta) will not be filtered. Hence is the question: how to propagate .Where(e => e.param == some_value) to all joins for underlying objects selection?

我正在寻找EntityFramework或NHibernate的解决方案。

I'm looking at solutions for EntityFramework or NHibernate.

谢谢。

推荐答案

使用NHibernate,您可以使用过滤器,如下所示:

With NHibernate you can use Filter per table like this:

public class FooMap : ClassMap<Foo>
{
    ApplyFilter("Param", "param = :paramValue");
}

public class BarMap : ClassMap<Bar>
{
    ApplyFilter("Param", "param = :paramValue");
}

public class XyzMap : ClassMap<Xyz>
{
    // This can be the (abstract?) base class map
    ApplyFilter("Param", "param = :paramValue");
}

在通过会话呼叫任何实体之前,启用过滤器:

Before you call any entity by the session enable the filter:

Session.EnableFilter("Param").SetParameter("paramValue", objValue);

这篇关于如何在ORM中传播联接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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