-1

I was talking with my boss recently about composing services from other services (in a Service-Oriented Architecture, if you haven't already guessed). We disagreed on some architectural principles and I'd like to find some articles to read around the subject to help the discussion.

My thesis is that injecting services into other services is fine. For example, I have no issues with doing the following:

class SomeService {

    protected $anotherService;

    public function __construct(AnotherService $anotherService)
    {
        $this->anotherService = $anotherService;
    }

    public function doSomething()
    {
        ...
        $this->anotherService->getWhatever();
        ...

        return $someObject;
    }
}

Some colleagues (and my boss) seem to think that this is not a good practice. Their hard rule is that a service cannot depend on another service, only a repository (or something in the architectural layer below it). The ramifications for this is that we have a lot of duplicate code, but they claim that something feels unclean about composing services from other services.

Are there some articles, books etc. out there that would be useful to consider?

1

2 Answers 2

1

Based on your problem description I think the problem is elsewhere.

If your services need to be composed in the way you describe it sounds like they don't have clear responsibility boundaries.

You may find this problem goes away if each service has only a single responsibility and the layer above them is responsible for composing them into more business-meaningful functionalities.

0

You are both wrong. Services may be composed from other services, but not in a depdendant order. Basicly there is no differance between a service, component or class. Just the way you 'link' things is different.

  1. Abstract services and only use those abstractions within other Services.
  2. Use SoC on Service Level and bind using (e.g.) a ServiceLocator
4
  • The danger in not having guidelines of this nature at all is that it's possible to build an architecture which contains dependency loops. Then, starting it up (e.g. after disaster recovery) gets difficult. Commented Mar 2, 2017 at 21:59
  • Regarding #1, are you saying use inheritance for shared logic? And moving that logic into the parent class of both A and B? Is it possible to mitigate that by using interfaces?
    – Eric Keyte
    Commented Mar 2, 2017 at 22:07
  • No! FCoI! Do not inherit, but abstract -> interfaces. Have a class/service (which is basicly the same), but don't talk to it in a concrete manner. Use an interface/abstract_class/whatever_your_language_is_capable_of; but never the concrete implemntation/type
    – Jaster
    Commented Mar 3, 2017 at 0:34
  • @Jaster totally, yes we only ever inject interfaces and then our container injects a concrete binding. So it's safe to inject InterfaceB into ClassA, then?
    – Eric Keyte
    Commented Mar 3, 2017 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.