Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primitive types only in command #14

Open
webdevilopers opened this issue Jun 3, 2016 · 13 comments
Open

Primitive types only in command #14

webdevilopers opened this issue Jun 3, 2016 · 13 comments

Comments

@webdevilopers
Copy link
Owner

webdevilopers commented Jun 3, 2016

When I started decoupling from Symfony Form and the EntityType I switched to primitive types only that would then be used on the data_class which is the Command / DTO.

In conlusion my command only uses integers (for IDs) and strings (also UUID) or arrays of these types.

I'm sometimes asked if objects e.g. value objects should be used. As I mentioned I don't use objects in a DTO at all. As @jeremyb states in his slides:

Tips #3: Command & Value Object

Il est recommandé d'utiliser des types primitifs si la Command doit être traitée en asynchrone (à cause de la serialization)
Passer des Values Objects à une Command permet de réutiliser un cadre et les validations définies

Most examples I see follow these "guidelines":
https://github.com/SimpleBus/MessageBus/blob/master/doc/command_bus.md#using-the-command-bus-an-example @matthiasnoback

Sometimes a DTO property transfers a string coming from a constant:

class ContractState
{
    const STATE_ACCEPTED        = 'contract_accepted';
    const STATE_APPROVED        = 'contract_approved';
    const STATE_QUOTE_SUBMITTED = 'quote_submitted';
    const STATE_IN_PROGRESS     = 'in_progress';
    const STATE_PENDING         = 'pending';
    const STATE_CLOSED          = 'closed';
}

People ask how to ensure (validate) that the string $contractState of the DTO is a correct string from the constants.

If you are using Symfony Form you already set the choices and a user cannot select a different choice.

When the input comes directly from the Request I guess you could add a custom Constraint to the Command that could check if the strings exists:

Since I'm using Symfony Form I prefer validating the input inside the Value Object. The handler will try to create an object from String:

ContractState::fromString($command->contractState());

The fromString method then will throw an Exception if the string was invalid.

What are your thoughts on this?

Related issues:

@webdevilopers
Copy link
Owner Author

What about dates? Should they be passed as ImmutableDateTime objects or as primitive strings?
What do you think @matthiasnoback?

@matthiasnoback
Copy link

My general rule is to have only immediately serializable data in a DTO (that is, primitive types). I know some people who add "getters" to a DTO which produce actual (domain) objects based on those primitive values. I don't do it like that, but always explicitly convert to objects inside the command handler.

@webdevilopers
Copy link
Owner Author

Agree! But do you even handle dates as strings instead of DateTimeImmutable?

@mablae
Copy link

mablae commented Jun 19, 2016

Having them always serializable is good for offloading to external queue.

If serializing with an serializer like symfony/serializer or jms' it should be fine to even use object types as properties since they would be denormalized as expected. DateTime would be no problem then.

@matthiasnoback
Copy link

Even though I'm often not actually serializing DTO's, I like them to be very flat, since that's in the spirit of a DTO. Any type of object would not be able to cross application boundaries (within the application it would probably cause incorrect coupling and to the world outside it would be completely impossible ;)).

@webdevilopers
Copy link
Owner Author

webdevilopers commented Jun 21, 2016

Thanks for your opinion!

Currently I'm using DateTime objects in my DTO. But I will switch to a string yyyy-mm-dd transfer.

For now most of my commands are populated by Symfony Forms (e.g. via LexikFormFilterBundle by @lexik) using the data_class.
But soon we will have primitive request parameters e.g. JSON format. Not sure if the UI would actually pass a Javascript Date object. But most of the times dates come from datepickers that return the desired string.

Converting the date strings to DateTime objects in my handler instead will make my commands flexible for any format resp. "application boundaries".

@mablae
Copy link

mablae commented Jun 25, 2016

@webdevilopers DateTime::ATOM can be parsed by php and js natively.

@webdevilopers
Copy link
Owner Author

webdevilopers commented Feb 3, 2019

Even though I'm often not actually serializing DTO's, I like them to be very flat, since that's in the spirit of a DTO. Any type of object would not be able to cross application boundaries (within the application it would probably cause incorrect coupling and to the world outside it would be completely impossible ;)).

I've been using Commands w/ value objects form a long time. But now that we are working with DTOs for read models from projections in a DDD CQRS environment I'm thinking about switching back again.

Recently there is discussion going on wether Commands (and Handlers) belong to the Domain Layer too:
#32

Currently having Commands using Value Objects would even support this Credo.
But moving back to a DTO with primitive would clearly separate it from the Domain Layer keeping it in the Application Layer.

And that makes it easier for our UI team to identify tasks:

The reason for this is that our UI Tasks are reflected directly by the commands. That makes it easier to talt to our UI developers by "keeping them away" from our Domain Layer. ;)
Our handler than "converts" the UI Task into our "Domain intention":
PlaceOrder(Command) -> PlaceOrderHandler -> Order::place($command->orderId())

In the end the Application Layer somehow has to be aware of the Domain Layer. At least when transforming the primitive types to Value Objects and then pass them to the Domain Model.

Though I think "Actor Models" also directly accept the Commands inside the Model and then transform them there. The Application Layer and orchestration is "skipped".

Any thoughts about that?

Similar discussion:

@webdevilopers
Copy link
Owner Author

Another advantage of moving the validation of creating object to the Handler would be the improvement of catching errors in a single place e.g.:
#4

@matthiasnoback
Copy link

I'm using commands and view models which have primitive types and won't throw any exceptions. I think this is a useful aspect of them. For commands it's very useful because now (if you use Symfony) the Form component can populate it, and validate it afterwards. Then if you pass the command to the application service, it will use the raw data from the command to construct value objects and entities. So any violations of domain invariants will appear in the application service only.

@webdevilopers
Copy link
Owner Author

That is exactly the approach I would like to use @matthiasnoback in the future.
Besides the validation it will also make it easier to work with my team since some members ca focus and designing the Controllers in a different Layer / Namespace maybe.

Thanks for the feedback!

@matthiasnoback
Copy link

Cool, you're welcome.

@webdevilopers
Copy link
Owner Author

webdevilopers commented Feb 28, 2020

We are currently decoupling our UI from our (mostly Symfony w/ Forms) applications. Now we no longer separate between a "CommandDTO" and the actual "Domain Command". The latter is sometimes directly passed to an aggregate root.

This solution by @vkhorikov works fine for us:

  1. A controller receives a ChangedEmailDto from the external client. Because that’s a DTO, meaning that its sole purpose is to transfer data from one application to another, it consists of primitive types only.

  2. The controller transforms the DTO into a command by converting some of the primitives into Value Objects (string email into Email email in the above example, where Email is a Value Object) and passes that command to a command handler (the application services layer).

  3. The command handler executes the command by coordinating the work between domain classes (including the already dehydrated Email Value Object).

  4. The domain model generates domain events. Domain events may also contain value objects — for the same reasons commands do, since both commands and domain events reside at the same level of abstraction in the onion architecture.

  5. The app services layer converts the domain events into messages on the message bus. Those messages are plain DTOs too because their sole purpose is to communicate the email change to other systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants