I created data access classes that encapsulate all communication with Linq2Sql. These classes have their own datacontext, which they use on their objects.
public class ClientDataLogic { private DataContext _db = new DataContext(); public Client GetClient(int id) { return _db.Clients.SingleOrDefault(c => c.Id == id); } public void SaveClient(Client c) { if (ChangeSetOnlyIncludesClient(c)) _db.SubmitChanges(); } }
Of course, you will need to support this object provided that you need objects.
Checking if only the rigth object has been modified is a bit of a bother; you could do methods like
void ChangeClientValue(int clientId, int value);
but it can become a lot of code.
Attaching and detaching is the missing feature of Linq2Sql, if you need to use a lot, maybe you are using Linq2Entities.
source share