DDD 和 MVC 模型保存单独实体的 ID 还是实体本身?

发布于 2024-09-04 06:53:04 字数 623 浏览 6 评论 0原文

如果您有一个引用客户的订单,那么该模型是否包含客户的 ID 或客户对象的副本(如值对象)(考虑 DDD)?

我想这样做:

public class Order {
    public int ID {get;set;}
    public Customer customer {get;set;}
    ...
}

现在我这样做:

public class Order {
    public int ID {get;set;}
    public int customerID {get;set;}
    ...
}

将完整的客户对象包含在传递给表单的视图模型中比包含 ID 更方便。否则,我需要弄清楚如何将供应商信息获取到订单按 ID 引用的视图。

这也意味着存储库了解如何处理在调用保存时在订单对象中找到的客户对象(如果我们选择第一个选项)。如果我们选择第二个选项,我们将需要知道将其放置在视图模型中的位置。

可以肯定的是,他们会选择现有客户。然而,也可以肯定的是,他们可能想要更改显示表单上的信息。有人可能会争论让控制器提取客户对象,将客户更改单独提交到存储库,然后提交对订单的更改,同时将 customerID 保留在订单中。

If you have an Order that references a customer, does the model include the ID of the customer or a copy of the customer object like a value object (thinking DDD)?

I would like to do ths:

public class Order {
    public int ID {get;set;}
    public Customer customer {get;set;}
    ...
}

right now I do this:

public class Order {
    public int ID {get;set;}
    public int customerID {get;set;}
    ...
}

It would be more convenient to include the complete customer object rather than an ID to the View Model passed to the form. Otherwise, I need to figure out how to get the vendor information to the view that the order references by ID.

This also implies that the repository understand how to deal with the customer object that it finds within the order object when they call save (if we select the first option). If we select the second option, we will need to know where in the view model to put it.

It is certain they will select an existing customer. However, it is also certain they may want to change the information in-place on the display form. One could argue to have the controller extract the customer object, submit customer changes separately to the repository, then submit changes to the order, keeping the customerID in the order.

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

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

发布评论

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

评论(1

最笨的告白 2024-09-11 06:53:04

如果您有以下模型:

public class Order {
    public int ID {get;set;}
    public Customer Customer {get;set;}
    public object OrderProperty {get;set;}
    ...
}

和以下视图(来自 ViewPage),

您可以在视图中执行操作

Html.TextBoxFor(m=>m.OrderProperty);

,也可以

Html.HiddenFor(m=>m.Customer.Id)
Html.TextBoxFor(m=>m.Customer.Name)

在控制器中执行操作,它将自动绑定

public ActionResult SomeAction(Order order)
{
    // order.OrderProperty will be bound
    // order.Customer.Id will be bound
    // order.Customer.Name will be bound
}

If you have the following model:

public class Order {
    public int ID {get;set;}
    public Customer Customer {get;set;}
    public object OrderProperty {get;set;}
    ...
}

and the following view (from ViewPage<Order>)

you can do in your view

Html.TextBoxFor(m=>m.OrderProperty);

but also

Html.HiddenFor(m=>m.Customer.Id)
Html.TextBoxFor(m=>m.Customer.Name)

in your controller it will be bound automatically

public ActionResult SomeAction(Order order)
{
    // order.OrderProperty will be bound
    // order.Customer.Id will be bound
    // order.Customer.Name will be bound
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文