Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/fairking/di_plus

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/fairking/di_plus

  • v0.0.1
  • Source
  • Go
  • Socket score

Version published
Created
Source

Go

Dependency Injection

This package contains all of the fundamental functionalities for dependency injection (DI). A ServiceProvider can be used to register services and consume them in your application.

Features

This package provides the following features:

  • Register services with a single instance (singleton only) or with a function (factory);
  • Use of generic function to get your desired service;
  • Three service lifetimes: singleton, scoped, transient;
  • Async initialization (coming soon)

Service Lifetimes

A service can have the following lifetimes:

  • Transient - a new instance is created every time it is requested
  • Singleton - a single, new instance is created the first time it is requested
  • Scoped - a new instance is created once per ServiceScope the first time it is requested

Scope Scenarios

There scenarios where a service needs to be scoped; for example, for the lifetime of a HTTP request. A service definitely shouldn't live for the life of the application (e.g. singleton), but it also shouldn't be created each time it's requested within the request (e.g. transient). A scoped service lives for the lifetime of the container (ServiceScope) it was created from.

Installation

go get github.com/fairking/di_plus

Usage

package main

import (
    "github.com/fairking/di_plus"
)

type MySettingsService struct {
	Connection string
}

type MyDatabaseService struct {
	Settings MyDatabaseService
}

// Register Services
services := NewServiceBuilder().
    // Singleton
    Register(
        GetTypeOf[MySettingsService](),
        ServiceTypeEnum.Singleton,
        func(s IServiceProvider) (interface{}, error) {
            return MySettingsService{Connection: "server=127.0.0.1;uid=root;pwd=12345;database=test"}, nil
        },
    ).
    // Scoped
    Register(
        GetTypeOf[MyDatabaseService](),
        ServiceTypeEnum.Scoped,
        func(s IServiceProvider) (interface{}, error) {
            return MyDatabaseService{Settings: GetService[*MySettingsService](s)}, nil
        },
    ).
    Build()

// Get Singleton
settings := services.GetService("MySettingsService").(*MySettingsService) // Creates a new settings value
// or
settings2 := GetService[*MySettingsService](services) // Returns the existing settings value
// settings and settings2 pointing to the same value

// Get Scope
{
    scope := services.GetScope()
    db := GetService[*MyDatabaseService](scope) // Creates a new database service value
    // or
    db_, err := GetServiceOr[*MyDatabaseService](scope) // Returns the existing database service value

    settings3 := GetService[*MySettingsService](services) // Returns the existing settings value
}

// Get Scope
{
    scope := services.GetScope()
    db2 := GetService[*MyDatabaseService](scope) // Creates a new database service value
    // or
    db2_, err := GetServiceOr[*MyDatabaseService](scope) // Returns the existing database service value

    settings3 := GetService[*MySettingsService](services) // Returns the existing settings value
}

// db and db_ is the same value, but db and db2 are different

Contribution

If you have questions please create a new issue.

Donations

Donate with Ӿ nano crypto (XNO).

di_plus Donations

Thank you!

FAQs

Package last updated on 28 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc