Return HTML from bounded context - domain-driven-design

I've recently read a book about Domain Driven Design. But, I surprised when I read that sending back the HTML from bounded context to the presentation layer is one of the two approaches to return data from bounded context(another is via various data types like JSON and XML, ... as everyone uses it)!
This is the exact content of the book:
HTML APIs versus Data APIs
By constructing web pages with snippets of HTML that are returned from
each bounded context, you give bounded contexts control of the
appearance and behavior of specific regions of a page.
Now, I have two question about this:
Is it really a good way to return HTML?
Where HTML should be generated? In the application services as the last layer of bounded context or other layers or what?
reference: PATTERNS, PRINCIPLES, AND PRACTICES OF DOMAIN-DRIVEN DESIGN
By Scott Millet & Nick Tune, published by Wrox

I think the book is talking about composed UIs there. The html returned by each BC means the UI of the BC. The whole UI is a composition of all UIs. So each BC has its own UI.
Another approach would be to have just one UI. The BCs would have no UI, just application layer. The UI would call the application layer of the BC it needs, usually the glue between the UI and the BCs APIs would be a REST API gateway.

Related

DDD. Strategic designing Bounded Contexts on a Web Project

I'm starting on DDD and I have a doubt on application of DDD on a Web Project.
If I have multiple Bounded Contexts for every section of a web project. For example, "Catalog" and "Shopping Cart" on a E-Commerce project. ¿Where should be the code that implements the frontend for all the Web and presents concepts from many Bounded Contexts?
I have thought about creating the "Web" Bounded Context, but this Bounded Context won`t represent a specific Ubiquitous Language because this BC will use concepts of many Bounded Contexts and Subdomains.
What you do think about this?
Thanks.
Where this code goes depend on the structure of your application.
DDD is a set of patterns and rules that helps you model your business. This model should be ubiquitous, meaning different applications should share the same business logic. The main rule of DDD is what describes the business goes in the domain, everything else does not. DDD does not state anything about how to you should structure your application, it can be applied to any architecture.
What you describe is called presentation logic and does not describe your business logic. It describes how your system interacts with clients, which are external actors and is specific to your application: if you make a web or a mobile version of your app, chances are that you will have the same domain implementation but your presentation logic will slightly differ. So, there is no DDD answer to where the presentation logic goes, besides not in the domain.
If you make a traditional 3-layered application, this logic goes in the presentation layer.

Using seperate Bounded Context Person Management (Speakers, Chairs, Authors, Participants) in Conference Management?

We built an application for planning the scientific program of conferences including rooms, sessions, lectures (oral oder poster presentations), speakers, chairs and so on (program planning context).
One important part are scientific papers with their authors from the paper submission which also can lead to lectures or poster presentations.
The paper management context is a separate bounded context and uses an external api to sync data between a third party tool. It acts as an Anti-Corruption Layer (ACL) for the program planning context.
The concept of speaker, chair and author has a person in common. I want to introduce a seperate person management context to reduce duplicated people and reuse unique people in program planning and paper management contexts using the unique identifier of the person and display information through composite UI. Later we want to integrate another conference registration context with the same ACL logic for another third party api.
As I learned from many books, blogs etc. only the person management context should own the person's data and create new instances.
The consequence would be to create a new person per author (sync api call) when importing all data from the external paper management api or alternatively use one batch call at the beginning.
Is this coupling to high? Should I store duplicated personal data of the authors in the paper management context? This seems wrong and makes the deduplication hard.
You may want to reconsider your use of DDD. This might be something more easily solved without the constraints of DDD because it sounds like it has a lot of data.
Assuming you want to keep DDD then you would need to decouple your contexts. There are several ways to decouple your contexts but my 2 favorite are:
Domain Events
Rest Api
You can trigger a domain event after you extract each author and the event can be handled by any context needing it. You could consume a rest api where you can send author data and it'll serve as the "Application Service" for your second domain.
I think I understood your problem/question. Correct me if I haven't solved it or addressed something completely different.

What type of UML diagram should I use to model the parts of a web app?

What type of UML diagram should I use to model the parts of a web app?
For instance, lets say my parts are as followed:
Server
Database
Web App
Multiple APIs
The web app interacts with all the other parts in some way. From my research, the best options seem to be either component, deployment, or sequence.
Here's a general idea that I'm going for with my diagram, but I do not know which UML diagram this would best be represented in.
If you know the diagram I should use, I would like to know how each part is represented in the diagram. For example, in a deployment diagram, I know that a server/database would be nodes. And, APIs would possible be artifacts?
For designing a web app (or any other type of software application), following a best practice Model-View-Controller (MVC) codebase architecture, you first need to make an information design model, typically in the form of a UML class diagram, defining the model classes, or the "model", of your app. The "view" (or user interface) of your app is based on the model.
Your diagram attempts to model the deployment architecture of your app. But this question comes after choosing a codebase architecture and an information architecture.
You can find more explanations about the architecting process, and the code of several example web apps, in my book Web Applications with JavaScript or Java, which is also available as an open access online book.
This depends a lot on what you're wanting to model and communicate. All of the diagram types you mention would be useful ways to describe a web app. Each focuses on a different aspect of the web app -- component models are about software component structure (database, web app, apis), deployment models show how instances of these components are deployed into deployment nodes (servers, devices etc). Like Thomas says, this is a very broad question -- in essence you are asking how should you describe your web app's solution architecture which is not a trivial exercise. Is there something more specific I can help with -- can you narrow down what it is you want to show about your app?
Your start with the component diagrams is fine. Generally your question is too broad to be answered here, but a few bread crumbs anyway:
In a next step you can describe the single components which are hosted inside the single nodes. You can make the nodes navigable (means you have a composite diagram showing their guts) so you can place component instances showing their interfaces. The latter can be shown with either lollipops or as stereotyped classes which the single components implement. Following that you can break down components in a similar fashion showing how the components are implemented with various classes and how these realize the single interfaces. It's possible to show the interfaces outside the components and <<delegate>> them inside the specific classes.
The story goes on, but it's too big to be told here.

A bounded context is a full application?

I've been reading about DDD and bounded contexts and I think I'm getting the idea wrong. At first, I liked the idea of subdomains and bounded contexts, I understood it like that: there's a software to be developed, but attacking all at once is too much, so we break it into logical pieces and develop each at once. Another problem we solve is ambiguities on the ubiquitous language.
This led me to think about bounded contexts as basically just folders where I group and bound code related to some specific piece of the application. This code I believed to be made up from things like
The domain model of that bounded context, including abstractions for repositories and services
Infrastructure layer for that bounded context, implementations of repositories and so on
Of course, being the domain model and infrastructure properly separeted within the bounded context.
Reading further, it seems, however, that each bounded context is an entire application on it's own right. It seems, sometimes, that each bounded context has it's own application layer, for instance.
This made me confused, because sometimes I don't want to end up developing tons of applications, I just one to develop one. The bounded context division of the application was supposed to build one app, not many apps to be integrated.
I've seem this question where #MikeSW says both approaches presented by the OP are valid. What I'm asking is about a third structure:
<bc 1>
|_ domain
|_ infrastructure
<bc 2>
|_ domain
|_ infrastructure
|_ application
|_ presentation
At least for all the applications I've seem this makes much more sense. I want one app, not several apps with several presentations, but I still want to be able to break the domain and benefit of things like "bounding the ubiquitous language".
So, is a bounded context a full aplication? Or can a bounded context be used like I understood and felt more useful? There are any problems with my approach?
The domain layer is usually the most complex part of your program, and can also change often due to business requirements and refactoring. So you generally don't want to expose it directly to your presentation layer or other bounded contexts. If you feel that you can expose it, it might be the case that your application logic or use case methods are mixed into your domain layer, or that your program is not large or complex enough to require multiple BC's to begin with. Otherwise, I would go with including the application layer in each BC to protect the domain model's integrity and expose only the commands that need to be called from a use case perspective.
I want one app, not several apps with several presentations, but I
still want to be able to break the domain and benefit of things like
"bounding the ubiquitous language".
You can have a thin application layer for each bounded context, and still have a single presentation layer. This is sometimes called a "composite UI", which should be considered a separate BC in itself. If you need to handle common logic such as authentication, create another application service or facade in the composite UI and have it handle the authentication before in turn calling the application service of an outside BC.
I think most of the examples you see in books and on the web are over-simplified in that they have 1 BC per physical running application (and perform some kind of network communication between them), whereas in the real world you might have a complex application that you need to split into separate logical units, but not run them as separate processes unless the need arrives.
At the end of the day the answer is both. The important thing to take away from bounded context is not how you structure your app, but that you have different spaces where you model specific behavior relating to some context. How you define the boundaries between these contexts is dependent on the problem you need to solve.
There is nothing wrong using namespaces(folders) to define bounded contexts. Like you said most of the time you are simply writing one application. You can also define your bounded context by having separate projects for each context. In this case your presentation layer will reference the project it needs.
There are many right ways to code DDD. You should ask yourself "Am I following the core principle by doing it this way"
The bounded context describes a subset of the complete solution and everything within that context serves that context. So, imo, each context has it's own domain so it could be a separate application or just a subsystem of the same project. The point of the "context" is that the ubiquitous language applies directly TO that context. For example, a User in the Account context might mean something completely different than a User in the Sales context. Each "User" will have different capabilities and follow different rules in each context. Each context needs to be isolated from any other context and are not allowed to share references (unless it's via a 'Shared' context); any communication should be mediated through a service that sits on top of that context. A context doesn't even have to follow DDD to be "DDD compliant" since each context can follow it's own approach (e.g. domain driven, data driven, etc.). Contexts are simply silos that outline a logical section of the business.
Whatever you need to do to prevent direct references across contexts is fine whether that means different namespaces, different assemblies within a solution, or different projects altogether.
The bounded context is the scope on which the code operates. It relies on a domain model, that can be supported by a ORM (or not). It implements different kinds of services (domain services and application services) but its aim is to expose only domain services to its environment. DDD is a service oriented architecture, meant to work as offline as possible and in a loose-coupled way. You may decide to consume your services in different ways. The solution implements different kinds of components, different kinds of layers, different kinds of projects. I believe the most critical attention must concern the model, that should not be distributed across components. Solution design and domain model are orthogonal purposes.

DDD and data export system

I'm a beginner in DDD and am facing a little problem with architecture.
Our system must be able to export business data in various formats (Excel, Word, PDF and other more exotic formats).
In your opinion, which layer must be responsible for the overall process of retrieving source data, exporting them in the target format and preparing the final result to the user ? I'm confusing between domain and application responsibilities.
And regarding the export subsystem, should implementations and their common interface contract belong to the infrastructure layer ?
Neither Application nor Domain layer or any other 'layer'. DDD is not a layered architecture. Search for onion architecture or ports and adapters pattern for mor on this subject.
Now to the core of your problem. The issue you are facing is a separate bounded context and should go to a separate component of your system. Lets call it Reporting. And as it's just a presentation problem, no domain logic - DDD is not suitable for it. Just make some SQL views, read them using NHibernate, LINQ2SQL, EF or even plain DataReaders and build your Word/whatever documents using a Builder pattern. No Aggregates, Repositories, Services or any other DDD building blocks.
You may want to go a bit further and make all data presentation in your application to be handled by a separate component. Thats CQRS.
I usually take the simple approach where possible.
The domain code implements the language of your domain - The nouns, verbs etc.
Application code creates poetry using this language.
So in your example the construction of the output is an application concern, while the construction of the bits that the application will talk about (since you don't mention it) would the the domain concern.
e.g. if the report consists of sales data, that things like dates, bills, orders etc. would be abstractly constructed in the domain, while the application would concerned with producing documents using these.

Resources