查询DTO通过WCF对象与LINQ to SQL的后端 [英] Query DTO objects through WCF with linq to sql backend

查看:188
本文介绍了查询DTO通过WCF对象与LINQ to SQL的后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个项目,我们需要建立一个反对
A WCF服务的复杂查询工作。

I am working on a project where we need to create complex queries against a WCF service.

该服务使用LINQ在后端和项目为sql查询到的数据传输对象是这样的:

The service uses linq to sql at the backend and projects queries to data transfer objects like this:



    dbContext.GetQueryable()
                  .Where(x => x.Id == formatId)
                    .Select(x => FormatHelper.PopulateMSFormat(x))
                    .ToList();



我想要做的是指定在客户端的查询,可以说,我要查询其所有格式某财产或他们夫妇。在这种风格
的内容:

What I want to do is to specify a query on the client side, lets say i want to query all formats having a certain property or a couple of them. Something in the style of this:



     var assets = client.QueryForAssets().Where(x => (x.name == "Test" || x == "Arne") && x.doe ==  "john");



荫知道我不能返回的IQueryable了WCF但类似的东西可以用OData的服务来完成。现在的问题是,我要返回DTO的和OData的,让我很容易结合到L2S-的datacontext这暴露我的数据模型,而不是DTO的。

Iam aware that I can't return IQueryable over WCF but that something like that can be done with OData services. The problem is that I have to return the DTO's and OData let me quite easily bind to L2S-datacontext which exposes my data model and not the DTO's.

那么,有没有序列化对DTO的查询效率会传播到L2S层的好办法吗?

So is there a good way of serializing a query against the DTO that efficiently will propagate to the l2s layer?

我想过写我自己的查询语言,但是我发现,这将是十分困难的构建正确的表达式树作为谓词L2S因为没有映射从DTO到LINQ类。

I thought about writing my own query language, but I found out that it would be quite hard to build the correct expression tree as a predicate to l2s as there is no mapping from the DTO to the linq classes.

推荐答案

通过的OData 服务,你不一定要返回数据库实体直。你可以简单地返回任何 DTO 在可查询的格式。然后使用LINQ的选择()方法的帮助下,你可以简单地转换任何数据库实体到 DTO 服之前查询:

With OData services, you are not bound to return database entities directly. You can simply return any DTO in queryable format. Then with the help of LINQ's Select() method, you can simply convert any database entity into DTO just before serving the query:

public class DataModel
{
  public DataModel()
  {
    using (var dbContext = new DatabaseContext())
    {
      Employees = from e in dbContext.Employee
                  select new EmployeeDto
                  {
                    ID = e.EmployeeID,
                    DepartmentID = e.DepartmentID,
                    AddressID = e.AddressID,
                    FirstName = e.FirstName,
                    LastName = e.LastName,
                    StreetNumber = e.Address.StreetNumber,
                    StreetName = e.Address.StreetName
                  };
    }
  }

  /// <summary>Returns the list of employees.</summary>
  public IQueryable<EmployeeDto> Employees { get; private set; }
}

您现在可以轻松地设置此为这样一个OData的服务:

You can now easily set this up as a OData service like this:

public class EmployeeDataService : DataService<DataModel>

有关全面实施细则,的看到这个的关于这一主题的优秀文章。 OData的服务实际上是非常非常强大的,一旦你得到他们的手。

For full implementation details, see this excellent article on the subject. OData services are actually very very powerful once you get the hand of them.

这篇关于查询DTO通过WCF对象与LINQ to SQL的后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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