Skip to content

Container

Josh Wright edited this page Jan 14, 2021 · 3 revisions

Container

A container from which services should be registered and resolved.

public final class Container

Initializers

init(parent:)

Initialize a container with an optional parent Container.

public init(parent: Container? = nil)

Parameters

  • parent: The optional parent Container. Defaults to nil.

Properties

global

A global, singleton container.

var global

Methods

register(_:factory:)

Register a transient service to this container. Transient means that it's factory closure will be called each time the service type is resolved.

public func register<T>(_ service: T.Type, factory: @escaping (Container) -> T)

Parameters

  • service: The type of the service to register.
  • factory: The closure for instantiating an instance of the service.

register(singleton:factory:)

Register a singleton service to this container. This means that it's factory closure will be called once and that value will be returned each time the service is resolved.

public func register<S>(singleton service: S.Type, factory: @escaping (Container) -> S)

Parameters

  • service: The type of the service to register.
  • factory: The closure for instantiating an instance of the service.

register(singleton:identifier:factory:)

Register a identified singleton service to this container. Singleton means that it's factory closure will be called once per unique identifier and that value will be returned each time the service is resolved.

public func register<S, H: Hashable>(singleton service: S.Type, identifier: H, factory: @escaping (Container) -> S)

Parameters

  • service: The type of the service to register.
  • factory: The closure for instantiating an instance of the service.

resolveOptional(_:)

Resolves a service, returning an instance of it, if one is registered.

public func resolveOptional<T>(_ service: T.Type) -> T?

Parameters

  • service: The type of the service to resolve.

Returns

An instance of the service.

resolveOptional(_:identifier:)

Resolves a service with the given identifier, returning an instance of it if one is registered.

public func resolveOptional<T, H: Hashable>(_ service: T.Type, identifier: H?) -> T?

Parameters

  • service: The type of the service to resolve.
  • identifier: The identifier of the service to resolve.

Returns

An instance of the service.

resolve(_:)

Resolves a service, returning an instance of it.

public func resolve<T>(_ service: T.Type) -> T

This will fatalError if the service isn't registered.

Parameters

  • service: The type of the service to resolve.

Returns

An instance of the service.

resolve(_:identifier:)

Resolves a service with the given identifier, returning an instance of it.

public func resolve<T, H: Hashable>(_ service: T.Type, identifier: H?) -> T

This will fatalError if the service isn't registered.

Parameters

  • service: The type of the service to resolve.
  • identifier: The identifier of the service to resolve.

Returns

An instance of the service.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally