gapp

package module
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 2 Imported by: 12

README

GApp

This library intent to provide a flexible and opinionated structure to build applications in a way that removes the problems of coding the same functionalities over and over regarding system components as configuration or logging.

Is the opinion of the library that the application scaffolding should revolve around the idea of a service container, seen in other types of frameworks. This container is responsible for storing factories of the application services that should be called for service instantiation, per need. This facilitates not only the composition of the application, but also directs the application architecture for a more dependency injection way of structuring services. By following this decoupled way of writing the application. will enable for more scalable and maintainable testing and production code.

Documentation

Index

Constants

View Source
const (
	// ContainerID defines the gapp package container id base string.
	ContainerID = "gapp"
)
View Source
const (
	// EnvBase defines the gapp package environment base string.
	EnvBase = "GAPP"
)

Variables

View Source
var (
	// ErrNilPointer defines a nil pointer argument error
	ErrNilPointer = fmt.Errorf("invalid nil pointer")

	// ErrConversion defines a type conversion error
	ErrConversion = fmt.Errorf("invalid type conversion")

	// ErrServiceNotFound defines a service not found on the container
	ErrServiceNotFound = fmt.Errorf("service not found")
)

Functions

This section is empty.

Types

type Application

type Application struct {
	Container ServiceContainer
	// contains filtered or unexported fields
}

Application defines the structure that controls the application container and container initialization.

func NewApplication

func NewApplication() *Application

NewApplication used to instantiate a new application.

func (*Application) Add

func (a *Application) Add(id string, provider ServiceProvider) error

Add will register a new provider into the application used on the application boot.

func (*Application) Boot

func (a *Application) Boot() (err error)

Boot initializes the application if not initialized yet. The initialization of an application is the calling of the register method on all providers, after the registration of all objects in the container, the boot method of all providers will be executed.

type ServiceContainer added in v1.20.0

type ServiceContainer map[string]serviceContainerEntry

ServiceContainer defines the structure that hold the application service factories and initialized services.

func (ServiceContainer) Clear added in v1.20.0

func (c ServiceContainer) Clear() error

Clear will eliminate all the registered object from the container.

func (ServiceContainer) Close added in v1.20.0

func (c ServiceContainer) Close() error

Close clean up the container from all the stored objects. If the object has been already instantiated and implements the Closable interface, then the Close method will be called upon the removing instance.

func (ServiceContainer) Factory added in v1.20.0

func (c ServiceContainer) Factory(id string, factory ServiceFactory, tags ...string) error

Factory will register a service factory used to return a new instance every time you request it. If any object was registered previously with the requested id, then the object will be removed by calling the Remove method previously the storing of the new object factory.

func (ServiceContainer) Get added in v1.20.0

func (c ServiceContainer) Get(id string) (any, error)

Get will retrieve the requested object from the container. If the object has not yet been instantiated, then the factory method will be executed to instantiate it.

func (ServiceContainer) Has added in v1.20.0

func (c ServiceContainer) Has(id string) bool

Has will check if an object is registered with the requested id. This does not mean that is instantiated. The instantiation is just executed when the instance is requested for the first time.

func (ServiceContainer) Remove added in v1.20.0

func (c ServiceContainer) Remove(id string) error

Remove will eliminate the object from the container. If the object has been already instantiated and implements the Closable interface, then the Close method will be called on the removing instance.

func (ServiceContainer) Service added in v1.20.0

func (c ServiceContainer) Service(id string, factory ServiceFactory, tags ...string) error

Service will register a service factory used to return an instance generated by the given factory. This factory method will only be called once, meaning that everytime the service is requested, is always returned the same instance. If any object was registered previously with the requested id, then the object will be removed by calling the Remove method previously the storing of the new object factory.

func (ServiceContainer) Tagged added in v1.20.0

func (c ServiceContainer) Tagged(tag string) ([]any, error)

Tagged will retrieve the list of entries instances that where registered with a tag list containing the request teg.

type ServiceFactory added in v1.20.0

type ServiceFactory func() (any, error)

ServiceFactory is a callback function used to instantiate an object used by the application container when a not yet instantiated object is requested.

type ServiceProvider added in v1.20.0

type ServiceProvider interface {
	Register(ServiceContainer) error
	Boot(ServiceContainer) error
}

ServiceProvider is an interface used to define the methods of an object that can be registered into a gapp application and register elements in the application container and do some necessary boot actions on initialization.

Jump to

Keyboard shortcuts

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