bussard

package module
v0.0.0-...-1ce3336 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2019 License: MIT Imports: 3 Imported by: 1

README

Bussard

GoDoc Build Status Maintainability Test Coverage

Bussard is a service container and dependency injection tool for Golang.

Overview

Bussard provides a ServiceContainer to which a service (any value) can be registered with a unique name. Users of that service can pull a reference to the service from the container by using the same name.

Usage of this library can improve code which must thread references to objects which are required either frequently, or somewhere deep within the application. Instead, a single populated container instance can be passed and clients can simply pull their own dependencies from it without requiring the calling code to worry about initialization dependencies.

Usage

Basic usage is very simple. Simply create a container, then get and set objects into it. Names must be unique, so setting an object requires the name is not already taken, and getting an object requires that the object has already been put into the container.

container := NewServiceContainer()
if err := container.Set("name", &NewService()); err != nil {
    // ...
}

// Later that day...
service, err := container.Get("name").(*Service)
if err != nil {
    // ...
}

The Get and Set methods have analogous MustGet and MustSet methods which panic on error.

Standard usage injects service instances into tagged structs. This should be the default way in which a client of a service receives an instance.

The following struct tags four fields with service dependencies. When the container injects services into this instance, it will look up services with the given name and attempt to cast them to the appropriate type. It is an error to request a service that has not been registered (unless the optional tag is present, in which case the field value remains nil), or to request a service of a type which cannot be assigned to the field. Notice that fields with tags must be exported.

type Server struct {
    Logger         logging.Logger     `service:"logger"`
    MetricReporter metrics.Reporter   `service:"metric-reporter"`
    Cache          cache.Cache        `service:"cache"`
    SecondaryAuth  auth.Authenticator `service:"authenticator" optional:"true"`
}

To inject services, the code which creates the server (depending on application flow) must simply pass the instance to inject to the container.

server := &Server{}

// ...

if err := container.Inject(server); err != nil {
    // ...
}

License

Copyright (c) 2018 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostInject

type PostInject interface {
	PostInject() error
}

PostInject is a marker interface for injectable objects which should perform some action after injection of services.

type ServiceContainer

type ServiceContainer interface {
	// Get retrieves the service registered to the given key. It is an
	// error for a service not to be registered to this key.
	Get(key string) (interface{}, error)

	// MustGet calls Get and panics on error.
	MustGet(service string) interface{}

	// Set registers a service with the given key. It is an error for
	// a service to already be registered to this key.
	Set(key string, service interface{}) error

	// MustSet calls Set and panics on error.
	MustSet(service string, value interface{})

	// Inject will attempt to populate the given type with values from
	// the service container based on the value's struct tags. An error
	// may occur if a service has not been registered, a service has a
	// different type than expected, or struct tags are malformed.
	Inject(obj interface{}) error
}

ServiceContainer is a wrapper around services indexed by a unique name. Services can be retrieved by name, or injected into a struct by reading tagged fields.

func NewServiceContainer

func NewServiceContainer() ServiceContainer

NewServiceContainer creates an empty service container.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL