泛型和数据库访问

发布于 2024-08-08 20:36:59 字数 950 浏览 9 评论 0原文

我有以下方法,可以传入 lambda 表达式来过滤结果,然后传入一个可处理结果列表的回调方法。这只是我的系统中的一个特定表,我将一遍又一遍地使用这个构造。我如何构建一个通用方法,比如以表作为参数的 DBget(公平地说,是一个 ADO.NET 数据服务实体)并传入一个过滤器(一个 lambda 表达式)。

 public void getServiceDevelopmentPlan(Expression<Func<tblServiceDevelopmentPlan, bool>> filter, Action<List<tblServiceDevelopmentPlan>> callback)
        {
            var query = from employerSector in sdContext.tblServiceDevelopmentPlan.Where(filter)
                        select employerSector;


            var DSQuery = (DataServiceQuery<tblServiceDevelopmentPlan>)query;
            DSQuery.BeginExecute(result =>
            {
                callback(DSQuery.EndExecute(result).ToList<tblServiceDevelopmentPlan>());

            }, null);

    }

我对此的第一次打击是:

public delegate Action<List<Table>> DBAccess<Table>(Expression<Func<Table, bool>> filter);

I have the following method I can pass in a lambda expression to filter my result and then a callback method that will work on the list of results. This is just one particular table in my system, I will use this construct over and over. How can I build out a generic method, say DBget that takes a Table as a parameter(An ADO.NET dataservice entity to be fair) and pass in a filter (a lambda experssion).

 public void getServiceDevelopmentPlan(Expression<Func<tblServiceDevelopmentPlan, bool>> filter, Action<List<tblServiceDevelopmentPlan>> callback)
        {
            var query = from employerSector in sdContext.tblServiceDevelopmentPlan.Where(filter)
                        select employerSector;


            var DSQuery = (DataServiceQuery<tblServiceDevelopmentPlan>)query;
            DSQuery.BeginExecute(result =>
            {
                callback(DSQuery.EndExecute(result).ToList<tblServiceDevelopmentPlan>());

            }, null);

    }

My first bash at this is:

public delegate Action<List<Table>> DBAccess<Table>(Expression<Func<Table, bool>> filter);

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

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

发布评论

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

评论(1

忘东忘西忘不掉你 2024-08-15 20:36:59

如果您使用 Linq to Ado.NET Dataservices 或 WCF Dataservices,您的模型将为您构建大量类型。一般来说,您会进行选择和过滤。您需要以下内容,那么您的所有方法都只是在此之上的糖果:

查询类型 1 - 一个过滤器,返回一个列表:

public  void makeQuery<T>(string entity, Expression<Func<T, bool>> filter, Action<List<T>> callback)
    {
        IQueryable<T> query = plussContext.CreateQuery<T>(entity).Where(filter);

        var DSQuery = (DataServiceQuery<T>)query;
        DSQuery.BeginExecute(result =>
        {
            callback(DSQuery.EndExecute(result).ToList<T>());
        }, null);

    }

查询类型 2 - 一个过滤器,返回单个实体:

public void makeQuery(stringentity, Expression>)过滤器、操作回调)
{

        IQueryable<T> query = plussContext.CreateQuery<T>(entity).Where(filter);
        var DSQuery = (DataServiceQuery<T>)query;
        DSQuery.BeginExecute(result =>
        {
            callback(DSQuery.EndExecute(result).First<T>());
        }, null);

    }

您需要做的是重载这些并将过滤器替换为简单的过滤器数组,

Expression<Func<T, bool>>[] filter

并重复单个和列表返回。

如果您想要一个数据上下文,则将其捆绑到一个单例中,或者在某种混合工厂/单例中跟踪一组上下文,然后您就可以离开了。让构造函数获取上下文,或者如果未提供上下文,则使用它自己的上下文,然后您就可以离开了。

然后,我在一大行中使用它,但全部集中在一个地方:

GenericQuery.Instance.Create().makeQuery<tblAgencyBranches>("tblAgencyBranches", f => f.tblAgencies.agencyID == _agency.agencyID, res => { AgenciesBranch.ItemsSource = res; });

这可能看起来很复杂,但它隐藏了很多异步魔法,并且在某些情况下可以直接从按钮处理程序调用。与其说是一个三层系统,不如说是一个节省大量时间的系统。

If you are using Linq to Ado.NET Dataservices or WCF Dataservices, your model will build you a lot of typed. Generally though you will be selecting and filtering. You need the following, then all your methods are just candy over the top of this:

Query Type 1 - One Filter, returns a list:

public  void makeQuery<T>(string entity, Expression<Func<T, bool>> filter, Action<List<T>> callback)
    {
        IQueryable<T> query = plussContext.CreateQuery<T>(entity).Where(filter);

        var DSQuery = (DataServiceQuery<T>)query;
        DSQuery.BeginExecute(result =>
        {
            callback(DSQuery.EndExecute(result).ToList<T>());
        }, null);

    }

Query Type 2 - One Filter, returns a single entity:

public void makeQuery(string entity, Expression> filter, Action callback)
{

        IQueryable<T> query = plussContext.CreateQuery<T>(entity).Where(filter);
        var DSQuery = (DataServiceQuery<T>)query;
        DSQuery.BeginExecute(result =>
        {
            callback(DSQuery.EndExecute(result).First<T>());
        }, null);

    }

What you need to do is overload these and swap out the filter for a simple array of filters

Expression<Func<T, bool>>[] filter

And repeat for single and list returns.

Bundle this into a singleton if you want one datacontext, or keep track of an array of contexts in some sort of hybrid factory/singleton and you are away. Let the constructor take a context or if non are supplied then use its own and you are away.

I then use this on a big line but all in one place:

GenericQuery.Instance.Create().makeQuery<tblAgencyBranches>("tblAgencyBranches", f => f.tblAgencies.agencyID == _agency.agencyID, res => { AgenciesBranch.ItemsSource = res; });

This may look complicated but it hides a lot of async magic, and in certain instances can be called straight from the button handlers. Not so much a 3 tier system, but a huge time saver.

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