views:

262

answers:

3
+2  Q: 

LinqToSql and WCF

Within an n-tier app that makes use of a WCF service to interact with the database, what is the best practice way of making use of LinqToSql classes throughout the app?

I've seen it done a couple of different ways but they seemed like they burned a lot of hours creating extra interfaces, message classes, and the like which reduces the benefit you get from not having to write your data access code.

Is there a good way to do it currently? Are we stuck waiting for the Entity Framework?

+2  A: 

LINQ to SQL isn't really suitable for use with a distributed app. The change tracking and lazy loading is part of the DataContext which is tied to the database so cannot travel across the wire. You can move L2S entities across the wire, modify them, move them back and update the database by reattaching them to the DataContext but that is pretty limited and you lose all concurrency checks as the old values are never kept around.

BTW I believe the same is true for L2E.

Maurice
+2  A: 

It is certainly not a good idea to pass the linq-to-sql object around to other parts of a distributed system. If you do that, you would couple your clients to the structure of the database, which is never a good idea. This was/is one of the major problems with DataSets by the way.

It is better to create your own classes for the transfer of data object. Those classes, of course, would be implemented as DataContracts. In your service layer, you'd convert between the linq-to-sql objects and instances of the data carrier objects. It is tedious but it decouples the clients of the service from the database schema. It also has the advantage of giving you better control of the data that is passed around in your system.

Karl
A: 

I agree with Karl, however the issue I am facing given a similar requirement is where do my business entities fit in. In my current design the service layer gets the data from the database and populates a data transfer object that contains just the fields required for a particular view (to make it efficient for over-the-wire transfer). Would it be a good idea to have another layer of busniess entities between the data transfer object and the data layer for a more flexible design ?