That's an interesting concern. I personally work with simple assertions that are quick to visually check if they are valid or not, but even in the more complex cases I tend to think they are tested as a by-product of the tests that are calling them. (Basically if I change an assertion I should probably see something failing somewhere.)
It could very well make sense to verify that the most complex of assertions are actually working as intended. But then, should we also check the assertions that are inside each individual test (and not bundled together in this kind of "assertables")? I personally think the old "comment the production code and see the test fail" should cover most of the cases in which you're modifying an assertion.
Yes, that's a nice example! There are a few differences (basically having a single WebAssert vs one per each concept -AssertableUser, AssertableSession, etc.- and that by convention I prefer to start with assert...), but it's the closest thing I've seen so far.
Do you think it'd be useful to change the main article to look like this draft? In case of a positive answer, will it be removed from /r/PHP for being an article written in another language?
I was worried about some people not being able to follow the examples by using a specific language they didn't understand, but the same could be said about a fictional generic language like the one I used.
Though if I write them in, say, Java, some people will have even more reasons to complain about this article being posted in /r/PHP (something I want to avoid; the actual language is irrelevant in this post).
As for examples in multiple languages, I fear that the post sees its number of lines explode for little gain (the differences between languages would boil down to language constructs, and they'd end up being very similar).
I'll think about your suggestions, I'll see if I can improve the readability. Thanks!
In this case, would you prefer to have one post with examples in PHP and another article written in some major language (or even one per language)? Or just have one single article written in a well-known language (probably not PHP)?
I started writing this post with examples in PHP, but I soon realized that the concepts were language-independent and that by switching to a fake generic languages I could focus on the concept rather than the implementation.
Given that this a place I know better and is much smaller than /r/programming, I've prefered to get a first feedback from /r/php before sending it to a bigger subreddit.
I haven't seen any of the links you posted, but AFAIK the correct place to put these commands is the application layer. The application service acts as a "translator" between external use cases (for ex., RegisterUserCommand) and your domain model. This way your domain model is kept "pure" and unaware of external clients.
Edit: just wanted to add, RegisterUserCommand seems like an standard user management context, which probably is not worth investing time for. If it's for learning purposes, sure, go for it, but in a normal application you should probably use third-party package to solve this case. It's important to be sensible about where to focus your efforts, as your (time/human) resources are limited.
We have a similar system (we also support different types of payment, including manually-validated bank accounts), so knowing that it works in your use-case gives me confidence in this approach. I'll study these changes and also try to add "fulfillment" to our vocabulary! Thanks for your help :)
Thanks for the input (and your time taken to answer). I'd like to add the expected flow of the process, in case it adds some light on what I want to achieve and why I "need" the SubscriptionRequest to never be created if it cannot be created on PayPal:
StoreOwner attempts to add a PayPal subscription -> A SubscriptionRequest is created locally and on PayPal -> StoreOwner is redirected to URL provided by PayPal (linked to newly created subscription request) -> SubscriptionRequest status is updated depending on response from PayPal -> If StoreOwner has validated the subscription, a Subscription is created
Thinking about your proposed approaches, I think the possibility of creating the SubscriptionRequest and then marking it as invalid if it cannot be validated on PayPal could be a solution. The problem is that right now I'm not exactly using a command rather than an "action" (don't know how to call it, I'm referring to a command that returns something), and I'm doing it synchronously. If I called a true command and expected it to be executed asynchronously, the client (the controller in this case) should perform queries until the newly created SubscriptionRequest is updated with PayPal data, and then redirect if successful or show an error message if it failed. Is that right?
Right now, if my "action" fails, it throws an exception that gives immediate feedback to the controller (it expects a redirection URL in order to immediately redirect the user). It makes the controller simpler, but forces me to add the PayPal calls inside a domain service (implemented in an adapter, PaypalSubscriptionService in this case). I don't have enough experience on these scenarios to fully understand the trade-offs...
In my case, if the subscription creation fails on Paypal (due to bad credentials, invalid parameters, etc.) I wouldn't want the subscription to be created. Would that work with what you're saying?
If I understand correctly, you're suggesting to modify the application service to work as following:
final class CommandHandler
{
...
public function execute(RequestNewSubscriptionCommand $command)
{
$storeOwner = new StoreOwner();
$subscriptionRequest = $storeOwner->requestNewSubscription();
$this->paypalGateway->createSubscription($subscriptionRequest);
$this->requestRepository->persist($subscriptionRequest);
}
}
The what should be extracted from the code itself. In this case, just put the code inside a method enforceRange (as others have said) and use whatever implementation is faster to write.
It's not throwing the exception, but it isn't catching it either, so it's passing the responsibility to handle it to its caller and therefore should be documented.
I'd probably create a factory to delegate the invoice creation from your domain model, at least from a testing point of view. I would be uncomfortable working with such a large handler, knowing that you have already stripped some lines from your example. Note:I prefer command handlers so delegate as much as possible, this is just a personal preference and not a rule.
Some other comments (take them with a grain of salt):
Invoice probably doesn't need whole User or Store instances, it probably just needs its ID. Pass only what you need. (If you need billing data, you should abstract that info out of these classes.)
Not having a customer, an author and a store might leave the invoice in an invalid state. If they are required, mark them as such in the Invoice constructor. (If they are not, ignore this.)
Currency could be extracted from each item's price, depending on what kind of usage you are giving to your invoices.
As others have said, you shouldn't be telling the invoice what's the total/subtotal, that's his job.
You should probably raise the event at the end of Invoice's constructor, but right now it would be meaningless as it lacks useful information. You could think about making the constructor ask for all required info, and create a builder to prepare everything in an easy way.
You should add docblocks to your properties to gain IDE autocompletion.
You should name your properties and variables to define what they are. Is $customers a Customer collection? No, it's a $customerRepository.
Hi! I've too been unable to find such information, in fact your package is still our best resource so far. I was thinking about downloading all postcodes from geonames.org and deriving validating regular expressions from that data, but as you say, the few seconds we gain from a simplified form exceed the effort required to do that.
At the moment we have discarded the idea of doing this validation and will require visitors to select a predefined subdivision for their country. Hope we can solve this in the future though!
We have found postal code validators and datasets with whole postal codes for every region, but we are struggling to find patterns like the ones in the example even looking one country at a time :(
Thank you for the link. I've been browsing around, but unfortunately they don't offer the data we need (postal code patterns to match to specific country subdivisions) :(
That's an interesting concern. I personally work with simple assertions that are quick to visually check if they are valid or not, but even in the more complex cases I tend to think they are tested as a by-product of the tests that are calling them. (Basically if I change an assertion I should probably see something failing somewhere.)
It could very well make sense to verify that the most complex of assertions are actually working as intended. But then, should we also check the assertions that are inside each individual test (and not bundled together in this kind of "assertables")? I personally think the old "comment the production code and see the test fail" should cover most of the cases in which you're modifying an assertion.
Yes, that's a nice example! There are a few differences (basically having a single
WebAssertvs one per each concept -AssertableUser,AssertableSession, etc.- and that by convention I prefer to start withassert...), but it's the closest thing I've seen so far.Thanks for the link!
Good, thanks for your feedback! I'll ask other people to see if they share you sentiment, in order to make the swap.
Yes, that's why I find it surprising that no one I've talked to uses this approach.
BTW, I'd find it helpful if you had a link at hand (in Github for example) of a codebase that uses this "pattern".
I've created the following draft with the examples in Java: https://medium.com/p/f82f4accf266
Do you think it'd be useful to change the main article to look like this draft? In case of a positive answer, will it be removed from /r/PHP for being an article written in another language?
I was worried about some people not being able to follow the examples by using a specific language they didn't understand, but the same could be said about a fictional generic language like the one I used.
Though if I write them in, say, Java, some people will have even more reasons to complain about this article being posted in /r/PHP (something I want to avoid; the actual language is irrelevant in this post).
As for examples in multiple languages, I fear that the post sees its number of lines explode for little gain (the differences between languages would boil down to language constructs, and they'd end up being very similar).
I'll think about your suggestions, I'll see if I can improve the readability. Thanks!
Thanks for your input.
In this case, would you prefer to have one post with examples in PHP and another article written in some major language (or even one per language)? Or just have one single article written in a well-known language (probably not PHP)?
I started writing this post with examples in PHP, but I soon realized that the concepts were language-independent and that by switching to a fake generic languages I could focus on the concept rather than the implementation.
Given that this a place I know better and is much smaller than /r/programming, I've prefered to get a first feedback from /r/php before sending it to a bigger subreddit.
I haven't seen any of the links you posted, but AFAIK the correct place to put these commands is the application layer. The application service acts as a "translator" between external use cases (for ex.,
RegisterUserCommand) and your domain model. This way your domain model is kept "pure" and unaware of external clients.Edit: just wanted to add,
RegisterUserCommandseems like an standard user management context, which probably is not worth investing time for. If it's for learning purposes, sure, go for it, but in a normal application you should probably use third-party package to solve this case. It's important to be sensible about where to focus your efforts, as your (time/human) resources are limited.We have a similar system (we also support different types of payment, including manually-validated bank accounts), so knowing that it works in your use-case gives me confidence in this approach. I'll study these changes and also try to add "fulfillment" to our vocabulary! Thanks for your help :)
Thanks for the input (and your time taken to answer). I'd like to add the expected flow of the process, in case it adds some light on what I want to achieve and why I "need" the
SubscriptionRequestto never be created if it cannot be created on PayPal:StoreOwner attempts to add a PayPal subscription->A SubscriptionRequest is created locally and on PayPal->StoreOwner is redirected to URL provided by PayPal (linked to newly created subscription request)->SubscriptionRequest status is updated depending on response from PayPal->If StoreOwner has validated the subscription, a Subscription is createdThinking about your proposed approaches, I think the possibility of creating the
SubscriptionRequestand then marking it asinvalidif it cannot be validated on PayPal could be a solution. The problem is that right now I'm not exactly using a command rather than an "action" (don't know how to call it, I'm referring to a command that returns something), and I'm doing it synchronously. If I called a true command and expected it to be executed asynchronously, the client (the controller in this case) should perform queries until the newly createdSubscriptionRequestis updated with PayPal data, and then redirect if successful or show an error message if it failed. Is that right?Right now, if my "action" fails, it throws an exception that gives immediate feedback to the controller (it expects a redirection URL in order to immediately redirect the user). It makes the controller simpler, but forces me to add the PayPal calls inside a domain service (implemented in an adapter,
PaypalSubscriptionServicein this case). I don't have enough experience on these scenarios to fully understand the trade-offs...In my case, if the subscription creation fails on Paypal (due to bad credentials, invalid parameters, etc.) I wouldn't want the subscription to be created. Would that work with what you're saying?
If I understand correctly, you're suggesting to modify the application service to work as following:
I'd personally go with
fn.Which one do you prefer?
I think they behaved slightly different, and they preferred to keep the syntax different so Hack could support both versions at the same time.
Having the following code:
Would you feel the need to add an explanation?
"Don't comment the _what_, comment the _why_."The what should be extracted from the code itself. In this case, just put the code inside a method
enforceRange(as others have said) and use whatever implementation is faster to write.Here you are: https://youtrack.jetbrains.com/issue/WI-28646
Thanks for the comments. I've ended up creating a ticket, as it seems that nobody has faced this problem (or at least hasn't complained about it).
Yes, the problem is that it doesn't (apparently) work with interfaces like the one in the example.
It's not throwing the exception, but it isn't catching it either, so it's passing the responsibility to handle it to its caller and therefore should be documented.
Edit: mobile typos
I'd probably create a factory to delegate the invoice creation from your domain model, at least from a testing point of view. I would be uncomfortable working with such a large handler, knowing that you have already stripped some lines from your example. Note: I prefer command handlers so delegate as much as possible, this is just a personal preference and not a rule.
Some other comments (take them with a grain of salt):
Invoiceprobably doesn't need wholeUserorStoreinstances, it probably just needs its ID. Pass only what you need. (If you need billing data, you should abstract that info out of these classes.)Invoice's constructor, but right now it would be meaningless as it lacks useful information. You could think about making the constructor ask for all required info, and create a builder to prepare everything in an easy way.$customersaCustomercollection? No, it's a$customerRepository.Hi! I've too been unable to find such information, in fact your package is still our best resource so far. I was thinking about downloading all postcodes from geonames.org and deriving validating regular expressions from that data, but as you say, the few seconds we gain from a simplified form exceed the effort required to do that.
At the moment we have discarded the idea of doing this validation and will require visitors to select a predefined subdivision for their country. Hope we can solve this in the future though!
We have found postal code validators and datasets with whole postal codes for every region, but we are struggling to find patterns like the ones in the example even looking one country at a time :(
Thank you for the link. I've been browsing around, but unfortunately they don't offer the data we need (postal code patterns to match to specific country subdivisions) :(