refactor: Switch from configuration by annotations to using register

This commit is contained in:
Johannes Loher 2018-12-21 21:22:06 +01:00
parent be59006d64
commit 138f8a3da4
6 changed files with 19 additions and 19 deletions

View file

@ -8,11 +8,11 @@ void main() @safe
import d_webservice_example.component_registration : registerComponents; import d_webservice_example.component_registration : registerComponents;
import d_webservice_example.controller.todo_controller : TodoController; import d_webservice_example.controller.todo_controller : TodoController;
auto container = singleton(); auto container = singleton;
scope (exit) scope (exit)
container.terminate; container.terminate;
container.registerComponents(); container.registerComponents;
container.instantiate; container.instantiate;
auto router = new URLRouter; auto router = new URLRouter;
@ -22,5 +22,5 @@ void main() @safe
settings.bindAddresses = ["::", "0.0.0.0"]; settings.bindAddresses = ["::", "0.0.0.0"];
listenHTTP(settings, router); listenHTTP(settings, router);
runApplication(); runApplication;
} }

View file

@ -1,10 +1,9 @@
module d_webservice_example.business.todo_service; module d_webservice_example.business.todo_service;
import aermicioi.aedi : autowired, component;
import d_webservice_example.model.todo : Todo; import d_webservice_example.model.todo : Todo;
import std.uuid : UUID, randomUUID; import std.uuid : UUID, randomUUID;
@component class TodoService class TodoService
{ {
import d_webservice_example.data.todo_update_do : TodoUpdateDO; import d_webservice_example.data.todo_update_do : TodoUpdateDO;
@ -13,20 +12,20 @@ private:
public: public:
@autowired this(TodoRepository todoRepository) @safe this(TodoRepository todoRepository) nothrow pure @nogc @safe
{ {
this.todoRepository = todoRepository; this.todoRepository = todoRepository;
} }
Todo createTodo(Todo newTodo) @safe 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); return todoRepository.save(todo);
} }
Todo[] getAllTodos() @safe Todo[] getAllTodos() @safe
{ {
return todoRepository.findAll(); return todoRepository.findAll;
} }
Todo getTodoByUuid(const UUID uuid) @safe Todo getTodoByUuid(const UUID uuid) @safe

View file

@ -1,10 +1,14 @@
module d_webservice_example.component_registration; module d_webservice_example.component_registration;
import aermicioi.aedi : ConfigurableContainer, scan; import aermicioi.aedi;
void registerComponents(ConfigurableContainer container) @safe void registerComponents(ConfigurableContainer container) @safe
{ {
container.scan!(d_webservice_example.business.todo_service); import d_webservice_example.business.todo_service : TodoRepository, TodoService;
container.scan!(d_webservice_example.controller.todo_controller); import d_webservice_example.controller.todo_controller : TodoController;
container.scan!(d_webservice_example.dataaccess.in_memory_todo_repository); 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;
} }

View file

@ -1,6 +1,5 @@
module d_webservice_example.controller.todo_controller; module d_webservice_example.controller.todo_controller;
import aermicioi.aedi : autowired, component;
import d_webservice_example.transport.todo_to : TodoTO; import d_webservice_example.transport.todo_to : TodoTO;
import std.typecons : Nullable; import std.typecons : Nullable;
import std.uuid : UUID; import std.uuid : UUID;
@ -25,7 +24,7 @@ interface TodoApi
void deleteTodo(UUID _uuid) @safe; void deleteTodo(UUID _uuid) @safe;
} }
@component class TodoController : TodoApi class TodoController : TodoApi
{ {
import d_webservice_example.business.todo_service : TodoService; import d_webservice_example.business.todo_service : TodoService;
import d_webservice_example.mapper.todo_mapper : asTodoTO; import d_webservice_example.mapper.todo_mapper : asTodoTO;
@ -35,7 +34,7 @@ private:
public: public:
@autowired this(TodoService todoService) @safe this(TodoService todoService) nothrow pure @nogc @safe
{ {
this.todoService = todoService; this.todoService = todoService;
} }
@ -53,7 +52,7 @@ public:
import std.algorithm.iteration : map; import std.algorithm.iteration : map;
import std.array : array; import std.array : array;
return todoService.getAllTodos().map!asTodoTO.array; return todoService.getAllTodos.map!asTodoTO.array;
} }
override TodoTO getTodo(UUID _uuid) @safe override TodoTO getTodo(UUID _uuid) @safe

View file

@ -1,6 +1,5 @@
module d_webservice_example.dataaccess.in_memory_todo_repository; module d_webservice_example.dataaccess.in_memory_todo_repository;
import aermicioi.aedi : component, qualifier;
import d_webservice_example.business.todo_service : TodoRepository; import d_webservice_example.business.todo_service : TodoRepository;
version (unittest) version (unittest)
@ -8,7 +7,7 @@ version (unittest)
import fluent.asserts; import fluent.asserts;
} }
@component @qualifier!TodoRepository() class InMemoryTodoRepository : TodoRepository class InMemoryTodoRepository : TodoRepository
{ {
import d_webservice_example.model.todo : Todo; import d_webservice_example.model.todo : Todo;
import std.typecons : Nullable, nullable; import std.typecons : Nullable, nullable;

View file

@ -5,6 +5,5 @@ import d_webservice_example.transport.todo_to : TodoTO;
TodoTO asTodoTO(Todo todo) nothrow pure @safe @nogc TodoTO asTodoTO(Todo todo) nothrow pure @safe @nogc
{ {
return TodoTO(todo.uuid, todo.title, todo.content); return TodoTO(todo.uuid, todo.title, todo.content);
} }