diff --git a/README.md b/README.md index 875b1e5c..ae68a737 100644 --- a/README.md +++ b/README.md @@ -51,11 +51,11 @@ $ dart create -t console your_project_name ```yaml dependencies: - dartness_server: ^0.5.0-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: build_runner: ^2.2.0 - dartness_generator: ^0.4.6-alpha + dartness_generator: ^0.5.2-alpha ``` diff --git a/docs/README.md b/docs/README.md index 875b1e5c..ae68a737 100644 --- a/docs/README.md +++ b/docs/README.md @@ -51,11 +51,11 @@ $ dart create -t console your_project_name ```yaml dependencies: - dartness_server: ^0.5.0-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: build_runner: ^2.2.0 - dartness_generator: ^0.4.6-alpha + dartness_generator: ^0.5.2-alpha ``` diff --git a/docs/_sidebar.md b/docs/_sidebar.md index dcdee567..cf6195ab 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -3,6 +3,8 @@ - Overview - [First steps](first_steps.md) - [Controllers](controllers.md) + - [Providers](providers.md) + - [Modules](modules.md) - [Middlewares](middleware.md) - [Interceptors](interceptor.md) - [Exceptions](exceptions.md) diff --git a/docs/controllers.md b/docs/controllers.md index 17a1d865..a308ecd5 100644 --- a/docs/controllers.md +++ b/docs/controllers.md @@ -149,7 +149,7 @@ part 'city_controller.g.dart'; @Controller('/cities') class CitiesController { @Get('/') - String findById(@PathParam() id) { + String findById(@PathParam() int id) { return 'This action returns a city with id: $id'; } } diff --git a/docs/custom_providers.md b/docs/custom_providers.md new file mode 100644 index 00000000..36c0aff7 --- /dev/null +++ b/docs/custom_providers.md @@ -0,0 +1,110 @@ +# Custom providers + +Custom providers typically refer to services or objects that you create and configure yourself to be used within your +application. These providers are not part of the core `Dartness` framework or provided by any third-party libraries; +instead, you define them to suit your specific needs. Custom providers can include services, repositories, factories, or +any other objects that your application requires for its functionality. + +Custom providers are an essential part of the Dartness dependency injection system, which allows you to manage and +inject dependencies throughout your application. By creating custom providers, you can decouple various parts of your +application and make it more modular and maintainable. + +## Standard providers + +Let's take a closer look at the `ModuleMetadata` class. In `app.dart`, we declare in our section [providers]: + +```dart +@Application( + module: Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: CitiesController, + ), + ], + providers: [ + ProviderMetadata( + classType: CitiesService, + ), + ], + ), + ), +) +class App {} +``` + +The providers attribute takes an array of providers. We've supplied those providers via a list of `ProviderMetadata` +class that we can specify by the attribute `classType` the class that we want to have as provider. + +## Custom providers + +What happens when your requirements go beyond those offered by Standard providers? Here are a few examples. + +### Factory providers + +The `useFactory` syntax allows for creating providers dynamically. The actual provider will be supplied by the value +returned from a factory function. The factory function can be as simple or complex as needed. A simple factory may not +depend on any other providers. A more complex factory can itself inject other providers it needs to compute its result. +For the latter case, the factory provider syntax has a pair of related mechanisms: + +```dart + +Dio createDio() => Dio(); + +CitiesService createCitiesService(Dio dio) => CitiesService(dio); + +@Application( + module: Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: CitiesController, + ), + ], + providers: [ + ProviderMetadata( + classType: Dio, + useFactory: createDio, + ), + ProviderMetadata( + classType: CitiesService, + useFactory: createCitiesService, + ), + ], + ), + ), +) +class App {} +``` + +> **_NOTE:_** As current limitations of `dart` language, it is not allow to use `const` `Function` as part of the +> attribute, in order to avoid it you need to declare the `Function` as global as we saw in the previous example. + +**BAD!!!** + +```dart +@Application( + module: Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: CitiesController, + ), + ], + providers: [ + ProviderMetadata( + classType: Dio, + // this is not going to work + useFactory: () => Dio(), + ), + ProviderMetadata( + classType: CitiesService, + // this is not going to work + useFactory: (Dio dio) => CitiesService(dio), + ), + ], + ), + ), +) +class App {} +``` \ No newline at end of file diff --git a/docs/first_steps.md b/docs/first_steps.md index ce558259..6afd2eeb 100644 --- a/docs/first_steps.md +++ b/docs/first_steps.md @@ -20,11 +20,11 @@ $ dart create -t console your_project_name ```yaml dependencies: - dartness_server: ^0.5.0-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: build_runner: ^2.2.0 - dartness_generator: ^0.4.6-alpha + dartness_generator: ^0.5.2-alpha ``` ### 2. Create the file in "bin/app.dart" or whatever file that you consider that you root application is. diff --git a/docs/modules.md b/docs/modules.md new file mode 100644 index 00000000..cc0cfabe --- /dev/null +++ b/docs/modules.md @@ -0,0 +1,80 @@ +# Modules + +A module is a class that provides metadata that Dartness makes use +of to organize the application structure. + +Each application has at least one module, a root module. The root module is the starting point Dartness uses to build +the application graph - the internal data structure Dartness uses to resolve module and provider relationships and +dependencies. While very small applications may theoretically have just the root module, this is not the typical case. +We want to emphasize that modules are strongly recommended as an effective way to organize your components. Thus, for +most applications, the resulting architecture will employ multiple modules, each encapsulating a closely related set of +capabilities. + +The `Module` class has properties to describe the module: + +| Attribute | Description | +|--------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `providers` | the providers that will be instantiated by the Nest injector and that may be shared at least across this module | +| `controllers` | the set of controllers defined in this module which have to be instantiated | +| `imports` | the list of imported modules that export the providers which are required in this module | +| `exports` (under construction) | the subset of providers that are provided by this module and should be available in other modules which import this module. You can use either the provider itself or just its token (provide value) | + +## Feature modules + +The `CitiesController` and `CitiesService` belong to the same application domain. As they are closely related, it makes +sense to move them into a feature module. A feature module simply organizes code relevant for a specific feature, +keeping code organized and establishing clear boundaries. This helps us manage complexity and develop with SOLID +principles, especially as the size of the application and/or team grow. + +To demonstrate this, we'll create the `CitiesModule`. + +```dart + +const cityModule = Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: CityController, + ), + ], + providers: [ + ProviderMetadata( + classType: CityService, + ), + ], + ), +); +``` + +Above, we defined the `CitiesModule` in the `cities_module.dart` file, and moved everything related to this module into +the cities' directory. The last thing we need to do is import this module into the root module (the AppModule, defined +in the `app.dart` file). + +```dart +import 'cities/city_module.dart'; + +@Application( + module: Module( + metadata: ModuleMetadata( + imports: [cityModule], + ), + ), +) +class App {} +``` + +Here is how our directory structure looks now: + +``` +├── src +│ ├── cities +│ │ ├── cities_service.dart +│ │ ├── cities_service.g.dart +│ │ ├── cities_controller.dart +│ │ ├── cities_controller.g.dart +│ │ ├── cities_module.dart +│ ├── app.dart +│ ├── app.g.dart +``` + +In this example our module is global since we declared it in a global variable. \ No newline at end of file diff --git a/docs/providers.md b/docs/providers.md new file mode 100644 index 00000000..5f1ed2e8 --- /dev/null +++ b/docs/providers.md @@ -0,0 +1,83 @@ +# Providers + +Providers refer to a core concept related to dependency injection. Providers are responsible for creating and managing +various dependencies and services that your application needs. These dependencies can include things like services, +repositories, configuration settings, database connections, and more. Dartness uses providers as a fundamental building +block for organizing and maintaining the components of your application. + +## Services + +To begin, we can create a basic `CitiesService` that will handle data storage and retrieval. This service is intended to +be utilized by the `CitiesController` and is a suitable candidate for registration as a provider. + +```dart +class CitiesService { + final List cities = []; + + void create(City city) { + cities.add(city); + } + + List findAll() { + return cities; + } +} +``` + +The `CitiesService` is a straightforward class with a single property and two methods. Currently, there is no need to +indicate that the class should be injectable. + +Now that we have a service class to retrieve cities, let's use it inside the `CitiesController`: + +```dart +@Controller('cities') +class CatsController { + final CitiesService _citiesService; + + CatsController(this._citiesService); + + @Post() + void create(@Body() City city) { + catsService.create(city); + } + + @Get() + List findAll() { + return catsService.findAll(); + } +} +``` + +The `CitiesService` is injected through the class constructor. + +# Provider registration + +Now that we have defined a provider (`CitiesService`), and we have a consumer of that service (`CitiesController`), we +need to +register the service with Dartness so that it can perform the injection. We do this by editing our module file ( +app.dart) and adding the service to the `providers` attribute of the `@Application()` annotation using +the `ModuleMetadata` class in order to structure the metadata and the `ProviderMetadata` class to structure our +provider. + +```dart +@Application( + module: Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: CitiesController, + ), + ], + providers: [ + ProviderMetadata( + classType: CitiesService, + ), + ], + ), + ), +) +class App {} +``` + +Dartness will now be able to resolve the dependencies of the `CitiesController` class. + diff --git a/docs/quickstart.md b/docs/quickstart.md index 31a7d96e..154d998e 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -29,30 +29,15 @@ $ dart create -t console your_project_name ```yaml dependencies: - dartness_server: ^0.5.0-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: - dartness_generator: ^0.4.6-alpha + dartness_generator: ^0.5.2-alpha ``` ### 2.Create the file in "src/app.dart" ```dart -@Application( - module: Module( - metadata: ModuleMetadata( - controllers: [], - providers: [], - exports: [], - imports: [], - ), - ), - options: DartnessApplicationOptions( - port: int.fromEnvironment( - 'port', - defaultValue: 8080, - ), - ), -) +@Application() class App {} ``` diff --git a/examples/dartness_flutter_melos/README.md b/examples/dartness_flutter_melos/README.md index 77fd5523..cfe72713 100644 --- a/examples/dartness_flutter_melos/README.md +++ b/examples/dartness_flutter_melos/README.md @@ -132,7 +132,7 @@ $ dart run servers/my_server/bin/my_server.dart #### Run Flutter ```bash -$ flutter run apps/my_app/lib/main.dart +$ flutter run apps/my_app/lib/app.dart ``` ## Conclusion diff --git a/examples/dartness_flutter_melos/apps/my_app/README.md b/examples/dartness_flutter_melos/apps/my_app/README.md index e22b7c46..217b433b 100644 --- a/examples/dartness_flutter_melos/apps/my_app/README.md +++ b/examples/dartness_flutter_melos/apps/my_app/README.md @@ -5,5 +5,5 @@ Flutter app with Dartness as backend example. ## Run the app ```bash -$ flutter run lib/main.dart +$ flutter run lib/app.dart ``` \ No newline at end of file diff --git a/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml b/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml index d580a277..aedb2c16 100644 --- a/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml +++ b/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml @@ -17,7 +17,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.3 + flutter_lints: ^3.0.0 flutter: uses-material-design: true diff --git a/examples/dartness_flutter_melos/pubspec.yaml b/examples/dartness_flutter_melos/pubspec.yaml new file mode 100644 index 00000000..b76ed56d --- /dev/null +++ b/examples/dartness_flutter_melos/pubspec.yaml @@ -0,0 +1,13 @@ +name: dartness_flutter_melos +description: Dartness flutter melos +version: 1.0.0 +homepage: https://ricardorb.github.io/dartness/ +repository: https://github.com/RicardoRB/dartness +issue_tracker: https://github.com/RicardoRB/dartness/issues +documentation: https://ricardorb.github.io/dartness/ + +environment: + sdk: ">=3.0.0 <4.0.0" + +dependencies: + melos: ^3.2.0 diff --git a/examples/dartness_flutter_melos/servers/my_server/bin/my_server.dart b/examples/dartness_flutter_melos/servers/my_server/bin/my_server.dart index 56b32c73..f1d40f44 100644 --- a/examples/dartness_flutter_melos/servers/my_server/bin/my_server.dart +++ b/examples/dartness_flutter_melos/servers/my_server/bin/my_server.dart @@ -1,5 +1,5 @@ -import 'package:my_server/main.dart' as my_server; +import 'package:my_server/app.dart'; -void main(List arguments) { - my_server.main(); +void main(List arguments) async { + await App().init(); } diff --git a/examples/dartness_flutter_melos/servers/my_server/lib/app.dart b/examples/dartness_flutter_melos/servers/my_server/lib/app.dart new file mode 100644 index 00000000..075a210b --- /dev/null +++ b/examples/dartness_flutter_melos/servers/my_server/lib/app.dart @@ -0,0 +1,26 @@ +import 'package:dartness_server/dartness.dart'; +import 'package:dartness_server/modules.dart'; +import 'package:dartness_server/server.dart'; + +import 'example_controller.dart'; + +part 'app.g.dart'; + +@Application( + module: Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: ExampleController, + ), + ], + ), + ), + options: DartnessApplicationOptions( + port: int.fromEnvironment( + 'port', + defaultValue: 3000, + ), + ), +) +class App {} diff --git a/examples/dartness_flutter_melos/servers/my_server/lib/app.g.dart b/examples/dartness_flutter_melos/servers/my_server/lib/app.g.dart new file mode 100644 index 00000000..f3f4f98c --- /dev/null +++ b/examples/dartness_flutter_melos/servers/my_server/lib/app.g.dart @@ -0,0 +1,29 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'app.dart'; + +// ************************************************************************** +// ApplicationGenerator +// ************************************************************************** + +extension AppExtension on App { + initDependencies() { + final injectRegister = InstanceRegister.instance; + injectRegister.register(ExampleController()); + } + + Future init() async { + initDependencies(); + final injectRegister = InstanceRegister.instance; + final app = Dartness(); + await app.create( + controllers: [ + ExampleDartnessController(injectRegister.resolve()), + ], + options: DartnessApplicationOptions( + logRequest: false, + port: 3000, + ), + ); + } +} diff --git a/examples/dartness_flutter_melos/servers/my_server/lib/example_controller.g.dart b/examples/dartness_flutter_melos/servers/my_server/lib/example_controller.g.dart index 13313fca..6a81166a 100644 --- a/examples/dartness_flutter_melos/servers/my_server/lib/example_controller.g.dart +++ b/examples/dartness_flutter_melos/servers/my_server/lib/example_controller.g.dart @@ -9,13 +9,22 @@ part of 'example_controller.dart'; extension ExampleControllerRoutes on ExampleController { List getRoutes() { final routes = []; - routes.add(ControllerRoute('GET', '/hello/world', getHelloWorld, [], - httpCode: null, headers: {})); + routes.add(ControllerRoute( + method: 'GET', + path: '/hello/world', + handler: getHelloWorld, + params: [], + httpCode: null, + headers: {}, + )); return routes; } } class ExampleDartnessController extends DartnessController { ExampleDartnessController(ExampleController controller) - : super(controller, controller.getRoutes()); + : super( + controller, + controller.getRoutes(), + ); } diff --git a/examples/dartness_flutter_melos/servers/my_server/lib/main.dart b/examples/dartness_flutter_melos/servers/my_server/lib/main.dart deleted file mode 100644 index 006d7223..00000000 --- a/examples/dartness_flutter_melos/servers/my_server/lib/main.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:dartness_server/dartness.dart'; - -import 'example_controller.dart'; - -void main() async { - final app = Dartness( - port: 3000, - controllers: [ - ExampleDartnessController(ExampleController()), - ], - ); - await app.create(); -} diff --git a/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml b/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml index 4da1e0e6..f026e8a7 100644 --- a/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml +++ b/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml @@ -9,10 +9,10 @@ environment: sdk: ">=3.0.0 <4.0.0" dependencies: - dartness_server: ^0.4.6-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: - lints: ^2.1.1 - test: ^1.24.7 + lints: ^3.0.0 + test: ^1.24.9 build_runner: ^2.4.6 - dartness_generator: ^0.1.3-alpha + dartness_generator: ^0.5.1-alpha diff --git a/examples/dartness_simple/lib/app_module.dart b/examples/dartness_simple/lib/app_module.dart deleted file mode 100644 index 5b001e87..00000000 --- a/examples/dartness_simple/lib/app_module.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:dartness_server/modules.dart'; -import 'package:example/src/services/city_service.dart'; - -import 'src/controllers/city/city_controller.dart'; - -@Module( - metadata: ModuleMetadata( - controllers: [ - ProviderMetadata( - classType: CityController, - ), - ], - providers: [ - ProviderMetadata( - classType: CityService, - ), - ], - ), -) -class AppModule {} diff --git a/examples/dartness_simple/lib/src/app.dart b/examples/dartness_simple/lib/src/app.dart index 3caf66fc..49e3ab49 100644 --- a/examples/dartness_simple/lib/src/app.dart +++ b/examples/dartness_simple/lib/src/app.dart @@ -1,34 +1,39 @@ import 'package:dartness_server/dartness.dart'; import 'package:dartness_server/modules.dart'; import 'package:dartness_server/server.dart'; -import 'package:example/src/controllers/city/city_module.dart'; -import 'package:example/src/controllers/user/user_module.dart'; -import 'package:example/src/services/city_service.dart'; +import 'package:dio/dio.dart'; -import 'controllers/city/city_controller.dart'; -import 'controllers/health_controller.dart'; -import 'controllers/user/user_controller.dart'; +import 'controllers/city_controller.dart'; +import 'controllers/todos_controller.dart'; +import 'controllers/user_controller.dart'; import 'error_handlers/example_error_handler.dart'; +import 'modules/city_module.dart'; +import 'modules/todos_module.dart'; +import 'modules/user_module.dart'; +import 'services/city_service.dart'; +import 'services/todos_service.dart'; part 'app.g.dart'; +Dio createDio() => Dio(); + @Application( module: Module( metadata: ModuleMetadata( - controllers: [ - ProviderMetadata( - classType: HealthController, - ), - ], providers: [ ProviderMetadata( classType: ExampleErrorHandler, ), + ProviderMetadata( + classType: Dio, + useFactory: createDio, + ), ], exports: [], imports: [ userModule, cityModule, + todosModule, ], ), ), diff --git a/examples/dartness_simple/lib/src/app.g.dart b/examples/dartness_simple/lib/src/app.g.dart index 4f5c5dea..0fc08d57 100644 --- a/examples/dartness_simple/lib/src/app.g.dart +++ b/examples/dartness_simple/lib/src/app.g.dart @@ -11,10 +11,18 @@ extension AppExtension on App { final injectRegister = InstanceRegister.instance; injectRegister.register(UserController()); injectRegister.register(CityService()); - injectRegister.register(CityController( - injectRegister.resolve(), - )); - injectRegister.register(HealthController()); + injectRegister.register(CityService(), name: "CITY_SECOND"); + injectRegister.register( + CityController(injectRegister.resolve( + name: "CITY_SECOND", + ))); + final createDioResult = Function.apply(createDio, []); + injectRegister.register(createDioResult); + final createTodosServiceResult = + Function.apply(createTodosService, [injectRegister.resolve()]); + injectRegister.register(createTodosServiceResult); + injectRegister.register( + TodosController(injectRegister.resolve())); injectRegister.register(ExampleErrorHandler()); } @@ -26,7 +34,7 @@ extension AppExtension on App { controllers: [ UserDartnessController(injectRegister.resolve()), CityDartnessController(injectRegister.resolve()), - HealthDartnessController(injectRegister.resolve()), + TodosDartnessController(injectRegister.resolve()), ], options: DartnessApplicationOptions( logRequest: false, diff --git a/examples/dartness_simple/lib/src/controllers/city/city_controller.dart b/examples/dartness_simple/lib/src/controllers/city_controller.dart similarity index 72% rename from examples/dartness_simple/lib/src/controllers/city/city_controller.dart rename to examples/dartness_simple/lib/src/controllers/city_controller.dart index ca6f03ab..4fbd7212 100644 --- a/examples/dartness_simple/lib/src/controllers/city/city_controller.dart +++ b/examples/dartness_simple/lib/src/controllers/city_controller.dart @@ -1,16 +1,17 @@ import 'dart:io'; +import 'package:dartness_server/dartness.dart'; import 'package:dartness_server/route.dart'; -import '../../dtos/city_dto.dart'; -import '../../services/city_service.dart'; +import '../dtos/city_dto.dart'; +import '../services/city_service.dart'; part 'city_controller.g.dart'; @Controller('/cities') @Header(HttpHeaders.contentTypeHeader, 'application/json') class CityController { - CityController(this._cityService); + CityController(@Inject('CITY_SECOND') this._cityService); final CityService _cityService; diff --git a/examples/dartness_simple/lib/src/controllers/city/city_controller.g.dart b/examples/dartness_simple/lib/src/controllers/city_controller.g.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/city/city_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/city_controller.g.dart diff --git a/examples/dartness_simple/lib/src/controllers/health_controller.dart b/examples/dartness_simple/lib/src/controllers/health_controller.dart deleted file mode 100644 index 5fd59739..00000000 --- a/examples/dartness_simple/lib/src/controllers/health_controller.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'dart:io'; - -import 'package:dartness_server/route.dart'; - -part 'health_controller.g.dart'; - -@Controller('/health') -@Header(HttpHeaders.contentTypeHeader, 'application/json') -class HealthController { - @HttpCode(202) - @Get() - bool getAlive() { - return true; - } -} diff --git a/examples/dartness_simple/lib/src/controllers/todos_controller.dart b/examples/dartness_simple/lib/src/controllers/todos_controller.dart new file mode 100644 index 00000000..347f26d5 --- /dev/null +++ b/examples/dartness_simple/lib/src/controllers/todos_controller.dart @@ -0,0 +1,19 @@ +import 'dart:io'; + +import 'package:dartness_server/route.dart'; +import 'package:example/src/services/todos_service.dart'; + +part 'todos_controller.g.dart'; + +@Controller('/todos') +@Header(HttpHeaders.contentTypeHeader, 'application/json') +class TodosController { + final TodosService _todoService; + + TodosController(this._todoService); + + @Get() + Future> getAlive() { + return _todoService.getTodos(); + } +} diff --git a/examples/dartness_simple/lib/src/controllers/health_controller.g.dart b/examples/dartness_simple/lib/src/controllers/todos_controller.g.dart similarity index 69% rename from examples/dartness_simple/lib/src/controllers/health_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/todos_controller.g.dart index 26528a21..e1161f16 100644 --- a/examples/dartness_simple/lib/src/controllers/health_controller.g.dart +++ b/examples/dartness_simple/lib/src/controllers/todos_controller.g.dart @@ -1,28 +1,28 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'health_controller.dart'; +part of 'todos_controller.dart'; // ************************************************************************** // ControllerGenerator // ************************************************************************** -extension HealthControllerRoutes on HealthController { +extension TodosControllerRoutes on TodosController { List getRoutes() { final routes = []; routes.add(ControllerRoute( method: 'GET', - path: '/health', + path: '/todos', handler: getAlive, params: [], - httpCode: 202, + httpCode: null, headers: {'content-type': 'application/json'}, )); return routes; } } -class HealthDartnessController extends DartnessController { - HealthDartnessController(HealthController controller) +class TodosDartnessController extends DartnessController { + TodosDartnessController(TodosController controller) : super( controller, controller.getRoutes(), diff --git a/examples/dartness_simple/lib/src/controllers/user/user_controller.dart b/examples/dartness_simple/lib/src/controllers/user_controller.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/user/user_controller.dart rename to examples/dartness_simple/lib/src/controllers/user_controller.dart diff --git a/examples/dartness_simple/lib/src/controllers/user/user_controller.g.dart b/examples/dartness_simple/lib/src/controllers/user_controller.g.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/user/user_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/user_controller.g.dart diff --git a/examples/dartness_simple/lib/src/controllers/city/city_module.dart b/examples/dartness_simple/lib/src/modules/city_module.dart similarity index 62% rename from examples/dartness_simple/lib/src/controllers/city/city_module.dart rename to examples/dartness_simple/lib/src/modules/city_module.dart index 7db0e2cd..4942a54f 100644 --- a/examples/dartness_simple/lib/src/controllers/city/city_module.dart +++ b/examples/dartness_simple/lib/src/modules/city_module.dart @@ -1,7 +1,7 @@ import 'package:dartness_server/modules.dart'; -import '../../services/city_service.dart'; -import 'city_controller.dart'; +import '../controllers/city_controller.dart'; +import '../services/city_service.dart'; const cityModule = Module( metadata: ModuleMetadata( @@ -14,6 +14,10 @@ const cityModule = Module( ProviderMetadata( classType: CityService, ), + ProviderMetadata( + classType: CityService, + name: 'CITY_SECOND', + ), ], ), ); diff --git a/examples/dartness_simple/lib/src/modules/todos_module.dart b/examples/dartness_simple/lib/src/modules/todos_module.dart new file mode 100644 index 00000000..5a266622 --- /dev/null +++ b/examples/dartness_simple/lib/src/modules/todos_module.dart @@ -0,0 +1,23 @@ +import 'package:dartness_server/modules.dart'; +import 'package:dio/dio.dart'; + +import '../controllers/todos_controller.dart'; +import '../services/todos_service.dart'; + +TodosService createTodosService(Dio dio) => TodosService(dio); + +const todosModule = Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: TodosController, + ), + ], + providers: [ + ProviderMetadata( + classType: TodosService, + useFactory: createTodosService, + ), + ], + ), +); diff --git a/examples/dartness_simple/lib/src/controllers/user/user_module.dart b/examples/dartness_simple/lib/src/modules/user_module.dart similarity index 81% rename from examples/dartness_simple/lib/src/controllers/user/user_module.dart rename to examples/dartness_simple/lib/src/modules/user_module.dart index 55d5dbd4..de22457f 100644 --- a/examples/dartness_simple/lib/src/controllers/user/user_module.dart +++ b/examples/dartness_simple/lib/src/modules/user_module.dart @@ -1,6 +1,6 @@ import 'package:dartness_server/modules.dart'; -import '../user/user_controller.dart'; +import '../controllers/user_controller.dart'; const userModule = Module( metadata: ModuleMetadata( diff --git a/examples/dartness_simple/lib/src/services/city_service.dart b/examples/dartness_simple/lib/src/services/city_service.dart index 997c0051..083720de 100644 --- a/examples/dartness_simple/lib/src/services/city_service.dart +++ b/examples/dartness_simple/lib/src/services/city_service.dart @@ -1,3 +1,4 @@ + import '../dtos/city_dto.dart'; import '../error_handlers/not_found_exception.dart'; diff --git a/examples/dartness_simple/lib/src/services/todos_service.dart b/examples/dartness_simple/lib/src/services/todos_service.dart new file mode 100644 index 00000000..60cf2ff0 --- /dev/null +++ b/examples/dartness_simple/lib/src/services/todos_service.dart @@ -0,0 +1,12 @@ +import 'package:dio/dio.dart'; + +class TodosService { + final Dio _dio; + + TodosService(this._dio); + + Future> getTodos() async { + final result = await _dio.get('https://dummyjson.com/todos/user/5'); + return result.data as Map; + } +} diff --git a/examples/dartness_simple/pubspec.yaml b/examples/dartness_simple/pubspec.yaml index a3b830bd..abacb049 100644 --- a/examples/dartness_simple/pubspec.yaml +++ b/examples/dartness_simple/pubspec.yaml @@ -9,10 +9,11 @@ environment: sdk: ">=3.0.0 <4.0.0" dependencies: - dartness_server: 0.5.0-alpha + dartness_server: ^0.5.1-alpha + dio: ^5.3.3 dev_dependencies: - lints: ^2.1.1 - test: ^1.24.7 + lints: ^3.0.0 + test: ^1.24.9 build_runner: ^2.4.6 - dartness_generator: 0.5.0-alpha + dartness_generator: ^0.5.1-alpha diff --git a/packages/dartness_generator/CHANGELOG.md b/packages/dartness_generator/CHANGELOG.md index 0556fc81..f6ca18b9 100644 --- a/packages/dartness_generator/CHANGELOG.md +++ b/packages/dartness_generator/CHANGELOG.md @@ -6,11 +6,23 @@ ## 0.1.1-alpha - Compatibility with @Headers -- +- + ## 0.1.2-alpha - Updated dart sdk to 3.0.0 ## 0.5.0-alpha -- Easiest way of creating an app by `@Application` annotation and modules \ No newline at end of file +- Easiest way of creating an app by `@Application` annotation and modules + +## 0.5.1-alpha + +- Updated code generation in order with the new functionalities + - `useFactory` + - @Inject + - Dependency Injection by name + +## 0.5.2-alpha + +- Fixed error when injecting dependency using `useFactory` \ No newline at end of file diff --git a/packages/dartness_generator/README.md b/packages/dartness_generator/README.md index 0c865a69..b83ddd91 100644 --- a/packages/dartness_generator/README.md +++ b/packages/dartness_generator/README.md @@ -9,11 +9,11 @@ dartness_generator is used with dartness_server in order to provide the code gen ```yaml dependencies: - dartness_server: ^0.5.0-alpha + dartness_server: ^0.5.1-alpha dev_dependencies: build_runner: ^2.2.0 - dartness_generator: ^0.4.6-alpha + dartness_generator: ^0.5.2-alpha ``` 2. Add the corresponding `part '.g.dart'` to your classes, otherwise the new code won't be generated, you can find an diff --git a/packages/dartness_generator/doc/api/__404error.html b/packages/dartness_generator/doc/api/__404error.html index cc5f1874..1fd87bdd 100644 --- a/packages/dartness_generator/doc/api/__404error.html +++ b/packages/dartness_generator/doc/api/__404error.html @@ -94,7 +94,7 @@
dartness_generator dartness_generator - 0.4.6-alpha + 0.5.2-alpha diff --git a/packages/dartness_generator/doc/api/builder/application.html b/packages/dartness_generator/doc/api/builder/application.html index a7810230..c6649c02 100644 --- a/packages/dartness_generator/doc/api/builder/application.html +++ b/packages/dartness_generator/doc/api/builder/application.html @@ -109,7 +109,7 @@
builder library