NHibernate 3 基于字符串的获取路径?

发布于 2024-11-04 21:32:04 字数 318 浏览 0 评论 0原文

我正在尝试使用 NHibernate 实现来实现 DAO 接口。该接口要求公开类似 Fetch 的方法,并具有以下签名:

IQueryable<TEntity> AllIncluding(params string[] properties);

EntityFramework 等效实现很简单,因为它们有一个 Include(string prop) 扩展方法,但我在 QueryOver 或 NHIbernate 中没有看到类似的内容LINQ;只是采用 Func 的 Fetch 方法。有什么想法如何解决这个问题吗?

谢谢

I'm trying to implement a DAO interface with an NHibernate implementation. The interface calls for a Fetch-like method to be exposed, with the following signature:

IQueryable<TEntity> AllIncluding(params string[] properties);

The EntityFramework equivalent implementation is easy, as they have an Include(string prop) extension method, but I dont' see anything like that in either QueryOver or NHIbernate LINQ; just the Fetch method that takes a Func. Any ideas how to resolve this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-11-11 21:32:04

你尝试过类似的事情吗?

session.QueryOver<Product>()
    .UnderlyingCriteria.SetFetchMode("PropertyName", FetchMode.Eager);

据我了解,QueryOver 只是 Criteria 的包装,Criteria 还有一个非类型化/基于字符串的设置获取模式版本。

更新

因此,您的方法可以如下所示:

public IQueryOver<TEntity, TEntity> AllIncluding(params string[] properties)
{
    var queryOver = session.QueryOver<TEntity>();
    var criteria = queryOver.UnderlyingCriteria;

    foreach (var property in properties)
    {
        criteria.SetFetchMode(property, FetchMode.Eager);
    }

    return queryOver;
}

Did you try something like ?

session.QueryOver<Product>()
    .UnderlyingCriteria.SetFetchMode("PropertyName", FetchMode.Eager);

As I understand, QueryOver is just a wrapper around Criteria, and Criteria has also a non-typed/string-based version of setting fetch mode.

Update

So, your method can look like:

public IQueryOver<TEntity, TEntity> AllIncluding(params string[] properties)
{
    var queryOver = session.QueryOver<TEntity>();
    var criteria = queryOver.UnderlyingCriteria;

    foreach (var property in properties)
    {
        criteria.SetFetchMode(property, FetchMode.Eager);
    }

    return queryOver;
}
南烟 2024-11-11 21:32:04

只是为了方便起见,Meligy 在他的解决方案中提供了方法的发布和扩展版本,

public static class QueryOverExtensions
{        
    public static IQueryOver<T, T> AllIncluding<T>(this IQueryOver<T, T> source, params string[] properties)
    {
        var criteria = source.UnderlyingCriteria;
        foreach (var property in properties)
        {
            criteria.SetFetchMode(property, FetchMode.Eager);
        }
        return source;
    }
}

现在您可以像这样调用该方法:

queryOver.AllIncluding<TEntity>("property").where(...);

Just for convenience ill post and extension version of the method Meligy provided in his solution

public static class QueryOverExtensions
{        
    public static IQueryOver<T, T> AllIncluding<T>(this IQueryOver<T, T> source, params string[] properties)
    {
        var criteria = source.UnderlyingCriteria;
        foreach (var property in properties)
        {
            criteria.SetFetchMode(property, FetchMode.Eager);
        }
        return source;
    }
}

now you can call the method like this:

queryOver.AllIncluding<TEntity>("property").where(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文