J
Jakob Lithner
I have implemented a solution where I use LINQ-to-SQL to map all my objects
to SQL database. Now I want to create an automated history log of the main
objects. Objects are changed on several places in the application so my
intention was to call an AddHistory method everytime the Insert and Update
methods of the DBML file are called. The basic idea works fine, but I run
into some tricky problems on the details.
What is the best way to obtain previous values of the object?
I would like to log a descriptive text to make the user understand what has
happened.
My first try was to call the database just before the saving, to read
current database values just before they are changed. I created a temporary
datacontext object and used it to return the single object. Then I compared
all relevant properties between the current object and the database version
and logged every difference.
It worked fine for all simple properties but for extended properties
compiled from related tables I have some problems. With extended properties I
mean lookup values related to other tables. I don't want to refer to integer
values but rather the friendly names the user is used to!
If the temporary datacontext is closed before values are read I can not read
values from other tables. But if I keep the temporary datacontext alive it
will block cascading updates on related objects in same table.
Is it possible to create ReadOnly datacontext that will not block updates?
A completely different approach would be to create a detached oldvalue
collection inside the object from the beginning. Before saving the object it
would be easy to compare saving values with previous.
But extended properties need to be taken into account here as well! If I
retrieve a big collection of objects I don't want to slow down the initial
work by retrieving a lot of lookup values from other tables. In most cases
they will probably not be used anyhow.
Can I somehow INSIDE my partial class code detect when the PropertyChanging
event is fired? When a property is going to change seems to be a good place
to retrieve the previous values.
Any help is appreciated.
to SQL database. Now I want to create an automated history log of the main
objects. Objects are changed on several places in the application so my
intention was to call an AddHistory method everytime the Insert and Update
methods of the DBML file are called. The basic idea works fine, but I run
into some tricky problems on the details.
What is the best way to obtain previous values of the object?
I would like to log a descriptive text to make the user understand what has
happened.
My first try was to call the database just before the saving, to read
current database values just before they are changed. I created a temporary
datacontext object and used it to return the single object. Then I compared
all relevant properties between the current object and the database version
and logged every difference.
It worked fine for all simple properties but for extended properties
compiled from related tables I have some problems. With extended properties I
mean lookup values related to other tables. I don't want to refer to integer
values but rather the friendly names the user is used to!
If the temporary datacontext is closed before values are read I can not read
values from other tables. But if I keep the temporary datacontext alive it
will block cascading updates on related objects in same table.
Is it possible to create ReadOnly datacontext that will not block updates?
A completely different approach would be to create a detached oldvalue
collection inside the object from the beginning. Before saving the object it
would be easy to compare saving values with previous.
But extended properties need to be taken into account here as well! If I
retrieve a big collection of objects I don't want to slow down the initial
work by retrieving a lot of lookup values from other tables. In most cases
they will probably not be used anyhow.
Can I somehow INSIDE my partial class code detect when the PropertyChanging
event is fired? When a property is going to change seems to be a good place
to retrieve the previous values.
Any help is appreciated.