dire_di library

Dire DI - Spring-like Dependency Injection for Dart

A powerful dependency injection framework that brings Spring-like features to Dart, including auto-wiring capabilities using Dart's mirrors library.

Features

  • Spring-like Annotations: @Service, @Repository, @Component, @Controller
  • Auto-wiring: Automatic dependency resolution using @Autowired
  • Qualifier Support: Use @Qualifier for specific bean selection
  • Singleton and Prototype Scopes: Control object lifecycle
  • Conditional Registration: @ConditionalOnProperty, @ConditionalOnClass
  • Profile Support: @Profile for environment-specific beans
  • Configuration Classes: @Configuration and @Bean for manual setup

Quick Start

import 'package:dire_di/dire_di.dart';

@Service()
class UserService {
  @Autowired()
  late UserRepository userRepository;

  Future<User> getUser(int id) {
    return userRepository.findById(id);
  }
}

@Repository()
class UserRepository {
  Future<User> findById(int id) async {
    // Implementation here
  }
}

void main() async {
  final container = DireContainer();
  await container.scan(); // Auto-discover and register components

  final userService = container.get<UserService>();
  // userService.userRepository is automatically injected!
}

Advanced Usage

Qualifiers

@Service()
@Qualifier('primary')
class PrimaryUserService implements UserService { }

@Service()
@Qualifier('secondary')
class SecondaryUserService implements UserService { }

@Component()
class UserController {
  @Autowired()
  @Qualifier('primary')
  late UserService userService;
}

Configuration Classes

@Configuration()
class DatabaseConfig {
  @Bean()
  @Singleton()
  Database createDatabase() {
    return Database(connectionString: 'localhost:5432');
  }
}

Profiles

@Service()
@Profile('development')
class DevUserService implements UserService { }

@Service()
@Profile('production')
class ProdUserService implements UserService { }

Classes

Autowired
Autowired annotation for automatic dependency injection
Bean
Bean method annotation
BeanDefinition
Represents a bean definition in the DI container
Component
Core component annotation - base for all stereotypes
Conditional
Base class for all conditional annotations
ConditionalOnBean
Register bean only if a bean of the specified type exists
ConditionalOnClass
Register bean only if the specified class is present on the classpath
ConditionalOnMissingBean
Register bean only if no bean of the specified type exists
ConditionalOnProperty
Register bean only if the specified property exists and matches the value
Configuration
Configuration class annotation
Controller
Controller layer annotation - indicates presentation layer components
DireContainer
The main dependency injection container for Dire DI
FieldDefinition
Represents a field that needs injection
InjectionContext
Injection context for managing bean creation and dependency resolution
ParameterDefinition
Represents a constructor parameter that needs injection
Profile
Profile annotation for environment-specific beans
Prototype
Convenience annotation for prototype scope
Qualifier
Qualifier annotation for specific bean selection
Repository
Repository layer annotation - indicates data access layer components
Scope
Scope annotation for controlling bean lifecycle
Service
Service layer annotation - indicates that this class provides business logic
Singleton
Convenience annotation for singleton scope

Enums

ScopeType
Enumeration of bean scope types

Functions

areTypesEquivalent(Type type1, Type type2) bool
Checks if two types are equivalent for dependency injection purposes
convertStringToType<T>(String value, Type targetType) → T?
Converts a string to the appropriate type
createBeanDefinition(ClassMirror classMirror) BeanDefinition
Creates a bean definition from a class mirror
getAnnotation<T>(DeclarationMirror mirror) → T?
Gets a specific annotation from a class or member
getAnnotations<T>(DeclarationMirror mirror) List<T>
Gets all annotations of a specific type from a class or member
getDefaultValue(Type type) Object?
Gets a default value for a type
getGenericTypeArguments(Type type) List<Type>
Extracts generic type arguments from a collection type
getNonNullableType(Type type) Type
Gets the non-nullable version of a type
getTypeName(Type type) String
Gets the simple name of a type (without library prefix)
hasAnnotation<T>(DeclarationMirror mirror) bool
Checks if a class or member has a specific annotation
hasComponentAnnotation(ClassMirror classMirror) bool
Checks if a class has any component annotation
injectFields(Object instance, List<FieldDefinition> fieldDefinitions, Object beanResolver(Type type, String? qualifier)) → void
Injects dependencies into an object's fields
isAssignableFrom(Type target, Type source) bool
isCollectionType(Type type) bool
Checks if a type is a collection type
isNullableType(Type type) bool
Checks if a type is nullable
isPrimitiveType(Type type) bool
Checks if a type is a primitive type
scanConfigurationBeans(List<ClassMirror> configClasses) List<BeanDefinition>
Scans for configuration classes and extracts bean methods
scanForComponents([List<String>? packagePatterns]) List<ClassMirror>
Utility functions for reflection operations using dart:mirrors

Exceptions / Errors

BeanCreationException
Exception thrown when bean creation fails
BeanNotFoundException
Exception thrown when a required bean cannot be found
CircularDependencyException
Exception thrown when circular dependencies are detected
ConditionalEvaluationException
Exception thrown when conditional evaluation fails
ContainerInitializationException
Exception thrown when container initialization fails
DireException
Base exception class for all Dire DI related exceptions
InjectionException
Exception thrown when dependency injection fails
InvalidBeanConfigurationException
Exception thrown when bean configuration is invalid
MultipleBeanFoundException
Exception thrown when multiple beans are found and no primary is specified