-
Notifications
You must be signed in to change notification settings - Fork 6
DDD sample
Let's see how to model a simplified cargo insurance policy management system using CoreDdd. The insured cargo will be carried on ships and trucks, and there can be many ships and trucks insured within one policy. There will be following aggregate root domain entities (=aggregate roots):
- Policy - represents an insurance policy. One policy can have many policy items, where a policy item can represent a ship cargo (ShipCargoPolicyItem) or a truck cargo (TruckCargoPolicyItem)
- PolicyHolder - represents a policy holder
- Ship - represents a ship
- Truck - represents a truck
There will be following non-aggregate root domain entities (=entities):
- PolicyItem - represents an insured item on the policy
- CargoPolicyItem - represents a policy item related to cargo insurance.
- ShipCargoPolicyItem - represents an insured cargo carried on a particular ship
- TruckCargoPolicyItem - represents an insured cargo carried on a particular truck
The policy will have a collection of policy items. CargoPolicyItem
will be derived from PolicyItem
, and ShipCargoPolicyItem
and TruckCargoPolicyItem
will be derived from CargoPolicyItem
. This model will allow Policy
to support not just a cargo insurance business, but other types of insurance businesses as well, for instance a car or a van insurance support could be added by deriving VehiclePolicyItem
from PolicyItem
, and deriving CarPolicyItem
and VanPolicyItem
from VehiclePolicyItem
.
There will be several commands to manage the system:
- CreateNewShipCommand - creates a new ship
- CreateNewTruckCommand - creates a new truck
- CreateNewPolicyHolderCommand - creates a new policy holder
- CreateNewPolicyCommand - creates a new policy
- AddShipToPolicyCommand - adds a ship to a policy
- AddTruckToPolicyCommand - adds a truck to a policy
The hypothetical application would have several screens to create ships, trucks and policy holders, sending respective commands, and a screen to create a policy, and screens to add ships and trucks to the policy, again sending respective commands. These screens will be populated by data queried over persisted entities - ships, trucks, policy holders and policies. There might be following queries in the system:
- GetPoliciesByTermsQuery - search policies by terms (using PolicyDto database view)
- GetShipCargoPolicyItemsByShipNameQuery - search for ships insured by an existing policy (using ShipCargoPolicyItemDto database view)
The DDD sample source code is available here. It uses couple of controller classes (similar to MVC/Web API controllers) (ShipController, PolicyHolderController, PolicyController) to manage ships, policy holders and policies. It first persists entities via aforementioned controllers which use commands to create or update aggregate root domain entities, and then queries data over the persisted entities.