jeffreygg wrote:
For future consideration:
We have a requirement to generate sequential numbers for the various documents in our system: PO, Requisition, Work Order, etc. These numbers, however, may be of various formats as defined by the customer (i.e., they may contain a single segment, multiple segments, have identifying positions in the string, etc). We've built a swappable sequence generator that can accomodate these various formats but the best place to call for the next number --and lock it-- is literally right before the document is saved. And, if any errors ocurred, the number must be released.
This is unsafe. You can't release a generated number upon error. A sequence number claimed once is gone forever. This is also the reason why sequences in db's never re-use a number already given out, even if the query it's used in fails.
We're going to look into tapping the BeforeEntitySaved and AfterEntitySaved (not sure if those are the correct names) events to do this, but I thought it would be a neat feature to be able to specify a factory class the entity uses for a sequence number (not necessarily the PK, mind you, but it might be) and automatically retrieves the value and releases it at the appropriate time. All, of course, in my unrelenting pursuit to avoid template changes.
You can simply derive a class from DataAccessAdapter and override OnBeforeEntitySave and indeed apply the sequence there, or better: bind an include template to the include template id for DataAccessAdapter and generate the code into the DataAccessAdapter class.
I didn't implement the pattern which is called 'Identity Map', which provides sequences for entities, because it can be unsafe in general to use these things in a multi-threaded system (e.g. duplicate keys), and also because the db systems offer a nice way to do this which IS safe.