Composing bounded context on single page

2019-05-23 14:32发布

问题:

I'm starting large project and I want use DDD. The main problem is how to display data from multiple Bounded Context without duplicating data and mappings of NH. I watched Udi's podcast about composite application. He mention about using Razor sections to display data from multiple bounded contexts but he doesn't provide any details. Does anybody know how to use it or does anybody know other way?

回答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.



回答2:

In regards to NH mappings, each bounded context (BC) should have its own set of mappings and therefore its own session factory. It can be tricky to configure a DI container such that it resolves the appropriate session factory for each respective BC, because the session factory interface will have to be "tagged" to be associated with a particular BC and then all dependencies within that BC will also have to be associated with that tag. Another option is to create a open host service (such as REST) to encapsulate each BC and then reference the service from your web app. This way you don't have to worry about managing NH mappings in your web application.