Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved doctrine knowledge into a manager class
The controller didn't need to know about the implementation details of doctrine, it only needs to know that our application can provide some blog posts objects. Therefore we introduce a "Manager" class whose job it is to do that.
- Loading branch information
Showing
3 changed files
with
20 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Peterjmit\BlogBundle\Model; | ||
|
||
interface BlogManagerInterface | ||
{ | ||
function findAll(); | ||
function find($id); | ||
} |
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's very good stuff! Great that you put that into words, in a clear and concrete example!
However, I would have implemented the interface in this commit, so that you can have a working controller at the end :)
I know that following DDD would make you create a semantic interface, but https://github.com/doctrine/common/blob/master/lib/Doctrine/Common/Persistence/ObjectRepository.php is already a good fit.
What about making the repository a service ?
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even more specifically - use concrete
BlogRepository
.4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right.
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you chaps are right, the example would be simpler/would benefit from just injecting the repository rather than
BlogManagerInterface
.Its more habit rather than anything because I've always found the way FOSUserBundle had managers for the entities with the repository injected useful.
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterjmit just don't forget that interfaces though are cleanest, are not the only way to define public communication protocol. Classes public API forms communication protocols as well. And yes, interfaces are the purest form of protocol. Question actually is do you need this purest protocol in that particular case ;)
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterjmit how to inject BlogManagerInterface in the constructor of BlogController, for the templating is easy, because is already a service with an id.
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@silviuvoicu I'll do a follow up with some concrete code, but you would need to both write a concrete implementation of
BlogManager
define it as a service, define your controller as a service then pass the manager service as an argument4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@peterjmit Thanks.
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess when you say "Classes public API " you talking about adjusting rather the uniquitous language (DDD related) so changing the method names on our models, that is defining a domain language I guess ...? or what exactly if i am totally lost 👶 ?
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, he means the public methods defined for a class. They are, by definition, accessible, and "open to receive messages". Otherwise, they would be protected.
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the prescription Dr. 👶
4a87e1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://www.craftitonline.com/2013/09/phpspec-practices-with-symfony2-controllers-part-vi/