Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:54:46+00:00 2026-05-28T22:54:46+00:00

I have a MVC application with a Domain Model well defined, with entities, repositories

  • 0

I have a MVC application with a Domain Model well defined, with entities, repositories and a service layer.

To avoid having to instantiate my service classes inside my controllers, and thus, mess my controllers with logic that does not suit they, I created a helper that acts as a sort of Service Locator, but after reading a bit, I realized that many devs:

  • http://blog.tfnico.com/2011/04/dreaded-service-locator-pattern.html
  • http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx
  • http://underground.infovark.com/2010/06/18/the-service-locator-pattern-is-the-new-global-variable/
  • http://www.andyfrench.info/2011/05/service-locator-anti-pattern_17.html

Say that the Service Locator is actually an anti-pattern. But I think my implementation is not an anti-pattern.

The reason they consider the Service Locator an anti-pattern, is because it hide dependencies, however, I inject the only dependency (the Entity Manager, and this dependency probably will not change, because it is in the signature of the Service interface) required by a service class, at the time I instantiate the Service Locator.

Here is my code:

<?php

namespace App\Controller\Action\Helper;
use Zend_Controller_Action_Helper_Abstract as Helper,
    Doctrine\ORM\EntityManager;

/**
 * Service Locator Helper
 * @author JCM
 */
class Service extends Helper {

    /**
     * The actual EntityManager
     * @var \Doctrine\ORM\EntityManager
     */
    private $entityManager;

    /**
     * Services Namespace
     * @var string
     */
    private $ns;

    /**
     * @param EntityManager $entityManager
     * @param string $ns The namespace where to find the services
     */
    public function __construct( EntityManager $entityManager, $ns )
    {
        $this->entityManager = $entityManager;
        $this->ns = $ns;
    }

    /**
     * @param string $serviceName
     * @param array $options
     * @param string $ns
     */
    public function direct( $serviceName, array $options = array(), $ns = null )
    {
        $ns = ( (!$ns) ? $this->ns : $ns ) . '\\';
        $class = $ns . $serviceName;

        return new $class( $this->entityManager, $options );
    }

    /**
     * @param EntityManager $entityManager
     */
    public function setEntityManager( EntityManager $entityManager )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @return \Doctrine\ORM\EntityManager
     */
    public function getEntityManager()
    {
        return $this->entityManager;
    }

    /**
     * @param string $name
     */
    public function __get( $name )
    {
        return $this->direct( $name );
    }
}

Registering the Action Helper with the Front Controller:

//inside some method in the bootstrap
HelperBroker::addHelper( new App\Controller\Action\Helper\Service( $entityManager, '\App\Domain\Service' ) );

And how I use this Helper in my controllers:

//Some Controller
$myService = $this->_helper->service( 'MyService' ); //returns an instance of the class App\Domain\Service\MyService
$result = $myService->doSomethingWithSomeData( $this->getRequest()->getPost() );
//etc...
  • My implementation is correct?
  • It really is an anti-pattern?
  • What are the possible problems that I might face?
  • How can I refactor my code to eliminate this anti-pattern, but continue with the functionality?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:54:46+00:00Added an answer on May 28, 2026 at 10:54 pm

    I don’t think that what you’ve built does implement the service locator pattern. If you had so, there would be a global “registry” somewhere.

    What I see is basically a factory class (App\Controller\Action\Helper\Service) with dependencies, which are injected via the ctor. So your class does not know where its dependencies came from and it’s also not responsible for creating them (which is a good thing!).

    Correct me if I’m wrong. 🙂

    BTW: That’s also the reason why you should not pass around your dependency injection container. It turns into a service locator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring MVC application trying to use a rich domain model, with
I have an MVC2 n-tier application (DAL, Domain, Service, MVC web) using a DDD
I have three projects in my solution: Domain layer GUI MVC application Now my
I have an MVC application. The model has a property that is a struct
Suppose you have an application that utilizes the domain-model pattern, DDD and lots of
I'm creating ViewModels in my MVC application. We are using automapper for domain model
I have MVC webApi application which works with Unity . I have to resolve
I have an MVC application view that is generating quite a large HTML table
I have an MVC application which is my marketing website, and two other regular
I have an MVC application which has a set of fields for contact details

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.