Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DependencyMapper

type DependencyMapper struct {

	// Mapped dependencies. These are mapped based upon a user-defined name and
	// are stored as the interface type. This is intended mostly for situations
	// where multiple dependencies of the same type cannot be stored cleanly or
	// where users require control over how they manage the dependencies.
	Mapped map[string]interface{}
	// contains filtered or unexported fields
}

DependencyMapper handles dependency management and dependency injection. Although we do not use generators to create compile-time type guarantees, care is taken with reflection here to ensure that some type safety is present and little burden is placed on the developer to handle type casting.

This dependency injection mechanism works by examining handler targets for exported fields with the struct tag `dependency:"inject"`. If the tag is present, the field type is inspected, and the mapped dependencies (if they were Register()'d) will be located and applied to the field. Use code can then define any dependency via field types, register almost anything required, and have the dependencies injected at run time during application initialization.

Reflection is only performed during initialization, not per request.

There are some limitations with this method: First, registering multiple dependencies of the same type will overwrite whatever previous dependency existed. If this behavior is required, it may be necessary to register a proxy type that interfaces with multiple dependencies (such as multiple databases of the same interface type). Likewise, this may present problems for non-pointer types in some situations. Second, we're considering solutions to this by providing a means to request a specific named dependency of a given type using struct tags, i.e. `inject:"<dependency_name>"`. Then, if multiple dependencies with that same type are registered, the mapper can select whichever one was requested by the client code.

func CopyDependencyMapper

func CopyDependencyMapper(src *DependencyMapper) *DependencyMapper

func NewDependencyMapper

func NewDependencyMapper() *DependencyMapper

NewDependencyMapper returns a new DependencyMapper pre-initialized with the appropriate internal data types. This function should be used in lieu of creating the type and initializing it manually, particularly since Capstan's API is likely to remain in a state of flux for some time.

func (*DependencyMapper) Apply

func (dm *DependencyMapper) Apply(target interface{})

Apply dependencies to the target interface type.

Dependencies are defined as exported fields with the `dependency:"inject"` struct tag. Note that other struct tags may be supported in the future.

func (*DependencyMapper) Map

func (dm *DependencyMapper) Map(name string, dep interface{})

Map the dependency `dep` to the name `name`. This should be used if you intended to handle dependencies manually.

func (*DependencyMapper) Register

func (dm *DependencyMapper) Register(name string, dep interface{})

RegisterFull registers the dependency both by its type and by a symbolic name. This is synonymous with calling Regster() and RegisterNamed() for the same type.

Source Files