简体   繁体   English

如何使用ASP.Net MVC绑定丰富的域模型

[英]How to bind rich Domain Model with ASP.Net MVC

I am trying to get up and running with DDD and there seems to be some limitations imposed by technology stack in my case ASP.Net that make you think about how things will work together. 我正在尝试DDD的启动和运行,在我的案例ASP.Net中,技术堆栈似乎施加了一些限制,使您思考事情如何协同工作。 For example let's say I have created a rich class of ShoppingCart and my ShoppingCart has two methods of AddItem(String Sku) and RemoveItem(String Sku) . 例如,假设我创建了一个丰富的ShoppingCart类,而我的ShoppingCart具有AddItem(String Sku)RemoveItem(String Sku)两种方法。 Now I might have a lot of logic in these two methods with a lot of my business rules validation. 现在,通过大量的业务规则验证,这两种方法中可能有很多逻辑。

What happens when I want to bind my ShoppingCart to the UI using ASP.Net MVC? 当我想使用ASP.Net MVC将ShoppingCart绑定到UI时会发生什么? Ideally I would like to call the AddItem and RemoveItem methods when the user adds or remove an item on the UI so that I can validate all the rules, but normally we bind the UI to View Models (POCO classes). 理想情况下,当用户在UI上添加或删除项目时,我想调用AddItemRemoveItem方法,以便我可以验证所有规则,但是通常我们将UI绑定到视图模型(POCO类)。 Thus when the user saves the cart from UI I will get a list of POCO classes that I now need to map to my actual business object. 因此,当用户从UI保存购物车时,我将获得一个POCO类列表,现在我需要将其映射到我的实际业务对象。 Should I iterate over each item and track it from existing data and call AddItem and RemoveItem methods now? 我是否应该遍历每个项目并从现有数据中跟踪它并立即调用AddItemRemoveItem方法?

In this case I need to keep an instance of my domain object in memory for that user so that I can perform operations on it. 在这种情况下,我需要为该用户在内存中保留域对象的实例,以便可以对其执行操作。 Even then I will have bulk of logic in controller to decide which methods to call on the Business object. 即使那样,我将在控制器中拥有大量逻辑来决定要在Business对象上调用哪些方法。

The more I am thinking down these lines the more confusing it gets because the problem would not happen on let's say Winforms, where you can easily call the respective methods on the Domain object from various event. 我越想这些行,就越容易混淆,因为问题不会发生在Winforms上,您可以在其中轻松地从各种事件中调用Domain对象上的各个方法。

What am I missing in this whole picture to make DDD work like it should? 我在整个图片中缺少什么来使DDD正常工作?

Typically, adding and removing resources would be an http POST call. 通常,添加和删除资源将是http POST调用。 So, the method on your controller handling this call would receive a model of the request to add or remove an item to the cart. 因此,控制器上处理此调用的方法将收到请求模型,以向购物车添加或删除商品。 For example: 例如:

public class AddItemToCartRequest
{
    public string CartId { get; set; }
    public string ItemId { get; set; }
}


public class SomeController : Controller
{
    // Reference to some sort of repository/data store for shopping carts
    private Carts carts;

    // Reference to some sort of repository/data store for store items.
    private Items items;

    public SomeController(Carts carts, Items items)
    {
        this.carts = carts;
        this.items = items;
    }

    [HttpPost]
    public ActionResult AddItem(AddItemToCartRequest request)
    {
        var cart = carts.GetCart(request.CartId);
        var item = items.GetItem(request.ItemId);

        cart.AddItem(item);
        carts.Save(cart);

        // Redirect to action showing the "item added" or whatever.
    }
}

The idea is that you do not pass rich domain models back and forth to the view. 这个想法是,您不会将富域模型来回传递给视图。 You pass classes that are models of the views and requests that you are using. 您传递的类是视图和正在使用的请求的模型。 On each request, you would fetch domain model instances from a repository/data store (ie, the carts and items fields in the example). 根据每个请求,您将从存储库/数据存储中获取域模型实例(即示例中的carts和items字段)。 The request data only need to specify identifiers of the domain model instances to fetch. 请求数据仅需要指定要获取的域模型实例的标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM