|
1 | | -<!-- |
2 | | -This README describes the package. If you publish this package to pub.dev, |
3 | | -this README's contents appear on the landing page for your package. |
| 1 | +# nest_core |
4 | 2 |
|
5 | | -For information about how to write a good package README, see the guide for |
6 | | -[writing package pages](https://dart.dev/tools/pub/writing-package-pages). |
| 3 | +Core dependency injection and module system for Nest Dart - a NestJS-inspired framework for Dart applications. |
7 | 4 |
|
8 | | -For general information about developing packages, see the Dart guide for |
9 | | -[creating packages](https://dart.dev/guides/libraries/create-packages) |
10 | | -and the Flutter guide for |
11 | | -[developing packages and plugins](https://flutter.dev/to/develop-packages). |
12 | | ---> |
| 5 | +## Features |
13 | 6 |
|
14 | | -TODO: Put a short description of the package here that helps potential users |
15 | | -know whether this package might be useful for them. |
| 7 | +- 🏗️ **Modular Architecture** - Organize code into reusable, self-contained modules |
| 8 | +- 💉 **Dependency Injection** - Type-safe service resolution with access control |
| 9 | +- 🔄 **Lifecycle Management** - Module initialization and cleanup hooks |
| 10 | +- 🔒 **Export Control** - Services are private by default, must be explicitly exported |
| 11 | +- ⚡ **Performance** - Minimal overhead with compile-time safety |
16 | 12 |
|
17 | | -## Features |
| 13 | +## Installation |
| 14 | + |
| 15 | +Add `nest_core` to your `pubspec.yaml`: |
| 16 | + |
| 17 | +```yaml |
| 18 | +dependencies: |
| 19 | + nest_core: ^0.1.0 |
| 20 | +``` |
| 21 | +
|
| 22 | +## Quick Start |
| 23 | +
|
| 24 | +### 1. Create a Service |
| 25 | +
|
| 26 | +```dart |
| 27 | +class LoggerService { |
| 28 | + void log(String message) { |
| 29 | + print('[LOG] ${DateTime.now()}: $message'); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Create a Module |
| 35 | + |
| 36 | +```dart |
| 37 | +import 'package:nest_core/nest_core.dart'; |
| 38 | +
|
| 39 | +class CoreModule extends Module { |
| 40 | + @override |
| 41 | + void providers(Locator locator) { |
| 42 | + locator.registerSingleton<LoggerService>(LoggerService()); |
| 43 | + } |
| 44 | + |
| 45 | + @override |
| 46 | + List<Type> get exports => [LoggerService]; |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### 3. Initialize Application |
| 51 | + |
| 52 | +```dart |
| 53 | +void main() async { |
| 54 | + final container = ApplicationContainer(); |
| 55 | + await container.registerModule(CoreModule()); |
| 56 | + |
| 57 | + final logger = container.get<LoggerService>(); |
| 58 | + logger.log('Application started!'); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Core Concepts |
| 63 | + |
| 64 | +### ApplicationContainer |
18 | 65 |
|
19 | | -TODO: List what your package can do. Maybe include images, gifs, or videos. |
| 66 | +The central container that manages modules and services: |
20 | 67 |
|
21 | | -## Getting started |
| 68 | +```dart |
| 69 | +final container = ApplicationContainer(); |
| 70 | +await container.registerModule(AppModule()); |
| 71 | +
|
| 72 | +// Get services |
| 73 | +final userService = container.get<UserService>(); |
22 | 74 |
|
23 | | -TODO: List prerequisites and provide or point to information on how to |
24 | | -start using the package. |
| 75 | +// Check if ready |
| 76 | +if (container.isReady) { |
| 77 | + print('Container initialized'); |
| 78 | +} |
| 79 | +``` |
25 | 80 |
|
26 | | -## Usage |
| 81 | +### Module System |
27 | 82 |
|
28 | | -TODO: Include short and useful examples for package users. Add longer examples |
29 | | -to `/example` folder. |
| 83 | +Modules group related services and define dependencies: |
30 | 84 |
|
31 | 85 | ```dart |
32 | | -const like = 'sample'; |
| 86 | +class UserModule extends Module { |
| 87 | + @override |
| 88 | + List<Module> get imports => [CoreModule()]; |
| 89 | + |
| 90 | + @override |
| 91 | + void providers(Locator locator) { |
| 92 | + locator.registerSingleton<UserService>( |
| 93 | + UserService(locator.get<LoggerService>()), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + @override |
| 98 | + List<Type> get exports => [UserService]; |
| 99 | +} |
33 | 100 | ``` |
34 | 101 |
|
35 | | -## Additional information |
| 102 | +### Service Registration |
| 103 | + |
| 104 | +Multiple registration patterns: |
| 105 | + |
| 106 | +```dart |
| 107 | +// Singleton (created once) |
| 108 | +locator.registerSingleton<UserService>(UserService()); |
| 109 | +
|
| 110 | +// Factory (new instance each time) |
| 111 | +locator.registerFactory<UserService>(() => UserService()); |
| 112 | +
|
| 113 | +// Lazy Singleton (created on first access) |
| 114 | +locator.registerLazySingleton<UserService>(() => UserService()); |
| 115 | +``` |
| 116 | + |
| 117 | +### Lifecycle Hooks |
| 118 | + |
| 119 | +Initialize and cleanup resources: |
| 120 | + |
| 121 | +```dart |
| 122 | +class DatabaseModule extends Module { |
| 123 | + @override |
| 124 | + Future<void> onModuleInit(Locator locator, ModuleContext context) async { |
| 125 | + final db = locator.get<DatabaseService>(); |
| 126 | + await db.connect(); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Future<void> onModuleDestroy(Locator locator, ModuleContext context) async { |
| 131 | + final db = locator.get<DatabaseService>(); |
| 132 | + await db.disconnect(); |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Platform Integrations |
| 138 | + |
| 139 | +- **[nest_flutter](https://pub.dev/packages/nest_flutter)** - Flutter integration with widget support |
| 140 | +- **[nest_frog](https://pub.dev/packages/nest_frog)** - Dart Frog backend integration |
| 141 | + |
| 142 | +## Documentation |
| 143 | + |
| 144 | +- [Getting Started Guide](https://chornthorn.github.io/nest-dart/getting-started) |
| 145 | +- [API Reference](https://chornthorn.github.io/nest-dart/api-reference) |
| 146 | +- [Examples](https://chornthorn.github.io/nest-dart/examples) |
| 147 | + |
| 148 | +## Contributing |
| 149 | + |
| 150 | +We welcome contributions! Please see our [Contributing Guide](https://github.com/chornthorn/nest-dart/blob/main/CONTRIBUTING.md) for details. |
| 151 | + |
| 152 | +## License |
36 | 153 |
|
37 | | -TODO: Tell users more about the package: where to find more information, how to |
38 | | -contribute to the package, how to file issues, what response they can expect |
39 | | -from the package authors, and more. |
| 154 | +This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details. |
0 commit comments