PHP and C#.NET MVC ( repository pattern in PHP, how might i go about it ) -
i love unity , ninject framework dependency injection c#.net controllers repository interfaces etc, trying think alternative in php , struggling.
i have:
class user { //.. } interface iuserrepository { public function repository(); // can't work variable, should abstract? } class userrepository implements iuserrepository { public function repository() { $users = // dao gets users , returns them.. // how iuserrepository me @ all? if had ninject or // unity creating new link between userrepo , iuserrepo? return $users; } public function getallusers() { // errr.. i'm confused } } // has if admincontroller called, // inject construct, new iuserrepository class admincontroller extends controller { private $repository; public function __construct( $repository ) { $this->repository = $repository; } public function actionresult_listusers() { $users = $repository->getallusers(); // clever view method thing $users model } }
the real question php , repository approach. how work? can see i'm blotching bit , want right!
edit::
even more simple question. benefit of interface keyword? how declaring interface methods undefined , making new class implements methods extend them, when cannot create new interface class filled in proper class defines them?
kind of broad question i'll take shot. symfony 2 modern php framework built around excellent dependency injection container. symfony 2 supports doctrine 2 modern orm persistence layer in repositories supported.
skim through here: http://symfony.com/doc/current/book/doctrine.html
skip details on setting things , instead for:
creating entity class
persisting objects database
fetching objects database
the fetching section repositories come play. gives broad overview of 1 way implement repositories on php. might decide research more on symfony 2/doctrine 2 or may go route entirely. give starting point.
as far interfaces go, hate real real real basic question. might want go through oop tutorials? 1 or 2 sentence explanation not going make sense.
====================================================
this may bit:
class user { //.. } interface iuserrepository { public function getallusers(); } class userrepository extends doctrinerepository implements iuserrepository { public function getallusers() { return $this->findall(); } } class admincontroller extends controller { private $repository; public function __construct(iuserrepository $repository ) { $this->repository = $repository; } public function actionresult_listusers() { $users = $this->repository->getallusers(); // clever view method thing $users model }
your user , iuserrepository reside in known domain layer.
the actual userrepository implementation in service layer. might have multiple implementations. 1 talking sql database. perhaps talking mongodb or maybe file.
you controller lives in yet layer. expects receive object implements iuserrepository , has getallusers method. controller not care how repository implemented. knows exposed via interface.
it's application wire things using dependency injection such controller gets correct repository.
Comments
Post a Comment