zeus

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 6 Imported by: 0

README

🌩 Zeus - Simple Dependency Injection Container

GoDoc GitHub GitHub go.mod Go version (subdirectory of monorepo) Go Report Card codecov

Zeus is a sleek and efficient dependency injection container for Go. Easily register "factories" (functions that create instances of types) and let zeus resolve those dependencies at runtime.

🌟 Features

Why using zeus?

🚀 Simple to Use

With a minimalist API, integrating zeus into any Go project is a breeze.

🔍 Dependency Resolution

Register your dependencies and let zeus handle the rest.

⚠️ Cyclic Dependency Detection

Zeus detects and reports cycles in your dependencies to prevent runtime errors.

🪝 Hooks

Zeus supports lifecycle hooks, allowing you to execute functions at the start and end of your application. This is especially useful for setups and teardowns, like establishing a database connection or gracefully shutting down services.

🚀 Getting Started

Installation
go get -u github.com/otoru/zeus
Register Dependencies
package main

import "github.com/otoru/zeus"

c := zeus.New()

c.Provide(func() int {
  return 42
})

c.Provide(func(i int) string {
  return fmt.Sprintf("Number: %d", i) 
})
Resolve & Run Functions
err := c.Run(func(s string) error {
    fmt.Println(s) // Outputs: Number: 42
    return nil
})
Using Hooks

Zeus allows you to register hooks that run at the start and end of your application. This is useful for setting up and tearing down resources.

c := zeus.New()

// Servoce is a dummy service that depends on Hooks.
type Service struct{}

c.Provide(func(h zeus.Hooks) *Service {
    h.OnStart(func() error {
        fmt.Println("Starting up...")
        return nil
    })

    h.OnStop(func() error {
        fmt.Println("Shutting down...")
        return nil
    })
    return &Service{}
})

c.Run(func(s *Service) {
    fmt.Println("Main function running with the service!")
})

// Outputs:
// Starting up...
// Main function running with the service!
// Shutting down...

Merging Containers

Zeus now supports merging two containers together using the Merge method. This is especially useful when you have modularized your application and want to combine dependencies from different modules.

How to Use
  1. Create two separate containers.
  2. Add factories to both containers.
  3. Use the Merge method to combine the factories of one container into another.
Example
containerA := zeus.New()
containerB := zeus.New()

containerA.Provide(func() string { return "Hello" })
containerB.Provide(func() int { return 42 })

err := containerA.Merge(containerB)
if err != nil {
    // Handle merge error
}
Note

If a factory from the merging container conflicts with an existing factory in the main container, and they are not identical, a FactoryAlreadyProvidedError will be returned. This ensures that you don't accidentally overwrite existing dependencies.

🤝 Contributing

Contributions are warmly welcomed! Please open a PR or an issue if you find any problems or have enhancement suggestions.

Documentation

Overview

Package zeus provides a lightweight dependency injection container for Go. It allows users to register factories and resolve dependencies at runtime.

Basic usage:

c := zeus.New()
c.Provide(func() int { return 42 })
c.Run(func(i int) {
    fmt.Println(i) // Outputs: 42
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

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

Container holds the registered factories for dependency resolution.

func New

func New() *Container

New initializes and returns a new instance of the Container.

Example:

c := zeus.New()

func (*Container) Merge

func (c *Container) Merge(other *Container) error

Merge combines the factories of another container into the current container. If a factory from the other container conflicts with an existing factory in the current container, and they are not identical, a FactoryAlreadyProvidedError is returned.

Example:

containerA := New()
containerB := New()

containerA.Provide(func() string { return "Hello" })
containerB.Provide(func() int { return 42 })

err := containerA.Merge(containerB)
if err != nil {
    // Handle merge error
}

func (*Container) Provide

func (c *Container) Provide(factories ...interface{}) error

Provide registers a factory function for dependency resolution. It ensures that the factory is a function, has a valid return type, and checks for duplicate factories. Returns an error if any of these conditions are not met.

Example:

c := zeus.New()
c.Provide(func() int { return 42 })

func (*Container) Run

func (c *Container) Run(fn interface{}) error

Run executes the provided function by resolving and injecting its dependencies. It ensures that the function has a valid signature and that all dependencies can be resolved. Returns an error if the function signature is invalid or if dependencies cannot be resolved.

Example:

c := zeus.New()
c.Provide(func() int { return 42 })
c.Run(func(i int) {
    fmt.Println(i) // Outputs: 42
})

type Hooks

type Hooks hooks.Hooks

Hooks is a facade for hooks.Hooks

Directories

Path Synopsis
Hooks defines an interface for a lifecycle of container.
Hooks defines an interface for a lifecycle of container.

Jump to

Keyboard shortcuts

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