Hi, I want to track DateLastModified (DateTime) and LastModifiedBy (User) in all of my entities. What is the most efficient way? I currently override the OnValidate() method and update these two fields. However, when I'm creating an entity, there is an INSERT query that inserts all of the other fields of the entity, then a second query that uses UPDATE to update the DateLastModified and LastModifiedBy fields. I'd like for all of this to be done in 1 query. One option is to override OnPropertyChanged and set the two field's values there, but I feel that maybe this will affect performance for entities with a large number of fields. The other option is to use ITransactionEventWatcher.OnBeforeTransactionCommit(...). In this method, is it possible to get all of the entities that have been modified and are being committed? That way I could update their DateLastModified and LastModifiedBy fields. Would this lead to 1 query? |
Hello After investigations and fixes the most efficient way to implement such functionality is following:
Obvious that it is most straigtforward approach and now it works. While there still possible situations (two cases) when object creation completes with two queries DataObjects will try to use single INSERT whenever it is possible. Let me explain when DataObjects will perform two queries:
That is all. As you can see our Let now consider implementation of Well. You need the new assembly to try the suggested aproach. It will soon available at our website or I can send it by e-mail. |
While Alex Ustinov keep silence about v3.X, I'll give an answer on how to do the same in v4.X - just to keep the people informed ;) Actually, there is a wide set of options:
IMO, #2 is the nicest option:
Btw, you should think about tracking Hi Alex! I've found that #3 works perfectly for me, could you tell us why do you prefer #2? 1
Well, #2 and #3 aren't much different:
So likely, #3 is really better :) Btw, concerning INSERT + UPDATE: there is no such problem with DO4. If it can do this at once (i.e. there is no explicit demand to flush the changes between creation and version update) - it will. Moreover, as you know, insertions, updates and regular queries are batched there. |
Hello, Ara
I've investigated the issue today. The bad news: current version can't insert new instance in a single query. But there also exist good news: I've fixed Session.Persist() method a bit and now it is able to use single INSERT query per instance in certain circumnstances, otherwise it will be exactly two queries INSERT+UPDATE.
(The two stages insert required e.g. in the case when data object has refernce fields and
CreateForeignKeys
database option is used.)I'll going to test the fixes tomorrow. After that I'll publish recommended implementation for DateLastModified here.