views:

92

answers:

1

I’ve been persuaded by Eric Evans’ book and am integrating DDD into my framework. All basic elements (services, repositories, bounded contexts, etc) have been implemented and now I’m looking for feedback on how to correctly integrate this.

I have some business logic which has to be performed when an entity is created or modified. This example is a very simple one. Most business logic will become much more complex.

This business logic can be split up into the following actions:

  1. Update calculated fields;
  2. Update a child record inside the aggregate root. When creating the aggregate root this entails creating a default child record. When updating the aggregate root this entails removing the existing child record and creating a new one if a specific field on the aggregate root has changed;
  3. Propagate start and end date of the aggregate root to the start and end date of the child records inside the aggregate root. These must be kept in sync under certain circumstances;
  4. Propagate a field of the aggregate root to a different aggregate root.

My first attempt is to put all of this on the aggregate root, but I feel this is not going to work. I have the following problems integrating this logic:

  • All these actions have to be completed as a single whole and should not be made available as separate actions. This has the result that this is going to be very difficult to test (TDD);
  • I am not clear on whether any of these actions can be moved out to a service. The reason for this is that they make no sense outside of the aggregate root, but it would make TDD a lot easier;
  • Some logic changes depending on whether a new entity is created or an existing one is modified. Should I put these two branches inside the update logic or should I make two entirely different paths that share the business code that does not differentiate based create/modify.

Any help on the above issues would be greatly appreciated and other feedback in general.

+5  A: 

The algorithm you've described should remain in the aggregate root, elsewise you end up with an anemic domain model, excepting propagating a field to another aggregate root where I will describe what I think you should do later.

As far as TDD is concerned, a method with "package" access on the aggregate root (e.g. "calculate()", should coordinate the entire action, which either the service or repository object would normally call. This is what tests should exercise in conjunction with setting different combinations of instance variables. The aggregate root should expose its instance variables, the children collection, and each child should expose its instance variables, through getters - this allows tests to validate their state. In all cases if you need to hide information make these getters package or private access and use your unit testing framework to make them public for the purpose of testing.

For your testing environment consider mocking the repository objects (you're using dependency injection right?) to return hard coded values. Short of this consider using something like dbunit to work with a database in a known state.

As far as logic changes are concerned create vs. modify, are you referring to how to persist or is there an actual algorithm to consider? If the former, I would make the repository responsible, if the latter I would make two separate methods (e.g. "calculateCreate()" & "calculateUpdate()") which calculate() would delegate as appropriate.

Also, there's a concurrency issue to think about as well because it sounds as if calculated values rely on mutable fields. So either need to have careful locking or aggregate roots that can only be used by a client once at a time. This also applies to propagating a field across aggregates - I would probably use the repository for this purpose - but you need to think carefully on how this should or should not impact other clients who are using the repository object.

orangepips
Thanks for the reply. First, yes I'm using Castle Windsor and Rhino. Further, so you're saying that I should make the separate methods available at package level for TDD?
Pieter
Yes. Make package access methods that a unit test can call directly by having the unit testing package make the method public for the purpose of the test. For your application, these method(s) are called implicitly - so code using aggregateRoot wouldn't be able to call calculate() directly because it's not public. So instead service.save(aggregateRoot) - or repository.save(aggregateRoot) - calls aggregateRoot.calculate() internally.
orangepips
And concerning the create/update logic. The method that decides which business logic methods are to be called should differentiate based on create/update mode, not the business logic itself?
Pieter
In terms of persistence, if possible I would use the same path for create and update e.g. "service.save()". Inside the service, I would do what you can most easily unit test. From what you've written I think you can check for the existence of the aggregateRoot and update or insert as appropriate. For the children, it sounds as if they are dependent on the parent for their existence, in which case the easiest path may always be to delete and then insert. Granted if there are a lot of children this may prove to be too inefficient, but it's definitely the most straightforward.
orangepips
I'm using CQRS so the entity itself decides what business logic to execute; very neat idea. I think I understand the idea. Thanks very much for your help.
Pieter