IoC/DI/ServiceLocator: Creating Services with sap.ui.core.service.Service
Nobody has claimed this yet.
Assessment
This issue has not been assessed yet.
Description
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:
- 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:
- The whole setup seems to be not mature enough to be used: E.g.
- Having a Service, that uses
getService()in itsinitmethod to get another Service as a dependency does not work well, because the Runtime does not wait for this secondgetService()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.
- Having a Service, that uses
- (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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from UI5/openui5
-
bug documentation in progress
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
bug in progress
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug in progress
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
sap.m.Breadcrumbs current location renders as focusable <span> without a role, failing WCAG 4.1.2 Openbug in progress
-
author action enhancement
Similar issues
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100