example_di

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 6 Imported by: 0

README

Dependency Injection Examples

This directory contains examples demonstrating how to use the di package, which provides a container-based dependency injection system for Go applications.

Examples

1. Basic Usage Example

basic_usage_example.go

Demonstrates basic usage of the Container type, including:

  • Creating a container with context, logger, and configuration
  • Getting the context, logger, and configuration from the container
2. Service Container Example

service_container_example.go

Demonstrates how to use the ServiceContainer type, including:

  • Implementing the Repository, DomainService, and ApplicationService interfaces
  • Implementing the Config interface with AppConfig and DatabaseConfig
  • Creating a service container with dependencies
  • Getting repositories, domain services, and application services from the container
3. Generic Container Example

generic_container_example.go

Demonstrates how to use the GenericAppContainer type, including:

  • Implementing repository, domain service, and application service initializers
  • Creating a generic container with dependencies
  • Getting repositories, domain services, and application services from the container

Running the Examples

To run any of the examples, use the go run command:

go run basic_usage_example.go
go run service_container_example.go
go run generic_container_example.go

Additional Resources

For more information about the di package, see the di package documentation.

Documentation

Overview

Example of basic usage of the dependency injection package

Example of using the generic container from the dependency injection package

Example of using the service container from the dependency injection package

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MyAppConfig

type MyAppConfig struct {
	AppName    string
	AppVersion string
	AppEnv     string
}

Implement the AppConfig interface

func (*MyAppConfig) GetEnvironment

func (c *MyAppConfig) GetEnvironment() string

func (*MyAppConfig) GetName

func (c *MyAppConfig) GetName() string

func (*MyAppConfig) GetVersion

func (c *MyAppConfig) GetVersion() string

type MyApplicationService

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

Implement the ApplicationService interface

func NewMyApplicationService

func NewMyApplicationService(domainService *MyDomainService, repo *MyRepository) (*MyApplicationService, error)

func (*MyApplicationService) Execute

func (s *MyApplicationService) Execute() string

func (*MyApplicationService) GetID

func (s *MyApplicationService) GetID() string

type MyConfig

type MyConfig struct {
	App      *MyAppConfig
	Database *MyDatabaseConfig
}

Implement the Config interface

func (*MyConfig) GetApp

func (c *MyConfig) GetApp() config.AppConfig

func (*MyConfig) GetDatabase

func (c *MyConfig) GetDatabase() config.DatabaseConfig

type MyDatabaseConfig

type MyDatabaseConfig struct {
	DBType             string
	DBConnectionString string
	DBName             string
	Collections        map[string]string
}

Implement the DatabaseConfig interface

func (*MyDatabaseConfig) GetCollectionName

func (c *MyDatabaseConfig) GetCollectionName(entityType string) string

func (*MyDatabaseConfig) GetConnectionString

func (c *MyDatabaseConfig) GetConnectionString() string

func (*MyDatabaseConfig) GetDatabaseName

func (c *MyDatabaseConfig) GetDatabaseName() string

func (*MyDatabaseConfig) GetType

func (c *MyDatabaseConfig) GetType() string

type MyDomainService

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

Implement the DomainService interface

func NewMyDomainService

func NewMyDomainService(repo *MyRepository) (*MyDomainService, error)

func (*MyDomainService) GetID

func (s *MyDomainService) GetID() string

func (*MyDomainService) ProcessData

func (s *MyDomainService) ProcessData() string

type MyRepository

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

Implement the Repository interface

func (*MyRepository) GetData

func (r *MyRepository) GetData() string

func (*MyRepository) GetID

func (r *MyRepository) GetID() string

type ProductApplicationService

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

Implement the ApplicationService interface

func InitProductApplicationService

func InitProductApplicationService(domainService *ProductDomainService, repo *ProductRepository) (*ProductApplicationService, error)

Initialize application service function

func (*ProductApplicationService) GetID

func (s *ProductApplicationService) GetID() string

func (*ProductApplicationService) GetProductRecommendations

func (s *ProductApplicationService) GetProductRecommendations() []string

type ProductDomainService

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

Implement the DomainService interface

func InitProductDomainService

func InitProductDomainService(repo *ProductRepository) (*ProductDomainService, error)

Initialize domain service function

func (*ProductDomainService) GetFeaturedProducts

func (s *ProductDomainService) GetFeaturedProducts() []string

func (*ProductDomainService) GetID

func (s *ProductDomainService) GetID() string

type ProductRepository

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

Implement the Repository interface

func InitProductRepository

func InitProductRepository(ctx context.Context, connectionString string, logger *zap.Logger) (*ProductRepository, error)

Initialize repository function

func (*ProductRepository) GetID

func (r *ProductRepository) GetID() string

func (*ProductRepository) GetProducts

func (r *ProductRepository) GetProducts() []string

type SimpleConfig

type SimpleConfig struct{}

Simple configuration type

Jump to

Keyboard shortcuts

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