IoC/DI/ServiceLocator: Creating Services with sap.ui.core.service.Service

Open
#2,259 4 comments 1 reaction 1 assignee View on GitHub

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

enhancement in progress

Hi UI5 team,

I'm trying to develop an application with UI5. As it is a bit more complex than just small webapps, I'd like to use some sort of Inversion of Control or Dependency Injection to improve testability. While reading the documentation, I found the method Component.getService() to be exactly what I need here: https://sapui5.hana.ondemand.com/1.58.4/#/api/sap.ui.core.Component/methods/getService

However, except for this short page on getService, no further documentation seems to be available and all links into the SDK (e.g. to sap.ui.core.service.Service) seem to be broken.

I had a look into the sourcecode, where comments provide some guidance on how to implement a Service. However, there are a few issues:

  1. The examples in the JSdoc in the Code do not seem to work as described. I managed to build my way around this issue, but:
  2. The whole setup seems to be not mature enough to be used: E.g.
    • Having a Service, that uses getService() in its init method to get another Service as a dependency does not work well, because the Runtime does not wait for this second getService() to be finished.
    • This does not seem to help with testability, as I can't test my Services without always starting the whole Component (which acts as ServiceLocator needed everywhere)
    • All in all, always needing to implement a ServiceFactory that basically does nothing seems like a big overhead to me. Also, despite giving the full name of the factory class in the Manifest, I still need to load the factories in my Component, which also seems like something the Runtime could do automatically (and lazy!) for me.
  3. (this is what I saw at the end:) Services seem to be marked as private.

Why is such a useful feature (at least in theory) not available publicly? Would it be possible to have it public (with some improvements, maybe, but the general approach seems really fine to me)?

If its not intended to be used to implement my own Services, maybe the documentation should be changed for now, as it heavily implies that I can just register my own ServiceFactory in the Manifest, which will then be loaded automatically and I'm good to go.

TL;DR: I saw that a ServiceLocator for Inversion of Control is already available in the code and seems to be just a few steps away from being not only usable but really useful. Why can't we make this public?

Thank you for your help! Kind regards
Lukas

(ServiceLocator as described by Martin Fowler: https://martinfowler.com/articles/injection.html#UsingAServiceLocator)

Dominant language
JavaScript
Stars
3.3k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from UI5/openui5

All issues in UI5/openui5

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.