IQueryable的< T>结果在加载实体后执行业务逻辑 [英] Iqueryable<T> result execute business logic after loading entities

查看:69
本文介绍了IQueryable的< T>结果在加载实体后执行业务逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF MVVM应用程序和一个通用的Datamodel,它返回一个IQueryable< t>

我想在执行查询时执行一些业务逻辑(例如,用特定值更新poco属性)在实体的结果集上。这个业务逻辑很重要,所以我不想把它放在我使用IQuerable的代码中。我希望在Datamodel级别可以实现这一点。我知道最好避免在Datamodel之外使用IQueryable,但由于某些特定原因,这是不可能的。



每个人的想法?



提前致谢



我的尝试:



我用谷歌搜索了几个小时,但我找不到适用的解决方案。

I have an WPF MVVM application and a generic Datamodel which returns an IQueryable<t>
I want to execute some business logic (e.g. update poco properties with specific values) when the query has been executed on the result set of Entities. This business logic is important so I don't want to put this everywhere in code where I use the IQuerable. I'll hope it's somehow possible to do this at Datamodel level. I know it's better to avoid using IQueryable outside of your Datamodel but for some specific reasons it's impossible.

Ideas everyone?

Thanks in advance

What I have tried:

I have Googled for several hours but I can't find an applicable solution.

推荐答案

Linq扩展包括ToArray,ToList等所有这些(包括foreach等) ...)运行转换IQueriable< t>的查询进入其他数据类型。



您应该使用分部类在您的数据模型中构建IQueriable。这是一个例子:



Linq extensions includes ToArray, ToList etc. All of these (including foreach etc...) run the query converting your IQueriable<t> into other data types.

You should build your IQueriable within your datamodel using partial classes. here is an example:

namespace myContextNamespace
{
public partial class Item
{
    internal IQueriable<Item> Query(ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return db.Items;
    }
    internal IQueriable<Item> QueryById(int id, ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return Query(db).Where(i=>i.Id == id);
    }
    public List<Item> SelectAll(ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return Query(db).ToList();
    }
    public Item SelectById(int id, ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return QueryById(id,db).SingleOrDefault();
    }
}
}





看看我如何将可查询保持为内部?我可以在条件连接等情况下在其他部分类中使用它,但是我总是在执行查询后返回对象。



在这种情况下我可能想要始终排除ExpiredDate的项目不为null。我只是像这样更改查询:



see how I keep the queriable as internal? I can use it in other partial classes in the case of conditional joins etc., but I always return the objects after executing the query.

In this case I may want to always exclude items with "ExpiredDate" not null. I simply change the queries like so:

namespace myContextNamespace
{
public partial class Item
{
    internal IQueriable<Item> Query(ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return QueryWithExpired(db).Where(i=>!i.ExpiredDate.HasValue);
    }
    internal IQueriable<Item> QueryWithExpired(ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return db.Items;
    }
    internal IQueriable<Item> QueryById(int id, ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return Query(db).Where(i=>i.Id == id);
    }
    public List<Item> SelectAll(ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return Query(db).ToList();
    }
    public Item SelectById(int id, ContextDB db = null)
    {
        if(db==null)
            db = new ContextDB();
        return QueryById(id,db).SingleOrDefault();
    }
}
}





请注意我如何更改Query以排除过期但保留了内部基本查询QueryWithExpired。我将保留这个以防万一我将来需要从另一个部分类访问它们。



这是你可以将所有查询逻辑保留在数据模型中并且永远不会暴露在它之外的IQueriable。



Note how I have changed Query to exclude expired but kept an internal base query "QueryWithExpired". I will keep this in case I need to access them from another partial class in the future.

This was you can keep all your query logic within your datamodel and never expose an IQueriable outside of it.


这篇关于IQueryable的&LT; T&GT;结果在加载实体后执行业务逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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