From 138f8a3da4181d2e7c558af71c7268bd0c89548e Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 21 Dec 2018 21:22:06 +0100 Subject: [PATCH] refactor: Switch from configuration by annotations to using register --- source/d_webservice_example/application.d | 6 +++--- source/d_webservice_example/business/todo_service.d | 9 ++++----- source/d_webservice_example/component_registration.d | 12 ++++++++---- .../controller/todo_controller.d | 7 +++---- .../dataaccess/in_memory_todo_repository.d | 3 +-- source/d_webservice_example/mapper/todo_mapper.d | 1 - 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/source/d_webservice_example/application.d b/source/d_webservice_example/application.d index 9fc2732..3e7961a 100644 --- a/source/d_webservice_example/application.d +++ b/source/d_webservice_example/application.d @@ -8,11 +8,11 @@ void main() @safe import d_webservice_example.component_registration : registerComponents; import d_webservice_example.controller.todo_controller : TodoController; - auto container = singleton(); + auto container = singleton; scope (exit) container.terminate; - container.registerComponents(); + container.registerComponents; container.instantiate; auto router = new URLRouter; @@ -22,5 +22,5 @@ void main() @safe settings.bindAddresses = ["::", "0.0.0.0"]; listenHTTP(settings, router); - runApplication(); + runApplication; } diff --git a/source/d_webservice_example/business/todo_service.d b/source/d_webservice_example/business/todo_service.d index b8f04d9..346ca43 100644 --- a/source/d_webservice_example/business/todo_service.d +++ b/source/d_webservice_example/business/todo_service.d @@ -1,10 +1,9 @@ module d_webservice_example.business.todo_service; -import aermicioi.aedi : autowired, component; import d_webservice_example.model.todo : Todo; import std.uuid : UUID, randomUUID; -@component class TodoService +class TodoService { import d_webservice_example.data.todo_update_do : TodoUpdateDO; @@ -13,20 +12,20 @@ private: public: - @autowired this(TodoRepository todoRepository) @safe + this(TodoRepository todoRepository) nothrow pure @nogc @safe { this.todoRepository = todoRepository; } Todo createTodo(Todo newTodo) @safe { - immutable todo = Todo(newTodo.title, newTodo.content, randomUUID()); + immutable todo = Todo(newTodo.title, newTodo.content, randomUUID); return todoRepository.save(todo); } Todo[] getAllTodos() @safe { - return todoRepository.findAll(); + return todoRepository.findAll; } Todo getTodoByUuid(const UUID uuid) @safe diff --git a/source/d_webservice_example/component_registration.d b/source/d_webservice_example/component_registration.d index cdab3e9..44bf688 100644 --- a/source/d_webservice_example/component_registration.d +++ b/source/d_webservice_example/component_registration.d @@ -1,10 +1,14 @@ module d_webservice_example.component_registration; -import aermicioi.aedi : ConfigurableContainer, scan; +import aermicioi.aedi; void registerComponents(ConfigurableContainer container) @safe { - container.scan!(d_webservice_example.business.todo_service); - container.scan!(d_webservice_example.controller.todo_controller); - container.scan!(d_webservice_example.dataaccess.in_memory_todo_repository); + import d_webservice_example.business.todo_service : TodoRepository, TodoService; + import d_webservice_example.controller.todo_controller : TodoController; + import d_webservice_example.dataaccess.in_memory_todo_repository : InMemoryTodoRepository; + + container.configure.register!(TodoRepository, InMemoryTodoRepository); + container.configure.register!TodoService.autowire; + container.configure.register!TodoController.autowire; } diff --git a/source/d_webservice_example/controller/todo_controller.d b/source/d_webservice_example/controller/todo_controller.d index 0a797fc..ae984c7 100644 --- a/source/d_webservice_example/controller/todo_controller.d +++ b/source/d_webservice_example/controller/todo_controller.d @@ -1,6 +1,5 @@ module d_webservice_example.controller.todo_controller; -import aermicioi.aedi : autowired, component; import d_webservice_example.transport.todo_to : TodoTO; import std.typecons : Nullable; import std.uuid : UUID; @@ -25,7 +24,7 @@ interface TodoApi void deleteTodo(UUID _uuid) @safe; } -@component class TodoController : TodoApi +class TodoController : TodoApi { import d_webservice_example.business.todo_service : TodoService; import d_webservice_example.mapper.todo_mapper : asTodoTO; @@ -35,7 +34,7 @@ private: public: - @autowired this(TodoService todoService) @safe + this(TodoService todoService) nothrow pure @nogc @safe { this.todoService = todoService; } @@ -53,7 +52,7 @@ public: import std.algorithm.iteration : map; import std.array : array; - return todoService.getAllTodos().map!asTodoTO.array; + return todoService.getAllTodos.map!asTodoTO.array; } override TodoTO getTodo(UUID _uuid) @safe diff --git a/source/d_webservice_example/dataaccess/in_memory_todo_repository.d b/source/d_webservice_example/dataaccess/in_memory_todo_repository.d index 43ee63f..3a4772c 100644 --- a/source/d_webservice_example/dataaccess/in_memory_todo_repository.d +++ b/source/d_webservice_example/dataaccess/in_memory_todo_repository.d @@ -1,6 +1,5 @@ module d_webservice_example.dataaccess.in_memory_todo_repository; -import aermicioi.aedi : component, qualifier; import d_webservice_example.business.todo_service : TodoRepository; version (unittest) @@ -8,7 +7,7 @@ version (unittest) import fluent.asserts; } -@component @qualifier!TodoRepository() class InMemoryTodoRepository : TodoRepository +class InMemoryTodoRepository : TodoRepository { import d_webservice_example.model.todo : Todo; import std.typecons : Nullable, nullable; diff --git a/source/d_webservice_example/mapper/todo_mapper.d b/source/d_webservice_example/mapper/todo_mapper.d index 51d7eff..e761df5 100644 --- a/source/d_webservice_example/mapper/todo_mapper.d +++ b/source/d_webservice_example/mapper/todo_mapper.d @@ -5,6 +5,5 @@ import d_webservice_example.transport.todo_to : TodoTO; TodoTO asTodoTO(Todo todo) nothrow pure @safe @nogc { - return TodoTO(todo.uuid, todo.title, todo.content); }