News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Wojciech Sznapka:
Injecting repositories to service in Symfony2
October 17, 2013 @ 11:45:54

Wojciech Sznapka has an interesting new post to his site today talking about injecting repositories into services in Symfony2-based applications. By injecting just a single repository instead of the entire EntityManager, you get a cleaner, more clear interface defined in the code.

It is generally a good idea to wrap business logic into services. Often, such services methods uses doctrine's repositories to operate on data storage. Injecting whole EntityManager service is very popular approach, but it isn't the most elegant way I could think of. EntityManager works only as a factory in that case and could lead to usage of other repositories, which might end up with too many responsibilities of given service.

He includes some code to illustrate his point - both a "services.xml" configuration of the related dependency injection container and a custom entity repository (defined in the config). He then shows how this repository (FooRepository) would be injected into the service (FooService) via constructor injection.

0 comments voice your opinion now!
symfony2 repository injection configuration tutorial entity manager

Link: http://blog.sznapka.pl/injecting-repositories-to-service-in-symfony2/

blog comments powered by Disqus

Similar Posts

DevShed: Searching and Replacing Nodes with SimpleXML in PHP 5 (Part 3)

BinaryTides.com: Javascript style object literals in PHP

ServerGrove Blog: Symfony2 components overview: Templating

NetTuts.com: 30+ Awesome CodeIgniter Tutorials for all Skill Levels

Sam Hennessy's Blog: A Dependency Injection for PHP Manifesto - Part 0 (Preface)


Community Events

Don't see your event here?
Let us know!


version community voicesoftheelephpant extension interview introduction opinion application php7 performance api series podcast symfony2 testing example release framework laravel library

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework