https://github.com/mideo/service-locator-example
https://github.com/mideo/service-locator-example
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mideo/service-locator-example
- Owner: MideO
- Created: 2023-12-01T09:02:00.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-08T21:00:15.000Z (about 2 years ago)
- Last Synced: 2025-04-08T00:59:04.516Z (about 1 year ago)
- Language: Scala
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A simple service locator pattern with all defined services detected via reflection
# Usage:
See [Main.scala](src%2Fmain%2Fscala%2FMain.scala)
```scala
// Define an abstract service
abstract class Service {
def run(string: String): String
}
// Implement the service
package com.github.mideo.example
class TestService extends Service {
override def run(string: String): String = s"test $string"
}
// Create a locator see implementation in src/main/scala/com/github/mideo/example/ServiceLocator.scala
val locator = new ServiceLocator[Service]("com.github.mideo.example")
// execute
locator.get("test").run("test") should be("test test")
// execute with dsl
implicit val locator = new ServiceLocator[Service]("com.github.mideo.example")
import com.github.mideo.example.ServiceLocator.ServiceLocatorSyntax._
"test".service.run("test") should be("test test")
//NB: All new implementations of Service will be registered on initialisation of the ServiceLocator
```