本文介绍了如何实现无间隙,在NHibernate的用户友好的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个应用程序,我的订单对象需要有一个顺序的和用户友好编号字段。我避免了高住低训算法,因为它产生了相当大的差距(见的)。当然,的Guid 值会让我的企业用户去香蕉。我也避免了因它的主要缺点甲骨文序列:

I'm designing an application where my Order objects need to have a sequential and user-friendly Id field. I'm avoiding the HiLo algorithm because of the rather large gaps it produces (see here). Naturally, Guid values would make my corporate users go bananas. I'm also avoiding Oracle sequences because of the major disadvantages of it:

(来源: NHibernate的POID发电机透露)

后插入发电机,正如其名
  建议,后分配的ID
  实体被存储在数据库中。一个
  select语句执行打击
  数据库。他们有很多缺点,
  而在我看来,他们必须使用
  只在棕地项目。 这些
  发电机是什么我们不建议
  作为NH团队。

>有些缺点是
  以下内容:


      
  • 工作单元是通过使用破碎
      这些战略。这不要紧,如果
      您正在使用FlushMode.Commit,每
      结果保存在INSERT语句
      针对DB。作为最佳实践,我们
      应按照插入的承诺,
      但使用后插入发生器
      使得它在提交保存(这是什么
      UOW不会做)。

  •   
  • 这些战略
      抵消配料,你不能拿
      优点发送多个查询
      一次(因为它必须去在数据库
      保存的时间)。

  •   

任何想法/上实现它们之间不存在重大差距人性化的标识经验?

Any ideas/experience on implementing user-friendly IDs without major gaps between them?

编辑:


  • 用户友好 编号字段是那些我的企业用户能记住,甚至讨论和/或有电话交谈谈论一个特定的订单其code,例如: 我打电话知道为什么订单#1625被拒绝。

  • 编号不需要为严格无间隙,但我担心,当他们看到这样的差距100我的用户会感到困惑,201,305。我的大项目,我现在实现使用NHibernate偶尔失去了几序列时,抛出异常,但同时也是一个相当整齐为了保持他们的Oracle序列。缺点他们是如何突破的工作单元导致额外的命中数据库中使用或不使用调用Session.flush每个保存命令

  • User friendly Id fields are ones my corporate users can memorize and even discuss and/or have phone conversations talking about a particular Order by its code, e.g. "I'm calling to know why the order #1625 was denied.".
  • The Id doesn't need to be strictly gapless, but I am worried that my users would get confused when they see gaps like 100, 201, 305. For my older projects, I currently implement NHibernate using Oracle sequences which occasionally lose a few sequences when exceptions are thrown, but yet keep a rather tidy order to them. The downside to them is how they break the Unit of Work which results in additional hits to the database for every Save command with or without the Session.Flush.

推荐答案

一个选择是保持一个密钥表中只存储一个递增值。这可能会引入一些问题,即可能锁定问题,以及附加命中到数据库。

One option would be to keep a key-table that simply stores an incrementing value. This can introduce a few problems, namely possible locking issues as well as additional hits to the database.

另一个选择可能是提炼你所说的用户友好的ID的意思。这可能包括一个日期/时间和一个客户特定序列(或包括客户ID以及)的组合。此外,您的订单ID并不一定要上表中的实际键。有什么可说的,你不能用一个单独的计算栏从而重新presents订单ID的代理键。

Another option might be to refine what you mean by "User-friendly Id". This could consist of a combination of a Date/Time and a customer-specific sequence (or including the customer id as well). Also, your order id does not necessarily have to be the actual key on the table. There is nothing to say that you can't use a surrogate key with a separate "calculated" column which represents the order id.

的底线是,这听起来像你想使用代理键,但有一个自然键的好处。它可以是非常困难的左右逢源和大量归结到你实际上是如何计划使用这些数据,如何对用户间preT的数据和个人preference。

The bottom-line is that it sounds like you want to use a surrogate key, but have the benefits of a natural key. It can be very difficult to have it both ways and a lot comes down to how you actually plan on using the data, how users interpret the data, and personal preference.

这篇关于如何实现无间隙,在NHibernate的用户友好的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:21