trait DIResource[+F[_], Resource] extends DIResourceBase[F, Resource]

DIResource is a class that captures the effectful allocation of a resource, along with its finalizer.

This can be used to wrap expensive resources.

Example with DIResource.make:

def open(file: File): DIResource[IO, BufferedReader] =
  DIResource.make(IO { new BufferedReader(new FileReader(file)) })(in => IO { in.close() })

Example with inheritance:

final class BufferedReaderResource(file: File) extends DIResource[IO, BufferedReader] {
  val acquire = IO { new BufferedReader(new FileReader(file)) }
  def release(in: BufferedReader) = IO { in.close() }
}

Usage is done via use:

open(file1).use { in1 =>
  open(file2).use { in2 =>
    readFiles(in1, in2)
  }
}

DIResources can be combined into a larger resource via DIResourceBase.flatMap:

val res: DIResource[IO, (BufferedReader, BufferedReader)] =
  open(file1).flatMap { in1 =>
   open(file2).flatMap { in2 =>
     IO.pure(in1 -> in2)
   }
 }

Nested resources are released in reverse order of acquisition. Outer resources are released even if an inner use or release fails.

DIResource can be used in non-FP context with DIResource.Simple it can also mimic Java's initialization-after-construction with DIResource.Mutable

DIResource is compatible with cats.effect.Resource. Use DIResource.fromCats and DIResource.DIResourceCatsSyntax.toCats to convert back and forth.

Use DIResource's to specify lifecycles of objects injected into the object graph.

Example:

import cats.effect.IO

class DBConnection
class MessageQueueConnection

val dbResource = DIResource.make(IO { println("Connecting to DB!"); new DBConnection })(_ => IO(println("Disconnecting DB")))
val mqResource = DIResource.make(IO { println("Connecting to Message Queue!"); new MessageQueueConnection })(_ => IO(println("Disconnecting Message Queue")))

class MyApp(db: DBConnection, mq: MessageQueueConnection) {
  val run = IO(println("Hello World!"))
}

val module = new ModuleDef {
  make[DBConnection].fromResource(dbResource)
  make[MessageQueueConnection].fromResource(mqResource)
  make[MyApp]
}

Injector().produceF[IO](module).use {
  objects =>
    objects.get[MyApp].run
}.unsafeRunSync()

Will produce the following output:

Connecting to DB!
Connecting to Message Queue!
Hello World!
Disconnecting Message Queue
Disconnecting DB

The lifecycle of the entire object graph is itself expressed with DIResource, you can control it by controlling the scope in .use or you can opt-out and use DIResourceBase.acquire and DIResourceBase.release manually.

See also

ModuleDef.fromResource: com.github.pshirshov.izumi.distage.model.definition.dsl.ModuleDefDSL.BindDSL.fromResource cats.effect.Resource: https://typelevel.org/cats-effect/datatypes/resource.html

Linear Supertypes
DIResourceBase[F, Resource], AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. DIResource
  2. DIResourceBase
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. final type InnerResource = Resource
    Definition Classes
    DIResourceDIResourceBase

Abstract Value Members

  1. abstract def acquire: F[Resource]
    Definition Classes
    DIResourceDIResourceBase
  2. abstract def release(resource: Resource): F[Unit]
    Definition Classes
    DIResourceDIResourceBase

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. final def evalMap[G[x] >: F[x], B](f: (Resource) ⇒ G[B])(implicit arg0: DIEffect[G]): DIResourceBase[G, B]
    Definition Classes
    DIResourceBase
  9. final def extract(resource: Resource): Resource
    Definition Classes
    DIResourceDIResourceBase
  10. final def flatMap[G[x] >: F[x], B](f: (Resource) ⇒ DIResourceBase[G, B])(implicit arg0: DIEffect[G]): DIResourceBase[G, B]
    Definition Classes
    DIResourceBase
  11. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  12. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  13. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  14. final def map[B](f: (Resource) ⇒ B): DIResourceBase[F, B]
    Definition Classes
    DIResourceBase
  15. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  16. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  17. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  18. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  19. def toString(): String
    Definition Classes
    AnyRef → Any
  20. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  22. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @Deprecated @deprecated @throws( classOf[java.lang.Throwable] )
    Deprecated

    (Since version ) see corresponding Javadoc for more information.

Inherited from DIResourceBase[F, Resource]

Inherited from AnyRef

Inherited from Any

Ungrouped