https://github.com/evandersondev/injectfy
https://github.com/evandersondev/injectfy
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/evandersondev/injectfy
- Owner: evandersondev
- License: mit
- Created: 2024-12-16T16:54:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-27T16:35:24.000Z (over 1 year ago)
- Last Synced: 2024-12-27T17:30:32.295Z (over 1 year ago)
- Language: Dart
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Injectfy
๐ A minimalistic and easy-to-use dependency injection library for managing singletons and factories in Dart.
## ๐ Features
- โ
Register singletons and factories to manage dependencies.
- ๐ Automatically resolve and inject dependencies.
- โ Easily unregister dependencies.
- โก Cache frequently accessed instances for better performance.
- ๐ ๏ธ Useful for both production and testing (with mock support).
## ๐ฆ Installation
Add the following dependency to your `pubspec.yaml`:
```yaml
dependencies:
injectfy: ^1.0.1
```
Then, run:
```bash
flutter pub get
```
## ๐ Getting Started
### ๐ Basic Usage
```dart
import 'package:injectfy/injectfy.dart';
void main() {
// Get instance
final injectfy = Injectfy.instance;
// Registering a singleton
injectfy.registerSingleton(() => SomeService());
// Registering a factory
injectfy.registerFactory(() => ClientsRepositoryImpl());
// Resolving dependencies
final someService = injectfy.get();
final clientsRepository = injectfy.get();
print(someService); // Instance of SomeService
print(clientsRepository); // Instance of ClientsRepositoryImpl
}
```
### ๐งฑ Registering a Singleton
A singleton will be instantiated only once and reused every time it's requested.
```dart
class SomeService {
void doSomething() => print("Doing something...");
}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService());
final service1 = Injectfy.get();
final service2 = Injectfy.get();
print(identical(service1, service2)); // Output: true
}
```
### ๐ข Registering a Factory
A factory allows creating a new instance of the dependency every time it is requested.
```dart
class UserRepository {}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerFactory(() => UserRepository());
final repo1 = Injectfy.get();
final repo2 = Injectfy.get();
print(identical(repo1, repo2)); // Output: false
}
```
### ๐ง Automatically Resolving Dependencies
You can automatically resolve dependencies when calling a method.
```dart
class SomeService {
final SomeDependency _someDependency;
SomeService(this._someDependency);
}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService(injectfy()));
}
```
### ๐ ๏ธ Unregistering a Dependency
You can unregister a previously registered dependency if it's no longer needed.
```dart
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService());
injectfy.unregister();
try {
final service = Injectfy.get();
} catch (e) {
print(e); // Dependency of type SomeService not found.
}
}
```
### ๐งช Registering Mock Dependencies (Useful for Testing)
For testing purposes, you can register mock objects in place of actual dependencies.
```dart
void main() {
final injectfy = Injectfy.instance;
final mockService = SomeService();
injectfy.registerMock(mockService);
final service = Injectfy.get();
print(service == mockService); // Output: true
}
```
## ๐๏ธ API Reference
### ๐ ๏ธ `Injectfy`
| Method | Description |
| ---------------------- | -------------------------------------------------------------------------- |
| `instance` | Get a singleton instance of Injectfy. |
| `I` | Short syntax to get the instance. |
| `registerSingleton` | Registers a singleton for the specified type `T`. |
| `registerFactory` | Registers a factory for the specified type `T`. |
| `get` | Resolves and returns the instance of type `T`. |
| `call` | Automatically resolves and returns the instance of type `T`. |
| `unregister` | Unregisters the dependency of type `T`. |
| `registerMock` | Registers a mock object in place of a real dependency. Useful for testing. |
## ๐งช Testing
You can test your application by registering mock dependencies for testing purposes.
### Example of Testing with Mocks
```dart
import 'package:test/test.dart';
void main() {
test('Singleton works correctly', () {
Injectfy.I.registerSingleton(() => SomeService());
final service1 = Injectfy.get();
final service2 = Injectfy.get();
expect(identical(service1, service2), true);
});
test('Factory works correctly', () {
Injectfy.I.registerFactory(() => UserRepository());
final repo1 = Injectfy.get();
final repo2 = Injectfy.get();
expect(identical(repo1, repo2), false);
});
test('Mock registration works correctly', () {
final mockService = SomeService();
Injectfy.I.registerMock(mockService);
final service = Injectfy.get();
expect(service, mockService);
});
}
```
## ๐งฑ Contributing
Contributions are welcome! Please open issues or submit pull requests on the GitHub repository.
## ๐ License
This library is licensed under the MIT License. See the LICENSE file for details.
# ๐ Injectfy: A Simple Dependency Injection Library for Dart
A minimalistic and easy-to-use dependency injection library for managing singletons and factories in Dart.
## ๐ Features
- โ
Register singletons and factories to manage dependencies.
- ๐ Automatically resolve and inject dependencies.
- โ Easily unregister dependencies.
- โก Cache frequently accessed instances for better performance.
- ๐ ๏ธ Useful for both production and testing (with mock support).
## ๐ฆ Installation
Add the following dependency to your `pubspec.yaml`:
```yaml
dependencies:
injectfy: ^1.0.0
```
Then, run:
```bash
flutter pub get
```
## ๐ Getting Started
### ๐ Basic Usage
```dart
import 'package:injectfy/injectfy.dart';
void main() {
// Get instance
final injectfy = Injectfy.instance;
// Registering a singleton
injectfy.registerSingleton(() => SomeService());
// Registering a factory
injectfy.registerFactory(() => ClientsRepositoryImpl());
// Resolving dependencies
final someService = injectfy.get();
final clientsRepository = injectfy.get();
print(someService); // Instance of SomeService
print(clientsRepository); // Instance of ClientsRepositoryImpl
}
```
### ๐งฑ Registering a Singleton
A singleton will be instantiated only once and reused every time it's requested.
```dart
class SomeService {
void doSomething() => print("Doing something...");
}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService());
final service1 = Injectfy.get();
final service2 = Injectfy.get();
print(identical(service1, service2)); // Output: true
}
```
### ๐ข Registering a Factory
A factory allows creating a new instance of the dependency every time it is requested.
```dart
class UserRepository {}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerFactory(() => UserRepository());
final repo1 = Injectfy.get();
final repo2 = Injectfy.get();
print(identical(repo1, repo2)); // Output: false
}
```
### ๐ง Automatically Resolving Dependencies
You can automatically resolve dependencies when calling a method.
```dart
class SomeService {
final SomeDependency _someDependency;
SomeService(this._someDependency);
}
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService(injectfy()));
}
```
### ๐ ๏ธ Unregistering a Dependency
You can unregister a previously registered dependency if it's no longer needed.
```dart
void main() {
final injectfy = Injectfy.instance;
injectfy.registerSingleton(() => SomeService());
injectfy.unregister();
try {
final service = Injectfy.get();
} catch (e) {
print(e); // Dependency of type SomeService not found.
}
}
```
### ๐งช Registering Mock Dependencies (Useful for Testing)
For testing purposes, you can register mock objects in place of actual dependencies.
```dart
void main() {
final injectfy = Injectfy.instance;
final mockService = SomeService();
injectfy.registerMock(mockService);
final service = Injectfy.get();
print(service == mockService); // Output: true
}
```
## ๐๏ธ API Reference
### ๐ ๏ธ `Injectfy`
| Method | Description |
| ---------------------- | -------------------------------------------------------------------------- |
| `instance` | Get a singleton instance of Injectfy. |
| `I` | Short syntax to get the instance. |
| `registerSingleton` | Registers a singleton for the specified type `T`. |
| `registerFactory` | Registers a factory for the specified type `T`. |
| `get` | Resolves and returns the instance of type `T`. |
| `call` | Automatically resolves and returns the instance of type `T`. |
| `unregister` | Unregisters the dependency of type `T`. |
| `registerMock` | Registers a mock object in place of a real dependency. Useful for testing. |
## ๐งช Testing
You can test your application by registering mock dependencies for testing purposes.
### Example of Testing with Mocks
```dart
import 'package:test/test.dart';
void main() {
test('Singleton works correctly', () {
Injectfy.I.registerSingleton(() => SomeService());
final service1 = Injectfy.get();
final service2 = Injectfy.get();
expect(identical(service1, service2), true);
});
test('Factory works correctly', () {
Injectfy.I.registerFactory(() => UserRepository());
final repo1 = Injectfy.get();
final repo2 = Injectfy.get();
expect(identical(repo1, repo2), false);
});
test('Mock registration works correctly', () {
final mockService = SomeService();
Injectfy.I.registerMock(mockService);
final service = Injectfy.get();
expect(service, mockService);
});
}
```
## ๐งฑ Contributing
Contributions are welcome! Please open issues or submit pull requests on the GitHub repository.
## ๐ License
This library is licensed under the MIT License. See the LICENSE file for details.
---
Made with โค๏ธ for Flutter developers! ๐ฏ