This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Before calling SaveChanges on a UnitOfWork is there any way to get a list of entities that are pending insert, update, or delete?
Something like the Linq DataContext that has a method called GetChangeSet. |
|
|
This isn't currently available. If you could say a bit more about your scenario then we can see if we can suggest an alternative approach, and/or add it to the wish list. Thanks! |
|
|
With LinqToSql I have an extension that does runtime reflection auditing. That way I don't have to write any specific code for database auditing in a project. All I have to do is call my extension on a LinqToSql DataContext. Make changes to entities...then call It works as follows:
What is great about this approach is that I don't have to write specific code for each project...the reflection does it all. |
|
|
That makes sense. We'll put this on the wish list. In the meantime, you can still get similar functionality in a couple of ways. 1. Create a standard base entity type and override OnSaving to perform the reflection and log the audit data. Then derive all your concrete entity types from this standard base type. (If you are using the designer, be sure to specify concrete table inheritance and mark the base type as abstract, or declare the base entity type in plain C# and reference it as an external base class via the LightSpeed Model window.) 2. Intercept the places where an entity may enter the unit of work, and hook the Saving event. To do this, create your own class derived from UnitOfWork (or, if you are using LINQ and the designer, create a partial class for the generated strongly-typed unit of work class), and override the Find(Query, IList), FindOne(Query), Find(ProcedureQuery, IList), Add(Entity) and Attach(Entity) methods. In each override, call the base implementation, then add a handler for the Saving event for the entity (or for each entity returned from the Find). The handler would again perform the reflection and log the audit data. Obviously these are not as simple or neat as iterating the changeset but hopefully one of them will do in the meantime. Also, in option 2 if extending the LINQ UOW class you probably couldn't reuse the exact same file across multiple projects due to namespace issues; however the body of the code would still be exactly the same across projects. Note: I think we had a bug a while back where OnSaving / the Saving event did not occur for deletes. If you run into this, please upgrade to a nightly build. |
|
|
I know that this post is a bit old now so if there is another more sensible way of doing this please directly me appropriately. I have a similar requirement where essentially if an Entity is of a particular type then it must be audited. Auditing entails putting an entry into another tabled called "MyEntity_archive". I have been trying to follow the approach in #1 above but get into an stackoverflow scenario. Below is my code and I'm hoping that you either have a suggestion or an alternative approach to solving this problem. Thanks, David
|
|
|
You are getting a stack overflow because OnSaving calls SaveChanges before the entity has been saved. So the sequence is: SaveChanges -> -> look for dirty entities -> someEntity is dirty -> OnSaving(someEntity) -> SaveChanges() -> look for dirty entities -> someEntity is dirty -> OnSaving(someEntity) -> SaveChanges() -> ... However, as you note this thread is a bit old, and in fact since LightSpeed 3 you have been able to get the pending change set as follows:
(Obviously you can filter this to entities of a particular type using the OfType operator.) So a better approach is probably to skip OnSaving, and get the list of pending changes requiring audit and add the audit records before calling SaveChanges. |
|