Skip to content

Commit

Permalink
Merge pull request #12 from Evyweb/feature/use-ioctopus-modules
Browse files Browse the repository at this point in the history
Feature/use ioctopus modules feature
  • Loading branch information
nikolovlazar authored Nov 27, 2024
2 parents 733ce93 + 557de3f commit bdfaf31
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 51 deletions.
20 changes: 10 additions & 10 deletions di/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { DI_RETURN_TYPES, DI_SYMBOLS } from '@/di/types';

import { IInstrumentationService } from '@/src/application/services/instrumentation.service.interface';

import { registerMonitoringModule } from '@/di/modules/monitoring.module';
import { registerAuthenticationModule } from '@/di/modules/authentication.module';
import { registerTransactionManagerModule } from '@/di/modules/database.module';
import { registerTodosModule } from '@/di/modules/todos.module';
import { registerUsersModule } from '@/di/modules/users.module';
import { createMonitoringModule } from '@/di/modules/monitoring.module';
import { createAuthenticationModule } from '@/di/modules/authentication.module';
import { createTransactionManagerModule } from '@/di/modules/database.module';
import { createTodosModule } from '@/di/modules/todos.module';
import { createUsersModule } from '@/di/modules/users.module';

const ApplicationContainer = createContainer();

registerMonitoringModule(ApplicationContainer);
registerTransactionManagerModule(ApplicationContainer);
registerAuthenticationModule(ApplicationContainer);
registerUsersModule(ApplicationContainer);
registerTodosModule(ApplicationContainer);
ApplicationContainer.load(Symbol('MonitoringModule'), createMonitoringModule());
ApplicationContainer.load(Symbol('TransactionManagerModule'), createTransactionManagerModule());
ApplicationContainer.load(Symbol('AuthenticationModule'), createAuthenticationModule());
ApplicationContainer.load(Symbol('UsersModule'), createUsersModule());
ApplicationContainer.load(Symbol('TodosModule'), createTodosModule());

export function getInjection<K extends keyof typeof DI_SYMBOLS>(
symbol: K
Expand Down
24 changes: 14 additions & 10 deletions di/modules/authentication.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container } from '@evyweb/ioctopus';
import { createModule } from '@evyweb/ioctopus';

import { AuthenticationService } from '@/src/infrastructure/services/authentication.service';
import { MockAuthenticationService } from '@/src/infrastructure/services/authentication.service.mock';
Expand All @@ -13,62 +13,66 @@ import { signUpController } from '@/src/interface-adapters/controllers/auth/sign

import { DI_SYMBOLS } from '@/di/types';

export function registerAuthenticationModule(container: Container) {
export function createAuthenticationModule() {
const authenticationModule = createModule();

if (process.env.NODE_ENV === 'test') {
container
authenticationModule
.bind(DI_SYMBOLS.IAuthenticationService)
.toClass(MockAuthenticationService, [DI_SYMBOLS.IUsersRepository]);
} else {
container
authenticationModule
.bind(DI_SYMBOLS.IAuthenticationService)
.toClass(AuthenticationService, [
DI_SYMBOLS.IUsersRepository,
DI_SYMBOLS.IInstrumentationService,
]);
}

container
authenticationModule
.bind(DI_SYMBOLS.ISignInUseCase)
.toHigherOrderFunction(signInUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IUsersRepository,
DI_SYMBOLS.IAuthenticationService,
]);

container
authenticationModule
.bind(DI_SYMBOLS.ISignOutUseCase)
.toHigherOrderFunction(signOutUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IAuthenticationService,
]);

container
authenticationModule
.bind(DI_SYMBOLS.ISignUpUseCase)
.toHigherOrderFunction(signUpUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IAuthenticationService,
DI_SYMBOLS.IUsersRepository,
]);

container
authenticationModule
.bind(DI_SYMBOLS.ISignInController)
.toHigherOrderFunction(signInController, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ISignInUseCase,
]);

container
authenticationModule
.bind(DI_SYMBOLS.ISignOutController)
.toHigherOrderFunction(signOutController, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IAuthenticationService,
DI_SYMBOLS.ISignOutUseCase,
]);

container
authenticationModule
.bind(DI_SYMBOLS.ISignUpController)
.toHigherOrderFunction(signUpController, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ISignUpUseCase,
]);

return authenticationModule;
}
12 changes: 8 additions & 4 deletions di/modules/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { Container } from '@evyweb/ioctopus';
import { createModule } from '@evyweb/ioctopus';

import { TransactionManagerService } from '@/src/infrastructure/services/transaction-manager.service';
import { MockTransactionManagerService } from '@/src/infrastructure/services/transaction-manager.service.mock';

import { DI_SYMBOLS } from '@/di/types';

export function registerTransactionManagerModule(container: Container) {
export function createTransactionManagerModule() {
const transactionManagerModule = createModule();

if (process.env.NODE_ENV === 'test') {
container
transactionManagerModule
.bind(DI_SYMBOLS.ITransactionManagerService)
.toClass(MockTransactionManagerService);
} else {
container
transactionManagerModule
.bind(DI_SYMBOLS.ITransactionManagerService)
.toClass(TransactionManagerService);
}

return transactionManagerModule;
}
16 changes: 10 additions & 6 deletions di/modules/monitoring.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container } from '@evyweb/ioctopus';
import { createModule } from '@evyweb/ioctopus';

import { MockInstrumentationService } from '@/src/infrastructure/services/instrumentation.service.mock';
import { InstrumentationService } from '@/src/infrastructure/services/instrumentation.service';
Expand All @@ -7,20 +7,24 @@ import { CrashReporterService } from '@/src/infrastructure/services/crash-report

import { DI_SYMBOLS } from '@/di/types';

export function registerMonitoringModule(container: Container) {
export function createMonitoringModule() {
const monitoringModule = createModule();

if (process.env.NODE_ENV === 'test') {
container
monitoringModule
.bind(DI_SYMBOLS.IInstrumentationService)
.toClass(MockInstrumentationService);
container
monitoringModule
.bind(DI_SYMBOLS.ICrashReporterService)
.toClass(MockCrashReporterService);
} else {
container
monitoringModule
.bind(DI_SYMBOLS.IInstrumentationService)
.toClass(InstrumentationService);
container
monitoringModule
.bind(DI_SYMBOLS.ICrashReporterService)
.toClass(CrashReporterService);
}

return monitoringModule;
}
28 changes: 16 additions & 12 deletions di/modules/todos.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Container } from '@evyweb/ioctopus';
import { createModule } from '@evyweb/ioctopus';

import { MockTodosRepository } from '@/src/infrastructure/repositories/todos.repository.mock';
import { TodosRepository } from '@/src/infrastructure/repositories/todos.repository';
Expand All @@ -15,47 +15,49 @@ import { toggleTodoController } from '@/src/interface-adapters/controllers/todos

import { DI_SYMBOLS } from '@/di/types';

export function registerTodosModule(container: Container) {
export function createTodosModule() {
const todosModule = createModule();

if (process.env.NODE_ENV === 'test') {
container.bind(DI_SYMBOLS.ITodosRepository).toClass(MockTodosRepository);
todosModule.bind(DI_SYMBOLS.ITodosRepository).toClass(MockTodosRepository);
} else {
container
todosModule
.bind(DI_SYMBOLS.ITodosRepository)
.toClass(TodosRepository, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ICrashReporterService,
]);
}

container
todosModule
.bind(DI_SYMBOLS.ICreateTodoUseCase)
.toHigherOrderFunction(createTodoUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ITodosRepository,
]);

container
todosModule
.bind(DI_SYMBOLS.IDeleteTodoUseCase)
.toHigherOrderFunction(deleteTodoUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ITodosRepository,
]);

container
todosModule
.bind(DI_SYMBOLS.IGetTodosForUserUseCase)
.toHigherOrderFunction(getTodosForUserUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ITodosRepository,
]);

container
todosModule
.bind(DI_SYMBOLS.IToggleTodoUseCase)
.toHigherOrderFunction(toggleTodoUseCase, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ITodosRepository,
]);

container
todosModule
.bind(DI_SYMBOLS.IBulkUpdateController)
.toHigherOrderFunction(bulkUpdateController, [
DI_SYMBOLS.IInstrumentationService,
Expand All @@ -65,7 +67,7 @@ export function registerTodosModule(container: Container) {
DI_SYMBOLS.IDeleteTodoUseCase,
]);

container
todosModule
.bind(DI_SYMBOLS.ICreateTodoController)
.toHigherOrderFunction(createTodoController, [
DI_SYMBOLS.IInstrumentationService,
Expand All @@ -74,19 +76,21 @@ export function registerTodosModule(container: Container) {
DI_SYMBOLS.ICreateTodoUseCase,
]);

container
todosModule
.bind(DI_SYMBOLS.IGetTodosForUserController)
.toHigherOrderFunction(getTodosForUserController, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IAuthenticationService,
DI_SYMBOLS.IGetTodosForUserUseCase,
]);

container
todosModule
.bind(DI_SYMBOLS.IToggleTodoController)
.toHigherOrderFunction(toggleTodoController, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.IAuthenticationService,
DI_SYMBOLS.IToggleTodoUseCase,
]);

return todosModule;
}
12 changes: 8 additions & 4 deletions di/modules/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { Container } from '@evyweb/ioctopus';
import { createModule } from '@evyweb/ioctopus';

import { MockUsersRepository } from '@/src/infrastructure/repositories/users.repository.mock';
import { UsersRepository } from '@/src/infrastructure/repositories/users.repository';

import { DI_SYMBOLS } from '@/di/types';

export function registerUsersModule(container: Container) {
export function createUsersModule() {
const usersModule = createModule();

if (process.env.NODE_ENV === 'test') {
container.bind(DI_SYMBOLS.IUsersRepository).toClass(MockUsersRepository);
usersModule.bind(DI_SYMBOLS.IUsersRepository).toClass(MockUsersRepository);
} else {
container
usersModule
.bind(DI_SYMBOLS.IUsersRepository)
.toClass(UsersRepository, [
DI_SYMBOLS.IInstrumentationService,
DI_SYMBOLS.ICrashReporterService,
]);
}

return usersModule;
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"coverage": "vitest run --coverage"
},
"dependencies": {
"@evyweb/ioctopus": "^0.3.0",
"@evyweb/ioctopus": "^1.1.0",
"@libsql/client": "^0.9.0",
"@lucia-auth/adapter-drizzle": "^1.1.0",
"@radix-ui/react-avatar": "^1.1.0",
Expand Down

0 comments on commit bdfaf31

Please sign in to comment.