Sequence Generator

Posts   
 
    
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 29-Oct-2005 01:13:22   

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.

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. wink

Thanks for the consideration.

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-Oct-2005 13:59:00   

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. wink

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.

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 31-Oct-2005 21:48:42   

Thanks, Frans. I guess the semantics of the generator itself is up for debate (number reuse, etc), but having a built-in solution for sequencing still might be a worthwhile addition. I'm sure my scenario isn't unique as sequence structure can be a highly personalized requirement for certain line-of-business applications (think inventory, asset serialization, etc), especially in a cross-DB platform situation.

Anyway, thanks for the response. simple_smile

Jeff...