Skip to content

Commit

Permalink
Specify what happens under the hood (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk authored Jul 3, 2024
1 parent 1ae8f31 commit 9e4176f
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ The suggested approach with this library would be:
```scala
import io.github.cats_effect_simple_di.Allocator

// create a Dependencies object:
class Dependencies(allocator: Allocator) {
// create a Dependencies object and class that holds all the dependencies:
object Dependencies {
def create(runtime: IORuntime): Resource[IO, Dependencies] =
Allocator.create(runtime).map(new Dependencies(_))
}

class Dependencies private(allocator: Allocator) {
// Suppose you need to instantiate a class that returns a Resource[F, A]
// Then you can use the allocator to allocate the resource
lazy val http4sClient: Client[IO] = allocator.allocate {
Expand All @@ -35,23 +40,25 @@ class Dependencies(allocator: Allocator) {

}

// Then you can use the Dependencies object to create a Resource[F, A]
object Dependencies {
def apply(runtime: IORuntime): Resource[IO, Dependencies] =
Allocator.create(runtime).map(new Dependencies(_))
}

// Use your dependencies in the main app class
object Main extends IOApp.Simple {
override def run: IO[Unit] = {
Dependencies(runtime).use { dependencies =>
override def run: IO[Unit] =
Dependencies.create(runtime).use { dependencies =>
// use your exit dependency here
dependencies.myServer.useForever
}
}
}
```

## What happens under the hood?

* `lazy val` solves the problem that dependencies are instantiated only when they are accessed and only one instance is
created.
* `Allocator` is a wrapper around `Resource` that keeps track of the order of resource allocation and finalization. So
when application is shut down, resources are shut down in the reverse order they were initialized.
* `Dependencies` initialization is wrapped in a `Resource` so that resources are shut down when the application
finishes.

## Installation

Supported Scala versions: `3.x`
Expand Down

0 comments on commit 9e4176f

Please sign in to comment.