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
|
This is not a new issue - but it's causing me problems so I need to revisit it.
if(this.BankAccount.MostRecentTransactionRetrievalId.HasValue && this.BankAccount.MostRecentTransactionRetrievalId == this.Id){
related posts: |
|
|
From initial discussions here we continue to suggest Jeremy's approach. You can get around the save ordering issue by initially saving with a null or dummy BankAccount.MRTRId, then once the save has completed and the TransactionRetrieval.Id has been assigned, update the BackAccount.MRTRId and save again. You can wrap both the saves in a transaction so nobody will see the null or dummy MRTRId. Re 4: no. Barring other references that keep the entity and EC live, the GC will be able to reclaim both despite the mutual reference. *does the circular reference dance on COM's grave* |
|
|
ok, that's great thanks. I did think I should be able to do everything through the model - the bit i was missing previously is that I wasn't doing a two-stage save. I was trying to set BankAccount.MRTRId in BankAccount.OnSaving() - which was where the insert/update ordering was getting out of sync. The suggested approach does rely however on anyone adding a TransactionRetrieval doing so in the prescribed way (transacted 2-stage save) doesn't it? I can't find any post-save event which would allow me to define this all on the BankAccount entity and thereby enforce this right in the model rather than in code external to the model (like a repository). How about an OnSaved event that runs within the same transaction as the Save? Then I could do this: [Transient] I can think of numerous reasons why you wouldn't want to expose a Post-Save hook but it would allow me to prevent any holes where people don't save stuff right - at least in this scenario ;) For the time being I'll just rely on the fact that only I am writing code that adds TransactionRetrievals to BankAccounts so I know the right way to do it - but is there a way of enforcing this within the model? cheers |
|
|
You can kind of do that by overriding UnitOfWork.SaveChanges(bool). Something like this: public override void SaveChanges(bool reset) { (Warning: code above has never even been compiled, but hopefully gives you the idea!) Obviously, this is pretty similar to the 'enforce it in a repository' approach, just slightly further down the stack; it could still be bypassed by somebody not using your strong-typed unit of work. |
|
|
perfect thanks. that's at the right level for me - if someone isn't using the same strong typed UnitOfWork that's used everywhere else then they deserve inconsistent data... :D |
|