How to move from LINQ to SQL to “LINQ to WCF”?

2020-06-04 01:06发布

问题:

I'm putting together a WPF application which will eventually use WCF web services as its data source.

During the prototyping phase, I'm using LINQ to SQL on an SQL 2008 database and have come to appreciate the ease with which I can do "Add New Item | LINQ to SQL Classes" and generate a model class for a table, then speak to it with LINQ and pass this to my XAML UI, very nice.

But what happens when we switch the datasource from SQL Server to WCF web services? In order to continue using LINQ, am I going to have to:

  • write my own LINQ-to-web-service classes manually (using IQueryable etc.?)
  • is there a code generation tool that does this, something like "LINQ-to-WCF" (strange that this term doesn't even come up in Google)
  • or will I have to inefficiently get large amounts of records from my web service, create objects out of them, and then to LINQ-to-objects to query them?

Thanks for any direction you can give here.

ADDED: thanks for the answers, very helpful, but led to some confusion:

  • this MSDN tutorial says to "Add New Item | ADO.NET Entity Data Service"
  • I have VS2008 and can just do "Add New Item | ADO.NET Entity Data Model"
  • this MSDN video says to create a new project with the ADO.NET Data Service template which I don't have in VS2008
  • what are the differences between all of these, which are used most commonly?
  • and from what I have read so far anything that has to do with "LINQ to Entities" entail costs whereas "LINQ to SQL" is free, so do the "Entity Data Service/Model" options above have to do with LINQ to Entities?

回答1:

You'll probably want ADO.NET Data Services. You'll have to add an IUpdatable interface implementation to your entities, and you can use LINQ to SQL, Entity Framework, SubSonic, or a number of others. We're using the CodeSmith PLINQO templates, which allow you to add this functionality to LINQ to SQL.



回答2:

Have you had a look at ADO.NET Data services. Here you can query a data store over the wire with linq syntax.

You'll keep the LINQ to SQL data context but expose it to the web and allow your clients to query the context in standard LINQ statements. You would obviously have to secure the Data Services so only authorised clients can connect but essentially with minimal work you are able to get your data context "client side" so to speak.



回答3:

A generic LINQ to WCF may not make sense. In particular, WCF calls only expose data in very specific ways, which doesn't really conform to the LINQ way of doing things. For example, my service could have the following contract:

List<DAO> GetObjectsInDateRange(DateTime start, DateTime end);
void ExportObjectsToLegacySystem(List<DAO> data);

How do you express either in LINQ format. In particular, when start and end are not members of DAO. The only way to express it would be for the service to expose an IQueryable interface and I don't know how possible that is.

Now, you can always do:

var data = from dao 
in wcfService.GetObjectsInDateRange(DateTime.Today,DateTime.Today.AddDays(50))
where dao.AmountOfStuff > 20
select dao;

That being said, you can certainly create a LINQ interface tailored to your own service. Let's say your service has a method:

List<DAO> GetObjects(DAOFilter filterObject);

where the DAOFilter is an object that specifies all your "where" criteria, what you can do is write a LINQ provider that translates all the Expressions into this DAOFilter object. This would be an incredibly one-off solution with a considerable amount of work so I would advise against it.

I echo the advice to take a look at ADO.Net Data Services.



回答4:

You can add a Linq to SQL class directly to a WCF project and set Serialization Mode on the Linq to SQL class to Unidirectional. If you do this the classes will show up over the WCF service.