Documentation
¶
Overview ¶
Package di provides dependency injection setup for the application.
Package di provides an improved dependency injection container
Index ¶
- func GetUserRepository() repo.UserRepository
- func GetUserService() *user.UserService
- func InitContainer(cfg config.Config, gdb *gorm.DB) error
- func IsRegistered[T any](c *Container, tag ...string) bool
- func MustResolve[T any](c *Container) T
- func MustResolveWithTag[T any](c *Container, tag string) T
- func Provide[T any](c *Container, impl T, tag ...string) error
- func Register[T any](c *Container, constructor any, scope Scope, tag ...string) error
- func RegisterFactory[T any](c *Container, factory func() T, scope Scope, tag ...string) error
- func Resolve[T any](c *Container) (T, error)
- func ResolveWithTag[T any](c *Container, tag string) (T, error)
- type Container
- type Scope
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetUserRepository ¶
func GetUserRepository() repo.UserRepository
GetUserRepository resolves the user repository from the container.
func GetUserService ¶
func GetUserService() *user.UserService
GetUserService resolves the user service from the container.
func InitContainer ¶
InitContainer initializes the DI container with all dependencies.
func IsRegistered ¶
IsRegistered reports whether T (and optional tag) has at least one registration.
func MustResolve ¶
func MustResolveWithTag ¶
func Provide ¶
Provide binds a concrete implementation value to an interface type T as a singleton. This helper intentionally supports interface T only.
func Register ¶
Register registers a constructor for type T with the given scope and optional tag. Patches applied:
- Allow constructors that return (T) or (T, error).
- Basic validation on constructor input parameters: forbid primitives unless explicitly registered.
- Double-check closed state while holding c.mu to avoid Close/Register races.
func RegisterFactory ¶
RegisterFactory registers a typed factory function for T that will be called directly (no reflection) when resolving. This is useful for hot-path types. The factory must be a `func() T` (no error). If you need error-returning factories, use Register with a constructor that returns (T, error).
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
var DIContainer *Container
DIContainer is the global DI container singleton.
func (*Container) Clear ¶
func (c *Container) Clear()
Clear removes all registrations, singletons and resolving state.
func (*Container) Close ¶
func (c *Container) Close()
Close marks the container as closed and clears internal maps. It acquires c.mu so that Close and concurrent Register cannot interleave.
func (*Container) GetRegisteredTypes ¶
GetRegisteredTypes returns all registered types (with tags) for debugging.