2

我正在启动大型项目,我想使用 DDD。主要问题是如何在不复制 NH 的数据和映射的情况下显示来自多个 Bounded Context 的数据。我观看了 Udi 关于复合应用程序的播客。他提到了使用 Razor 部分来显示来自多个有界上下文的数据,但他没有提供任何细节。有人知道如何使用它或有人知道其他方式吗?

4

2 回答 2

1

Good thing about Razor is that it allows you to have completely independent controllers that are responsible for rendering parts of the single page (portal style). For example in your main Razor view:

<some_markup> New products </some_markup>

@{ Html.RenderAction("Get", "NewProducts"); }

<some_markup> Product ratings </some_markup>

@{ Html.RenderAction("Get", "ProductRatings"); }

Where NewProductsController and ProductRatingsController belong to different Bounded Contexts and look like this:

public class NewProductsController {

    private readonly IProducts repository;

    public NewProductsController(IProducts repository) {
        ...
    }

    [ChildActionOnly]
    public ViewResult Get() {
        // load products from repository and
        // return corresponding ViewModel 
    }
}

public class ProductRatingsController {

    private readonly IProductRatings repository;

    public ProductRatingsController(IProductRatings repository) {
        ...
    }

    [ChildActionOnly]
    public ViewResult Get() {
        // load product ratings from repository and
        // return corresponding ViewModel 
    }
}

Note that controllers don't know about each other although they will display data on the same page. The repositories can be injected using DI container in the Composition Root of your application.

于 2012-06-07T04:04:13.250 回答
1

关于 NH 映射,每个有界上下文 (BC) 都应该有自己的一组映射,因此也应该有自己的会话工厂。配置 DI 容器以使其为每个相应的 BC 解析适当的会话工厂可能很棘手,因为必须“标记”会话工厂接口以与特定 BC 相关联,然后该 BC 中的所有依赖项也将必须与该标签相关联。另一种选择是创建一个开放的主机服务(例如 REST)来封装每个 BC,然后从您的 Web 应用程序中引用该服务。这样您就不必担心在 Web 应用程序中管理 NH 映射。

于 2012-06-08T17:59:39.540 回答