component

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2019 License: Apache-2.0 Imports: 5 Imported by: 30

README

Overview

Component Manager provides dependency injection and lifecycle management for component-based monolith architecture apps.

See Demo application

A Component is a struct which can have dependencies and/or can implement lifecycle interfaces.

Build Status GolangCI Go Report Card GoDoc codecov

Features
  • two step initialization
  • reflect based dependency injection for interfaces
  • resolving circular dependency
  • components lifecycle support
  • ordered start, gracefully stop with reverse order
  • easy component and integration tests with mock
  • subcomponents support
  • reduce boilerplate code

Contetns

Basic usage

Object definition

A Component is a struct which can have dependencies and/or can implement lifecycle interfaces.

Dependencies defined as fields in the struct and must be an interface type. have to be exportable because reflect can set only exportable struct fields. Also Dependencies must have tag inject:"".

type Supermarket struct {
	Warehouse core.Warehouse `inject:""`
}
	cm := component.NewManager(nil)
	cm.Register(producer.NewFarm(), producer.NewDoorFactory())
	cm.Register(&supermarket.Supermarket{}, &warehouse.Warehouse{})
	cm.Register(NewCustomer("Bob"), NewCustomer("Alice"))
	cm.Inject()

Component lifecycle

Usually components lives from app process executes till process finished.

  • new(instance created, first initialization)
  • inject(required dependency injected)
  • init(second initialization)
  • start(component can call their dependency interfaces, run goroutines)
  • prepare stop(optional)
  • stop (gracefully stop goroutines, close descriptors)
Component constructor

Constructor with config as param.

Init and start

When should use Init and when Start? What does it means.

Stop and gracefully stop

tbd

intefaces

type Initer interface {
	Init(ctx context.Context) error
}

type Starter interface {
	Start(ctx context.Context) error
}

type Stopper interface {
	Stop(ctx context.Context) error
}

Similar projects

Documentation

Overview

This package provides dependency injection and lifecycle management for component based monolith architecture. Dependency injection feature is based on reflection. The package also provides Initer, Starter and Stopper interfaces for lifecycle management. A component is a struct which implements one or several interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultLogger

type DefaultLogger struct{}

DefaultLogger logs to std out

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(v ...interface{})

func (*DefaultLogger) Debugf

func (l *DefaultLogger) Debugf(format string, v ...interface{})

type GracefulStopper

type GracefulStopper interface {
	GracefulStop(ctx context.Context) error
}

GracefulStopper interface provides method to end work with other components.

type Initer

type Initer interface {
	Init(ctx context.Context) error
}

Initer interface provides method to init a component. During initialization components may NOT be ready. Only safe methods (e.g. not dependant on other components) can be called during initialization.

type Logger

type Logger interface {
	Debug(v ...interface{})
	Debugf(format string, v ...interface{})
}

Logger interface provides methods for debug logging

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager provide methods to manage components lifecycle

func NewManager

func NewManager(parent *Manager) *Manager

NewManager creates new component manager with default logger

func (*Manager) GracefulStop

func (m *Manager) GracefulStop(ctx context.Context) error

Stop invokes Stop method of all components which implements Starter interface

func (*Manager) Init

func (m *Manager) Init(ctx context.Context) error

Init invokes Init method of all components which implements Initer interface

func (*Manager) Inject

func (m *Manager) Inject(components ...interface{})

Inject components in Manager and inject required dependencies Inject can inject interfaces only, tag public struct fields with `inject:""`

func (*Manager) Register

func (m *Manager) Register(components ...interface{})

Register components in Manager and inject required dependencies. Register can inject interfaces only, tag public struct fields with `inject:""`. If the injectable struct already has a value on the tagged field, the value WILL NOT be overridden.

func (*Manager) SetLogger

func (m *Manager) SetLogger(logger Logger)

SetLogger sets custom DefaultLogger

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) error

Start invokes Start method of all components which implements Starter interface

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context) error

Stop invokes Stop method of all components which implements Starter interface

type NoLogger

type NoLogger struct{}

NoLogger skips log messages

func (*NoLogger) Debug

func (l *NoLogger) Debug(v ...interface{})

func (*NoLogger) Debugf

func (l *NoLogger) Debugf(format string, v ...interface{})

type Starter

type Starter interface {
	Start(ctx context.Context) error
}

Starter interface provides method to start a component.

type Stopper

type Stopper interface {
	Stop(ctx context.Context) error
}

Stopper interface provides method to stop a component.

Jump to

Keyboard shortcuts

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