泛型和数据库访问
我有以下方法,可以传入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Linq to Ado.NET Dataservices 或 WCF Dataservices,您的模型将为您构建大量类型。一般来说,您会进行选择和过滤。您需要以下内容,那么您的所有方法都只是在此之上的糖果:
查询类型 1 - 一个过滤器,返回一个列表:
查询类型 2 - 一个过滤器,返回单个实体:
public void makeQuery(stringentity, Expression>)过滤器、操作回调)
{
您需要做的是重载这些并将过滤器替换为简单的过滤器数组,
并重复单个和列表返回。
如果您想要一个数据上下文,则将其捆绑到一个单例中,或者在某种混合工厂/单例中跟踪一组上下文,然后您就可以离开了。让构造函数获取上下文,或者如果未提供上下文,则使用它自己的上下文,然后您就可以离开了。
然后,我在一大行中使用它,但全部集中在一个地方:
这可能看起来很复杂,但它隐藏了很多异步魔法,并且在某些情况下可以直接从按钮处理程序调用。与其说是一个三层系统,不如说是一个节省大量时间的系统。
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:
Query Type 2 - One Filter, returns a single entity:
public void makeQuery(string entity, Expression> filter, Action callback)
{
What you need to do is overload these and swap out the filter for a simple array of filters
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:
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.