锁定商业模式

发布于 2024-09-14 07:26:26 字数 925 浏览 1 评论 0原文

我需要客户端应用程序“锁定”(也称为“签出”数据库中存储的某些业务实体。

工作流程是这样的:

  1. 用户导航到某个业务对象的页面。

  2. 用户点击“编辑”。

  3. 这将锁定该项目,防止其他人编辑。

    >
  4. 其他工作站上的其他用户将在正在编辑的项目旁边看到一个小“锁定”图标,并且“编辑”按钮将是只读的。

  5. 管理员用户可以单击按钮“强制”解锁该项目。

这很标准,对吧?我过去已经这样做过很多次了,这次我正在寻找一些关于“正确”方法的想法。 ...

也就是说,我想有两种方法:

  1. 让我的业务对象实现一些 ILockable 接口,该接口具有 LockOwnerId 属性,并且让数据库中的相应表具有相同的 LockOwnerId。

  2. 在数据库中拥有一个集中的“EntityLocks”表,用于管理当前锁定/签出的实体的所有实体类型/主键对。

至于获取锁的 API,我只是想到了类似于 CheckOut 方法的东西:

// Returns true if the check-out was successful, 
// false if the check-out was not successful, becase the item was already locked. If 
// force is set to true, will check out the toCheckOut to the current user, regardless 
// of existing check-outs.
bool CheckOut(object toCheckOut, bool force)

有什么想法吗?

谢谢。

I have the necessity for a client application to "lock" (aka "check-out" certain business entities store in the database.

The workflow is such that:

  1. The user navigates to a page for some business object.

  2. The user hits "edit".

  3. This locks the item from being edited by anyone else.

  4. Other users on other workstations will see a little "lock" icon by the item being edited and the "edit" button will be read-only.

  5. Admin users may click a button to "force" unlocking the item.

Pretty standard, right? I've done this a bunch of times in the past and I'm looking for some thoughts on the "right" way of doing this this time...

Namely, I guess there are two approaches:

  1. Have my business objects implement some ILockable interface which has a LockOwnerId property and have the corresponding table in the DB have that same LockOwnerId.

  2. Have a centralized "EntityLocks" table in the DB that manages all entity-type/primary key pairs of entities that are currently locked/checked out.

As for the API for acquiring a lock, I'm simply thinking of something along the lines of a CheckOut method:

// Returns true if the check-out was successful, 
// false if the check-out was not successful, becase the item was already locked. If 
// force is set to true, will check out the toCheckOut to the current user, regardless 
// of existing check-outs.
bool CheckOut(object toCheckOut, bool force)

Thoughts?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-09-21 07:26:26

就我个人而言,如果可以的话,我通常会通过请求更改业务流程来尝试远离这种模式。然而,这并不总是可能的,因此 Martin Fowler 将其描述为实际模式。

您描述的解决方案听起来不错。不过,我宁愿让 CheckOut 方法抛出异常,但返回 bool 除外。这更加明确。当开发人员忘记处理这种异常情况(无法签出该项目)时,错误将更容易发现(因为未捕获异常)。

另外,请注意 CheckOut 方法的实现。确保它是交易性的。您最不希望发生的事情是两个用户都获得同一个对象的锁定,因为实现没有正确实现。

祝你好运。

Personally, I usually try to stay away from this pattern if I can, by requesting the business processes to be changed. This however, is not always possible and for this reason Martin Fowler described it as an actual pattern..

The solution you described sounds fine. However, I rather let the CheckOut method throw an exception except returning a bool. This is much more explicit. When developers forget to handle this exceptional case (the item could not be checked out) the error will be much easier to spot (because the exception isn't caught).

Also, please take good care of the implementation of the CheckOut method. Make sure it is transactional. The last thing you want is the possibility that two users both got a lock on the same object, because the implementation isn't implemented correctly.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文