生成业务密钥 [英] Generate a business key

查看:96
本文介绍了生成业务密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为业务密钥生成唯一编号的最佳方法是什么?

What is the best way to generate a unique number for a business key?

在下面的示例中,我们的客户想要一个可以用来引用订单的数字(OrderNumber).

In the example below, our customer wants a number (OrderNumber) they can use to refer to an Order.

<class name="Order">
  <id name="Id" ... />

  <property name="OrderNumber" column="order_number" />

</class>

推荐答案

我发现执行这些任务的最佳方法是用代码编写它们,而不是将它们泄漏到数据层中.我不让NHibernate计算它.我将此概念视为应用程序的完整功能.很好,因为我可以为此编写测试并具有灵活性.有时在计算下一个序列号时还涉及其他因素,例如与另一个实体的关系.也许您希望在客户领域内获得顺序的订单号:对于客户02是02-001、02-002,对于客户01是01-001、01-002.

I've found that the best way to perform these tasks is to write them in code and not leak them into the data layer. I don't let NHibernate calculate it. I treat this concept as a fully fledged feature of the application. This is nice because I can write tests for it and have flexibility. Sometimes there are other factors involved in calculating the next sequence number, like a relationship to another entity. Maybe you want sequential order numbers within the realm of a client: 02-001, 02-002 for client 02 and 01-001, 01-002 for client 01.

因此,我将SequneceNumber的概念推广到领域模型对象,并使用NHibernate进行映射,并执行我的逻辑.每当我需要一个新的界面时,我都会使用看起来像

So I promote the concept of a SequneceNumber to a domain model object, map it with NHibernate and do the logic on my side. Whenever I need a new one, I use an interface that looks something like

public interface ISequenceRepository
{
    string GetNextSequenceNumber(Customer customer);
}

它管理数据库表的一个记录(每个客户)中的当前序列号.每当我调用此函数时,它都会增加数据库中的占位符并返回值.

And it manages the current sequence number in one record (per Customer) in a database table. Whenever I call this function it increments the place-keeper in the database and returns the value.

工厂依赖于此接口,并使用它来构建需要序列号的对象.

A factory depends on this interface and uses it to build up the objects that need a sequence number.

这篇关于生成业务密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆