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?
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.
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.
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:
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:
That being said, you can certainly create a LINQ interface tailored to your own service. Let's say your service has a method:
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.
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.