Skip to main content

Questions tagged [service-locator]

The service locator is a design pattern that aims at decoupling the use of an abstract services from concrete implementations of these services. This pattern encapsulates the determination of the concrete implementation to use and implements the principle of dependency inversion.

Filter by
Sorted by
Tagged with
1 vote
2 answers
220 views

It is okay to create an interface of interfaces?

Let me first describe the situation I have a component that uses dependency injection through a service locator. In the first scenario (image 1) the component needs a class that implements Interface0, ...
DanielJaramillo's user avatar
0 votes
0 answers
63 views

We are aware of Service Discovery- is "Object" Discovery a thing?

Context My office is currently working on distributed (micro) services, as part of this we obviously have Service Discovery between these services so that they are aware of each others locations (when ...
jmcgrory's user avatar
  • 109
4 votes
1 answer
944 views

Dependency Injection: What are advantages of using a framework? [duplicate]

Introduction and Question I understand what the advantages of dependency injection, e.g. constructor injection or setter injection and that it is one way of doing inversion of control. I also ...
Make42's user avatar
  • 307
5 votes
4 answers
1k views

Why are Service Locator frameworks often called Dependency Injection Containers?

First, here's how I understand these two concepts: Service Locators are not Dependency Injection. Both Service Locators and Dependency Injection are applications of Inversion of Control. This is the ...
Snail Cadet's user avatar
1 vote
1 answer
214 views

Alternatives to service locator with opaque pointer in C

I have a project in which one module keeps the state of the target device (things like current command level, but mostly status registers caches). I'm aware that having a global public variable (...
Martel's user avatar
  • 615
1 vote
2 answers
1k views

How to configure and setup an IOC Container for a library?

When using Dependency Injection across internal classes (implementation details) in a library which is to be consumed by multiple applications, is there a good way to configure the IOC container? ...
brent's user avatar
  • 121
0 votes
1 answer
110 views

Does this architectural design "smell" like Service Locator and can this be dealt with in a more robust manner?

I have an architecture based on a domain with multiple object types. These objects (many and of disparate types) must reside together with each other because they make up a use-case. The types are not ...
Vector Zita's user avatar
  • 2,482
122 votes
5 answers
15k views

What's the difference between using dependency injection with a container and using a service locator?

I understand that directly instantiating dependencies inside a class is considered bad practise. This makes sense as doing so tightly couples everything which in turn makes testing very hard. Almost ...
tom6025222's user avatar
  • 1,004
1 vote
1 answer
190 views

How to design a reassignable global instance that can be referenced by many other classes

I have a DeviceManager class which can handle different physical devices, and to say there are Classes A,B,C, which require a DeviceManager instance as dependency. They should always use the same ...
Haoyuan Tang's user avatar
0 votes
1 answer
371 views

What pattern should I use to implement a Message Routing mechanism?

I'm hoping someone can give some guidance on an issue I'm having. I have: A WebSocket service, where I have a single method on the server that handles all traffic. Lots of different kinds of ...
Dan Rayson's user avatar
4 votes
3 answers
888 views

Decoupled design using static interfaces

Now first I want to mention that I am not quite sure what is the term describing the issue at hand but I hope that I can illustrate it with some code. Problem: A shared class library defines an ...
Ricky Spanish's user avatar
2 votes
3 answers
123 views

Does a service-locator lead to more straightforward code in this situation?

Assume I have a class like this: public interface IService { // Gets some data, possibly from a website / database List<string> GetStrings(); } public class ItemViewModel : ...
Patrik Bak's user avatar
6 votes
3 answers
9k views

Unity injection with too many constructor parameters

The question is about choosing the appropriate design for the scenario described below. This is a repost from https://stackoverflow.com/questions/51940180/unity-injection-with-too-many-constructor-...
Konstantin Konstantinov's user avatar
2 votes
2 answers
753 views

Dependency Injection and DI Container

I have implemented clean architecture for my app, and I have a few questions. Typically, pure DI is argued for over a Service Locator pattern, because it is very explicit and more testable. However, ...
Lansana's user avatar
  • 129
2 votes
3 answers
519 views

IOC container & accessing Implementation from the container

Background As mentioned in this article, Inversion of Control can be achieved through various mechanisms such as: Strategy design pattern, Service Locator pattern(SLP), Factory pattern, and ...
user1787812's user avatar
5 votes
2 answers
15k views

How to separate the ViewModel from the Model, yet associate them with each other based on the Model?

In a wpf application, a Model object is created either from scratch or deserialized from some .xml file. To create a connection to the ViewModel, the following method (on the Model!) is used by the ...
surface's user avatar
  • 109
1 vote
1 answer
2k views

Service locator vs Dependency Injection?

I have a class which acts as a Adaptor between an HTTP request and our Application's custom domain request. So every Http request element: URI, headers, content are mapped to our customized domain ...
Siddharth Trikha's user avatar
1 vote
4 answers
1k views

Image processing - Service locator pattern & Container of dependencies

As per wiki, A dependency is an object that can be used (as a service). here is the OOP paradigm using C syntax that address 4 roles, shown below. 1) interface (handlers.h) typedef struct { ...
overexchange's user avatar
  • 2,305
4 votes
3 answers
2k views

DependencyInjection - Constructor over-injection smell vs service locator - where is the proper approach?

I am trying to improve my MVC projects and I read a few articles about DI, IoC containers, Constructor Injection and Service locators. I wanted to go with Ninject to help with the dependencies, but ...
Bartosz's user avatar
  • 597
15 votes
3 answers
439 views

Multitudes constructing one implementation. DI hopeless? Use service locator?

Say we have 1001 clients that construct their dependencies directly rather than accept injections. Refactoring the 1001 is not an option according to our boss. We're actually not even allowed access ...
candied_orange's user avatar
1 vote
0 answers
147 views

How can I load service dependencies into model classes?

My model objects are generated by the library using hard-wired new operators, which makes dependencies injection using the constructor impossible. However, they also have methods, which are called by ...
Michael Tsang's user avatar
6 votes
1 answer
247 views

How do I remove smell from a pluggable DAL

I am working with an application that is composed of several different, disconnected, components and each piece has a dependency on up to three different Data Stores (SQL Server, Doc Storage, BLOB ...
dparsons's user avatar
  • 161
6 votes
2 answers
688 views

In an enterprise architecture, where is Service Locator most appropriately used, and why?

I'm trying to implement the Service Locator pattern. In the below diagram is my references between projects (.net projects), each box represents a project. The arrows indicate the dependencies. I ...
user1653400's user avatar
2 votes
1 answer
4k views

Using ninject in a class library

Looking for some help getting my head around ninject and DI. Using the simple examples I've found online everything works nicely but trying to do something more complex is causing headaches. I have ...
Flick's user avatar
  • 23
8 votes
3 answers
2k views

Ambient dependency injection through static service locator

After some googling I found some debates about whether constructor injection or property/field injection is better, but there is yet another alternative that strikes me as more beneficial. In most ...
John's user avatar
  • 253
11 votes
2 answers
6k views

Service-locator anti-pattern alternative

I'm using Unity as IoC with C#, but I guess the question really isn't really limited to Unity and C#, but IoC in general. I try to follow the SOLID-principle, which means that I got very few ...
smoksnes's user avatar
  • 483
4 votes
2 answers
9k views

Only use a dependency injection container at the root of an application?

normally, I'll use a dependency injection container (unity) in c# like this example: class SomeClass { private readonly ILogger _logger; public SomeClass() { _logger = ...
Marcel Hoffmann's user avatar
1 vote
1 answer
124 views

How far do I, or can I take TDD tests with Service Objects?

In writing PHPSPEC tests for a Zend Framework 2 application, I'm left wondering how far to 'dig'. Consider this very simple case: A DomainService (Domain in the URL sense of the word) should be ...
Saeven's user avatar
  • 119
1 vote
4 answers
507 views

Is it ok to use service locator for ubiquitously used dependencies? [closed]

We strictly adhere to inversion of control in our codebase, but that creates hellish constructors (yes, I know that means our classes aren't cohesive enough, this is a work in progress). The thing is, ...
Ziv's user avatar
  • 3,086
0 votes
2 answers
2k views

How to avoid using service location when using dependency injection? [closed]

I am trying to use dependency injection in a WPF application, and I really can't figure how to avoid using service location, which is considered to be an anti-pattern in many articles. I am using the ...
Ondřej's user avatar
  • 111
6 votes
1 answer
2k views

Pattern for communicating between nested usercontrols and parent form (C# / Winforms)

I'm trying to decide the best way to handle communication between a UserControl that is nested within another UserControl and the parent Form itself. I have a Form, inside the form there's a ...
Adam Plocher's user avatar
2 votes
2 answers
569 views

Dependency injection of ViewModel with unmanaged parameter

I have a PersonEditViewModel which needs two other objects, the personId and the PersonRepository. PersonRepository is obtained from a service locator: private final int personId; public ...
xmedeko's user avatar
  • 186
2 votes
1 answer
4k views

How to deal with dependency when you have separate projects?

I know there is all this talk about having only one composition root that set all your dependencies and that you should always prefer constructor injection to other types, but sometimes it just doesn'...
Gudradain's user avatar
  • 454
2 votes
1 answer
2k views

Where to keep persistent services in MVVM architecture?

In an MVVM application I need to use some services that are persistent, e.g. network services like P2P and WCF where the main "engine" or server endpoint must remain up and running during the lifetime ...
DaveO's user avatar
  • 136
1 vote
1 answer
1k views

Is this a good service locator, and is this service locator pattern(?) OK?

I'm working in Zend framework 1 and was using Zend_Registry to store objects so that I could replace them in my controllers while unit testing: $auth = Zend_Registry::get('Auth'); // set in Bootstrap....
Martyn's user avatar
  • 795
1 vote
1 answer
139 views

Lookup pattern as a way of passing global references

I am looking at some legacy code in java that uses the Service Locator pattern as a way of passing global references as opposed to passing them over and over again through method arguments and I am ...
user avatar
4 votes
2 answers
1k views

IoC as service locator

I have been wondering for quite some time on some issues while using dependency injection: In a layered application, I normally inject repositories into the application service using constructor ...
Phuong Nguyen's user avatar
1 vote
1 answer
1k views

How can I use guice to replace code dependent on service locator implementation?

Consider I have a service called FileSystem, and that this FileSystem is used by various classes throughout the application. Typically, the service is acquired via some static class method ...
Jeremy's user avatar
  • 149
4 votes
2 answers
272 views

How to extend Constructor Injection?

Suppose I have MyClass with a very simple dependency. It uses constructor injection. public class MyClass : IMyClass { private IA A; public MyClass(IA a) { A = a; } } This ...
Swesus's user avatar
  • 41
5 votes
2 answers
1k views

How to decouple simple factory and default implementation?

I have a simple factory class (FileResources) with static factory methods providing a default implementation (DefaultFileResource). public final class FileResources { private FileResources() {} ...
Vorzard's user avatar
  • 51
0 votes
4 answers
997 views

API design with references to root object

[Normally I post on StackOverflow but as this is more a design/theory question rather than a code question I'll give it a shot here] Most of my applications currently use a core object model that I ...
Richard Moss's user avatar
1 vote
1 answer
578 views

How should dependencies be managed across a modular application?

Let's say that we have a structure like this: Application -- Modules --Module1 -- Controller -- PublicHelper --Module2 -- Controller ...
bear's user avatar
  • 111
5 votes
3 answers
2k views

What is a Service Locator?

I've heard the term pop all around. I've read various articles regarding the subject and heard two main definitions to the term "Service Location": A glorified Registry - Bad practice, global ...
Madara's Ghost's user avatar
0 votes
0 answers
177 views

Refactoring expansive message architecture

I am currently working with a system that acts like a message bus. A message comes in to the service (WCF Windows Service hosted). The system then uses a service locator pattern to look up which ...
disjointed's user avatar
3 votes
2 answers
1k views

Desktop application, dependency injection

I am thinking of applying a real dependency injection library to my toy C#/GTK# desktop application. I chose NInject, but I think this is irrelevant to my question. There is a database object, a main ...
liori's user avatar
  • 725
5 votes
2 answers
7k views

IoC containers and service locator pattern

I am trying to get an understanding of Inversion of Control and the dos and donts of this. Of all the articles I read, there is one by Mark Seemann (which is widely linked to in SO) which strongly ...
TheSilverBullet's user avatar
7 votes
3 answers
8k views

Inject dependency as method parameter instead of constructor parameter

I'm using an ORM which doesn't allow me to inject dependencies in the constructor. Let's say I'm using DDD for the business logic, and the MVC pattern for the UI. Now one of my domain objects needs ...
dvdvorle's user avatar
  • 849
3 votes
1 answer
2k views

Better to inject IoC container or use a Service Locator pattern? [duplicate]

I have an IoC container (If you want a specific one to look at, it's very similar to Unity) and I'm working on how I want to integrate it into my application. My overall goal for this exercise is to ...
Andrew's user avatar
  • 133