trait ModuleDef extends Module with ModuleDefDSL
DSL for defining module Bindings.
Example:
class Program[F[_]: TagK: Monad] extends ModuleDef { make[TaglessProgram[F]] } object TryInterpreters extends ModuleDef { make[Validation.Handler[Try]].from(tryValidationHandler) make[Interaction.Handler[Try]].from(tryInteractionHandler) } // Combine modules into a full program val TryProgram = new Program[Try] ++ TryInterpreters
Singleton bindings:
make[X]
= create X using its constructormakeTrait[X]
= create an abstract class or a traitX
using izumi.distage.constructors.TraitConstructor (Auto-Traits feature)makeFactory[X]
= create a "factory-like" abstract class or a traitX
using izumi.distage.constructors.FactoryConstructor (Auto-Factories feature)make[X].from[XImpl]
= bind X to its subtype XImpl using XImpl's constructormake[X].fromTrait[XImpl]
= bind X to its abstract class or a trait subtype XImpl, deriving constructor using izumi.distage.constructors.TraitConstructor (Auto-Traits feature)make[X].fromFactory[XImpl]
= bind X to its "factory-like" abstract class or a trait subtype XImpl, deriving constructor using izumi.distage.constructors.FactoryConstructor (Auto-Factories feature)make[X].from(myX)
= bind X to an already existing instancemyX
make[X].from { y: Y => new X(y) }
= bind X to an instance of X constructed by a given Functoid requesting an Y parametermake[X].from { y: Y @Id("special") => new X(y) }
= bind X to an instance of X constructed by a given Functoid, requesting a named "special" Y parametermake[X].from { y: Y => new X(y) }
.annotateParameter[Y]("special") = bind X to an instance of X constructed by a given Functoid, requesting a named "special" Y parametermake[X].named("special")
= bind a named instance of X. It can then be summoned using Id annotation.make[X].using[X]("special")
= bind X to refer to another already bound named instance at key[X].named("special")
make[X].fromEffect(X.create[F]: F[X])
= create X using a purely-functional effectX.create
inF
monadmake[X].fromResource(X.resource[F]: Lifecycle[F, X])
= create X using aLifecycle
value specifying its creation and destruction lifecyclemake[X].from[XImpl].modify(fun(_))
= Create X using XImpl's constructor and applyfun
to the resultmake[X].from[XImpl].modifyBy(_.flatAp { (c: C, d: D) => (x: X) => c.method(x, d) })
= Create X using XImpl's constructor and modify itsFunctoid
using the provided lambda - in this case by summoning additionalC
&D
dependencies and applyingC.method
toX
Set bindings:
many[X].add[X1].add[X2]
= bind aSet
of X, and add subtypes X1 and X2 created via their constructors to it. Sets can be bound in multiple different modules. All the elements of the same set in different modules will be joined together.many[X].add(x1).add(x2)
= add *instances* x1 and x2 to aSet[X]
many[X].add { y: Y => new X1(y).add { y: Y => X2(y) }
= add instances of X1 and X2 constructed by a given Provider functionmany[X].named("special").add[X1]
= create a named set of X, all the elements of it are added to this named set.many[X].ref[XImpl]
= add a reference to an already **existing** binding of XImpl to a set of X'smany[X].ref[X]("special")
= add a reference to an **existing** named binding of X to a set of X's
Mutators:
modify[X](fun(_))
= add a modifier applyingfun
to the value bound atX
(mutator application order is unspecified)modify[X].by(_.flatAp { (c: C, d: D) => (x: X) => c.method(x, d) })
= add a modifier, applying the provided lambda to aFunctoid
retrievingX
- in this case by summoning additionalC
&D
dependencies and applyingC.method
toX
Tags:
make[X].tagged("t1", "t2)
= attach tags to X's binding.many[X].add[X1].tagged("x1tag")
= Tag a specific element of X. The tags of sets and their elements are separate.many[X].tagged("xsettag")
= Tag the binding of empty Set of X with a tag. The tags of sets and their elements are separate.
Includes:
include(that: ModuleDef)
= add all bindings inthat
module intothis
module
- See also
TagK
ModuleDefDSL
- Alphabetic
- By Inheritance
- ModuleDef
- ModuleDefDSL
- TagsDSL
- IncludesDSL
- AbstractBindingDefDSL
- AbstractBindingDefDSLMacro
- Module
- ModuleBase
- CachedHashcode
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class MutationContext extends AnyRef
Use this to create utility functions that add bindings mutably to the current module, as opposed to creating new modules and including them.
Use this to create utility functions that add bindings mutably to the current module, as opposed to creating new modules and including them.
Example:
import distage.{ClassConstructor, Tag, ModuleDef} import izumi.distage.model.definition.dsl.ModuleDefDSL trait RegisteredComponent class RegisteredComponentImpl extends RegisteredComponent def addAndRegister[T <: RegisteredComponent: Tag: ClassConstructor](implicit mutateModule: ModuleDefDSL#MutationContext): Unit = { new mutateModule.dsl { make[T] many[RegisteredComponent] .weak[T] } } new ModuleDef { addAndRegister[RegisteredComponentImpl] }
- Definition Classes
- AbstractBindingDefDSL
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def _initialIncludes: ArrayBuffer[Include]
- Attributes
- protected[this]
- Definition Classes
- IncludesDSL
- def _initialState: ArrayBuffer[BindingRef]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- def _initialTaggedIncludes: ArrayBuffer[IncludeApplyTags]
- Attributes
- protected[this]
- Definition Classes
- IncludesDSL
- def _initialTags: Set[BindingTag]
- Attributes
- protected[this]
- Definition Classes
- TagsDSL
- final def _make[T](provider: Functoid[T])(implicit arg0: Tag[T], pos: CodePositionMaterializer): MakeDSL[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- def _registered[T <: BindingRef](bindingRef: T): T
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- final def addImplicit[T](implicit arg0: Tag[T], instance: T, pos: CodePositionMaterializer): MakeDSLUnnamedAfterFrom[T]
Same as
make[T].from(implicitly[T])
*Same as
make[T].from(implicitly[T])
*- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- final def bindings: Set[Binding]
- Definition Classes
- ModuleDefDSL
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def equals(obj: Any): Boolean
- Definition Classes
- ModuleBase → AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def hash: Int
- Attributes
- protected
- Definition Classes
- ModuleBase → CachedHashcode
- final lazy val hashCode: Int
- Definition Classes
- CachedHashcode → AnyRef → Any
- final def include(that: ModuleBase): Unit
Add all bindings in
that
module intothis
moduleAdd all bindings in
that
module intothis
moduleWON'T add global tags from TagsDSL#tag to included bindings.
- Attributes
- protected[this]
- Definition Classes
- IncludesDSL
- final def includeApplyTags(that: ModuleBase): Unit
Add all bindings in
that
module intothis
moduleAdd all bindings in
that
module intothis
moduleWILL add global tags from TagsDSL#tag to included bindings.
- Attributes
- protected[this]
- Definition Classes
- IncludesDSL
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def iterator: Iterator[Binding]
- Definition Classes
- ModuleDefDSL
- final def keys: Set[DIKey]
- Definition Classes
- ModuleBase
- final def keysIterator: Iterator[DIKey]
- Definition Classes
- ModuleDefDSL
- final macro def make[T]: MakeDSL[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSLMacro
- final def makeFactory[T](implicit arg0: Tag[T], arg1: FactoryConstructor[T]): MakeDSLUnnamedAfterFrom[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- See also
- final def makeSubcontext[T](implicit arg0: Tag[T]): SubcontextDSL[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- See also
- final def makeSubcontext[T](submodule: ModuleBase)(implicit arg0: Tag[T]): SubcontextDSL[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- See also
- final def makeTrait[T](implicit arg0: Tag[T], arg1: TraitConstructor[T]): MakeDSLUnnamedAfterFrom[T]
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- See also
- final def many[T](implicit tag: Tag[Set[T]], pos: CodePositionMaterializer): SetDSL[T]
Set bindings are useful for implementing event listeners, plugins, hooks, http routes, etc.
Set bindings are useful for implementing event listeners, plugins, hooks, http routes, etc.
To define a multibinding use
.many
and.add
methods in ModuleDef DSL:import cats.effect._, org.http4s._, org.http4s.dsl.io._, scala.concurrent.ExecutionContext.Implicits.global import distage._ object HomeRouteModule extends ModuleDef { many[HttpRoutes[IO]].add { HttpRoutes.of[IO] { case GET -> Root / "home" => Ok(s"Home page!") } } }
Set bindings defined in different modules will be merged together into a single Set. You can summon a created Set by type
Set[T]
:import cats.implicits._, org.http4s.server.blaze._, org.http4s.implicits._ object BlogRouteModule extends ModuleDef { many[HttpRoutes[IO]].add { HttpRoutes.of[IO] { case GET -> Root / "blog" / post => Ok("Blog post ``$post''!") } } } class HttpServer(routes: Set[HttpRoutes[IO]]) { val router = routes.foldK def serve = BlazeBuilder[IO] .bindHttp(8080, "localhost") .mountService(router, "/") .start } val objects = Injector().produce(HomeRouteModule ++ BlogRouteModule) val server = objects.get[HttpServer] val testRouter = server.router.orNotFound testRouter.run(Request[IO](uri = uri("/home"))).flatMap(_.as[String]).unsafeRunSync // Home page! testRouter.run(Request[IO](uri = uri("/blog/1"))).flatMap(_.as[String]).unsafeRunSync // Blog post ``1''!
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- See also
Guice wiki on Multibindings: https://github.com/google/guice/wiki/Multibindings
- final def modify[T]: ModifyDSL[T, MakeDSL, MakeDSLUnnamedAfterFrom, SetDSL]
Modify a value bound at
T
.Modify a value bound at
T
. Modifiers stack and are all applied beforeT
is added to the object graph; only the final instance is observable.import distage.{Injector, ModuleDef} Injector().produceGet[Int](new ModuleDef { make[Int].from(1) modify[Int](_ + 1) modify[Int](_ + 1) }).use(i => println(s"Got `Int` $i")) // Got `Int` 3
You can also modify while summoning additional dependencies:
modify[Int]("named").by(_.flatAp { (adder: Adder, multiplier: Multiplier) => int: Int => multiplier.multiply(adder.add(int, 1), 10) })
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- implicit final lazy val mutationContext: MutationContext
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- final def tag(tags: BindingTag*): Unit
Add
tags
to all bindings in this module, except for included bindingsAdd
tags
to all bindings in this module, except for included bindings- Attributes
- protected[this]
- Definition Classes
- TagsDSL
- final def toString(): String
- Definition Classes
- ModuleBase → AnyRef → Any
- final def todo[T](implicit arg0: Tag[T], pos: CodePositionMaterializer): MakeDSLUnnamedAfterFrom[T]
Create a dummy binding that throws an exception with an error message when it's created.
Create a dummy binding that throws an exception with an error message when it's created.
Useful for prototyping.
- Attributes
- protected[this]
- Definition Classes
- AbstractBindingDefDSL
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()