0

我有一个在 MonoRail 上运行并使用 Castle ActiveRecord/NHibernate 的购物车应用程序,并且有一个 ShoppingCart 表和一个 ShoppingCartItems 表,它们映射到实体。

场景如下:用户将东西添加到购物车,比如 5 件商品,然后去查看购物车。购物车显示所有 5 件商品。用户复制选项卡/窗口并获得同一购物车的另一个​​选项卡(称为选项卡 B)。用户从购物车中删除了一个项目,所以现在选项卡 B 中有 4 个项目,但在原始选项卡 A 中,仍然有 5 个项目。用户返回选项卡 A,更新购物车中的某些内容,然后单击提交更改的“更新”按钮。我的 MonoRail 操作尝试使用视图中的数据对 ShoppingCartItems 执行 ARDataBind,其中包括所有 5 个项目。当它到达用户从选项卡 B 中删除的项目时,它会为该项目抛出“不存在具有给定标识符的行”。

我不知道是否有办法让它不绑定该行、返回 null、返回新实例等?ARDataBind 属性上有一个 AutoLoadBehavior 参数,但这似乎只影响子实体的加载,而不影响根实体的加载。无论我选择哪个选项,我都会在控制进入操作方法之前得到异常(AutoLoadBehavior.Never 除外,但这对我没有帮助)。

相反,我有代码调用 Request.ObtainParamsNode() 来拉取表单节点并将它们手动解析为对象,并忽略不再存在的那些。有没有更好的办法?

谢谢。

4

1 回答 1

1

Inherit ARDataBinder, override FindByPrimaryKey(Type targetType, object id):

protected override object FindByPrimaryKey(Type targetType, object id) {
  return FindByPrimaryKey(targetType, id, false);
}

The key here is the false parameter, which makes it return null instead of throwing.

Then inherit ARDataBindAttribute, override the CreateBinder() method and make it return your new binder instead of the default ARDataBinder.

Then apply your custom binder attribute instead of ARDataBindAttribute.

于 2010-03-20T22:09:20.903 回答