service-locator

Faster, better, and more efficient type hinting for PHP Storm with service locators

自闭症网瘾萝莉.ら 提交于 2019-12-03 15:06:12
问题 I have been looking for a way to do this for months. I am one of those developers that loves autocompletion. For every Service Locator call in zend framework 2 I type hint with the following: Without global hinting file /** @var \Module\Service\SuperService $superService */ $superService => $this->getServiceLocator()>get('\Module\Service\SuperService'); $superService->coolFunction(); This works, but the code can get messy when you start getting 2-4 Services in a single Controller. I am trying

Registry pattern Vs Service locator pattern Vs Dependency Injection Container

故事扮演 提交于 2019-12-03 10:49:58
问题 Is there any difference between them rather than set and get objects in an array by key? class Registry { private $container=array(); public static function Set($name,$object){ self::$container[$name]=$object; } public static function &Get($name){ return self::$container[$name]; } } 回答1: Registry Pattern Registry pattern is a pattern used to lookup an object knowing only its name. This pattern stores instances of objects internally and uses a dictionary mapping to retrieve those instances

Avoiding Service Locator Antipattern with legacy app not designed for IOC

懵懂的女人 提交于 2019-12-03 07:26:25
问题 I have read often that Service Locators in IOC are an anti-pattern. Last year we introduced IOC (Ninject specifically) to our application at work. The app is legacy, it's very big and it's fragmented. There are lots of ways a class, or a chain of classes can get created. Some are created by the web framework (which is custom), some are created by nHibernate. Lots are just scattered around in weird places. How would we handle the different scenarios and not come up with something thats not at

Testable Controllers with dependencies

╄→尐↘猪︶ㄣ 提交于 2019-12-03 02:46:33
问题 How can I resolve dependencies to a controller that is testable? How it works: A URI is routed to a Controller, a Controller may have dependencies to perform a certain task. <?php require 'vendor/autoload.php'; /* * Registry * Singleton * Tight coupling * Testable? */ $request = new Example\Http\Request(); Example\Dependency\Registry::getInstance()->set('request', $request); $controller = new Example\Controller\RegistryController(); $controller->indexAction(); /* * Service Locator * *

Spring ServiceLocator or pure factory pattern?

吃可爱长大的小学妹 提交于 2019-12-03 02:33:00
99% of my dependency is manage with DI pattern via @Autowired Spring annotation. Nevertheless in a particular scenario, I can't determine which implementation to be used until run-time. The most known case, is the multiple implementation of parsers. The first solution is to used multiple @Autowired (ugly mode) Interface Parser { <T> T parse(); } @Component("JsonParser") class JsonParser implements Parser { ... } @Component("XmlParser") class XmlParser implements Parser { ... } class MyService { @Autowired @Qualifier("XmlParser") Parser xmlParser; @Autowired @Qualifier("JsonParser") Parser

Avoiding Service Locator Antipattern with legacy app not designed for IOC

泄露秘密 提交于 2019-12-02 20:59:28
I have read often that Service Locators in IOC are an anti-pattern . Last year we introduced IOC (Ninject specifically) to our application at work. The app is legacy, it's very big and it's fragmented. There are lots of ways a class, or a chain of classes can get created. Some are created by the web framework (which is custom), some are created by nHibernate. Lots are just scattered around in weird places. How would we handle the different scenarios and not come up with something thats not at least ServiceLocatorish and not end up with different kernels in different places (scopes like

Accessing ASP.NET Core DI Container From Static Factory Class

假如想象 提交于 2019-12-02 17:30:59
I've created an ASP.NET Core MVC/WebApi site that has a RabbitMQ subscriber based off James Still's blog article Real-World PubSub Messaging with RabbitMQ . In his article he uses a static class to start the queue subscriber and define the event handler for queued events. This static method then instantiates the event handler classes via a static factory class. using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Text; namespace NST.Web.MessageProcessing { public static class MessageListener { private static IConnection _connection; private static IModel _channel;

Which pattern to use for logging? Dependency Injection or Service Locator?

本秂侑毒 提交于 2019-12-02 17:28:08
Consider this scenario. I have some business logic that now and then will be required to write to a log. interface ILogger { void Log(string stuff); } interface IDependency { string GetInfo(); } class MyBusinessObject { private IDependency _dependency; public MyBusinessObject(IDependency dependency) { _dependency = dependency; } public string DoSomething(string input) { // Process input var info = _dependency.GetInfo(); var intermediateResult = PerformInterestingStuff(input, info); if (intermediateResult== "SomethingWeNeedToLog") { // How do I get to the ILogger-interface? } var result =

Testable Controllers with dependencies

£可爱£侵袭症+ 提交于 2019-12-02 16:21:01
How can I resolve dependencies to a controller that is testable? How it works: A URI is routed to a Controller, a Controller may have dependencies to perform a certain task. <?php require 'vendor/autoload.php'; /* * Registry * Singleton * Tight coupling * Testable? */ $request = new Example\Http\Request(); Example\Dependency\Registry::getInstance()->set('request', $request); $controller = new Example\Controller\RegistryController(); $controller->indexAction(); /* * Service Locator * * Testable? Hard! * */ $request = new Example\Http\Request(); $serviceLocator = new Example\Dependency

What is the actual difference betwen a service locatior and a dependency injection?

被刻印的时光 ゝ 提交于 2019-12-02 09:43:15
I was going through the previous discussion in which there was a detailed discussion on the difference between a service locator and a dependency injector, but still I am not able to get that. Can I get a general response without any code? This code sample applies the Dependency Injection principle: public class UserService : IUserService { private IUserRepository repository; // Constructor taking dependencies public UserService(IUserRepository repository) { this.repository = repository; } } This code sample uses the Service Locator pattern: public class UserService : IUserService { private