开发者

Can Identity Generators be used for other columns with NHibernate

I'm creating an invoicing feature and I want to use a counter for the invoice number,开发者_StackOverflow but instead of writing an implementation I was wondering if it is possible to use an existing identity generator in NHibernate for this property?


Although there are many identity generators for NHibernate, Ayende, i.e. Mr NHibernate himself advices against it:

Before I start, I wanted to explain that NHibernate fully support the identity generator, and you can work with it easily and without pain.

There are, however, implications of using the identity generator in your system. Tuna does a great job in detailing them. The most common issue that you’ll run into is that identity breaks the notion of unit of work. When we use an identity, we have to insert the value to the database as soon as we get it, instead of deferring to a later time. It also render batching useless.

And, just to put some additional icing on the cake. On SQL 2005 and SQL 2008, identity is broken.

I know that “select ain’t broken” most of the time, but this time, it appears it does :-)

We strongly recommend using some other generator strategy, such as guid.comb (similar to new sequential id) or HiLo (which also generates human readable values).


Technically, yes, you can. They're all public classes living in the NHibernate.Id namespace, so you can instantiate and use any of them whenever you want.

In practice though, it depends on which one you want to use. Some of them are fairly simple and don't require any configuration or dependencies, like CounterGenerator, GuidCombGenerator or UUIDStringGenerator. Others need the session, like NativeGuidGenerator. Others need to be configured before they can be used, like SequenceHiLoGenerator.

I don't think NHibernate supports using generators other than in ids and idbags, so using them is entirely up to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜