Linq To SQL and Change Tracking
I have been using Linq To SQL in a project recently. Linq is a great way to traverse through a collection of objects. Its a very nice utility and can get rid of those unsightly nested foreach loops with if/elses in your code. Linq To SQL is also a nice basic Object Relational Mapper. The issue is "when the rubber meets the road", as it were. Linq seems a slight bit incomplete. Take this example:
A simple ChangeLog class. It has an Enum ChangeLogStates that has 4 states, Created, Modified, StatusChanged (a disposition), Shelved(basically deleted). Now, i could do an ChangeLog.add(LogStates.Created/Modified/Changed/Shelved,Object Key, strDescription) to every line of code where there is a change in the database....everywhere i insert, update the underlying entity. But Using the beauty of OOP and Linq to SQL, i can impletement the Datacontext's insert and update partial methods:
insertLinqItem works great!
the problem lies with the other statuses!
Linq doesnt do change tracking! I was shocked. There is an IsDirty or HasChanged property. Datasets have a DiffGram concept that allows you to check the changes that have happened during the lifetime of the dataset. What happened? Luckily i can pepper my code with changelog lines everywhere an entity has been changed, but why cant i just use the OOP way? I'd have to create a change tracking framework!
Anyhow, Sorry for the simplistic example, There is a great blog entry on Change tracking in Linq by Freek Leemhuis. Visit for More information and other links.
A simple ChangeLog class. It has an Enum ChangeLogStates that has 4 states, Created, Modified, StatusChanged (a disposition), Shelved(basically deleted). Now, i could do an ChangeLog.add(LogStates.Created/Modified/Changed/Shelved,Object Key, strDescription) to every line of code where there is a change in the database....everywhere i insert, update the underlying entity. But Using the beauty of OOP and Linq to SQL, i can impletement the Datacontext's insert and update partial methods:
partial void insertLinqItem(LinqItem item); partial void updateLinqItem(LinqItem item);
insertLinqItem works great!
partial void insertLinqItem(LinqItem item){
ChangeLog.add(ChangeLogState.Created, item.ID, string.format("New Item was created with ID:{0}", item.ID));
}
the problem lies with the other statuses!
partial void updateLinqItem(LinqItem item){
//TODO: how in the world do i find out if the item has been changed
//Done: I can check if the current status is Shelved
if(item.status == "shelved"){
ChangeLog.add(ChangeLogState.Shelved, item.ID, string.format("Item {0} was Shelved", item.ID));
}
//else if(item.oldstatus != item.newstatus? || item.newstatus.isdirty?
//TODO: How do i tell if a specific property item.status has been changed?!
//ChangeLog.add(ChangeLogState.StatusChanged, item.ID, string.format("Item's Status was changed from {0} to {1}", item.oldstatus, item.newstatus));
}
//DONE: I can Tell when any other property other than status has been changed.....
ChangeLog.add(ChangeLogState.Modified, item.ID, string.format("Item {0} was modified", item.ID));
}
Linq doesnt do change tracking! I was shocked. There is an IsDirty or HasChanged property. Datasets have a DiffGram concept that allows you to check the changes that have happened during the lifetime of the dataset. What happened? Luckily i can pepper my code with changelog lines everywhere an entity has been changed, but why cant i just use the OOP way? I'd have to create a change tracking framework!
Anyhow, Sorry for the simplistic example, There is a great blog entry on Change tracking in Linq by Freek Leemhuis. Visit for More information and other links.
Comments