Has anyone used a Key Generator class?

I was reading Martin Fowler's book "Patterns of Enterprise Application Architecture".

Inside he talks of using a Key Generator for business transaction's that require multiple inserts on different tables, and still maintain valid keys during the entire course of the transaction.

Has anyone architected a Key Generator in .NET and if so, could you tell me about the pro's and con's that you experienced?

Or even ways around using a Key Generator that you've found to avoid a Key Generator.

Thanks,

Nick

[564 byte] By [nick5454] at [2008-1-1]
# 1
Guid key= Guid.NewGuid();
seems to do that quite well
The pros are obvious - you don't have to worry about keys and they are easy to generate etc.
the cons are that it isn't a meaningful (natural) key and it is hard to type in test queries
and indexing which can be solved by rearranging the bits a little

As far as the business scenario I would probably won't generate a key in the UI for the backend (especially if using SOA) but that's another question I guess

HTH,

Arnon

ArnonRotemGalOz at 2007-9-12 > top of Msdn Tech,Architecture,Architecture General...
# 2

Yes a GUID would be an easy choice but, i'd rather not explore that because most DBA's wont allow that as a solution to the table. They will argue that guids are too large for the table. I know this, because I have already been in that discussion before.

Thank you for replying.

I am referring to the Business Layer, actually as it pertains to Domain Object patterns. I should have been more clear about that, sorry.

If anyone has any example of what they used and why. I would greatly appreciate it.

Thanks,'

Nic

nick5454 at 2007-9-12 > top of Msdn Tech,Architecture,Architecture General...
# 3
The other methods I used to achieve distributed keys was either pre-assign bits so that each site/component would have its own range (e.g. first 5 bits designate the site, next 10 bits the sub system within a site etc.) - this isn't a very good way as you usually run out of bits ( to my defense I'd say I was very inexperienced when I used that). The other option besides GUIDs (which I never had problems using) was to lease key ranges. So an interested component can go to a central component (either in the DB or not) and acquire a range for while . There are few policies you can use for determining how much time a lease can hold few examples include indefinitely, fixed time, varying by range size (the large the range the shorted the lease) etc.)
Leasing keys gives you increased autonomy on the leasing component as it doesn't have to interact with a DB or another service or whatever to get a valid new identifier

Arnon

ArnonRotemGalOz at 2007-9-12 > top of Msdn Tech,Architecture,Architecture General...