// Repository Interfaces import { IUserRepository } from '../../Domain/IRepository/IUserRepository'; import { IChatRepository } from '../../Domain/IRepository/IChatRepository'; import { IChatArchiveRepository } from '../../Domain/IRepository/IChatArchiveRepository'; import { IDeckRepository } from '../../Domain/IRepository/IDeckRepository'; import { IOrganizationRepository } from '../../Domain/IRepository/IOrganizationRepository'; import { IContactRepository } from '../../Domain/IRepository/IContactRepository'; import { IGameRepository } from '../../Domain/IRepository/IGameRepository'; // Repository Implementations import { UserRepository } from '../../Infrastructure/Repository/UserRepository'; import { ChatRepository } from '../../Infrastructure/Repository/ChatRepository'; import { ChatArchiveRepository } from '../../Infrastructure/Repository/ChatArchiveRepository'; import { DeckRepository } from '../../Infrastructure/Repository/DeckRepository'; import { OrganizationRepository } from '../../Infrastructure/Repository/OrganizationRepository'; import { ContactRepository } from '../../Infrastructure/Repository/ContactRepository'; import { GameRepository } from '../../Infrastructure/Repository/GameRepository'; // Command Handlers import { CreateUserCommandHandler } from '../User/commands/CreateUserCommandHandler'; import { LoginCommandHandler } from '../User/commands/LoginCommandHandler'; import { LogoutCommandHandler } from '../User/commands/LogoutCommandHandler'; import { UpdateUserCommandHandler } from '../User/commands/UpdateUserCommandHandler'; import { DeactivateUserCommandHandler } from '../User/commands/DeactivateUserCommandHandler'; import { DeleteUserCommandHandler } from '../User/commands/DeleteUserCommandHandler'; import { VerifyEmailCommandHandler } from '../User/commands/VerifyEmailCommandHandler'; import { RequestPasswordResetCommandHandler } from '../User/commands/RequestPasswordResetCommandHandler'; import { ResetPasswordCommandHandler } from '../User/commands/ResetPasswordCommandHandler'; import { CreateChatCommandHandler } from '../Chat/commands/CreateChatCommandHandler'; import { SendMessageCommandHandler } from '../Chat/commands/SendMessageCommandHandler'; import { ArchiveChatCommandHandler, RestoreChatCommandHandler } from '../Chat/commands/ChatArchiveCommandHandlers'; import { CreateDeckCommandHandler } from '../Deck/commands/CreateDeckCommandHandler'; import { UpdateDeckCommandHandler } from '../Deck/commands/UpdateDeckCommandHandler'; import { DeleteDeckCommandHandler } from '../Deck/commands/DeleteDeckCommandHandler'; import { CreateOrganizationCommandHandler } from '../Organization/commands/CreateOrganizationCommandHandler'; import { UpdateOrganizationCommandHandler } from '../Organization/commands/UpdateOrganizationCommandHandler'; import { DeleteOrganizationCommandHandler } from '../Organization/commands/DeleteOrganizationCommandHandler'; import { ProcessOrgAuthCallbackCommandHandler } from '../Organization/commands/ProcessOrgAuthCallbackCommandHandler'; import { CreateContactCommandHandler } from '../Contact/commands/CreateContactCommandHandler'; import { UpdateContactCommandHandler } from '../Contact/commands/UpdateContactCommandHandler'; import { DeleteContactCommandHandler } from '../Contact/commands/DeleteContactCommandHandler'; import { ActivateUserCommandHandler } from '../User/commands/ActivateUserCommandHandler'; // Query Handlers import { GetUserByIdQueryHandler } from '../User/queries/GetUserByIdQueryHandler'; import { GetUsersByPageQueryHandler } from '../User/queries/GetUsersByPageQueryHandler'; import { GetUserChatsQueryHandler } from '../Chat/queries/GetUserChatsQueryHandler'; import { GetChatHistoryQueryHandler, GetArchivedChatsQueryHandler } from '../Chat/queries/ChatHistoryQueryHandlers'; import { GetChatsByPageQueryHandler } from '../Chat/queries/GetChatsByPageQueryHandler'; import { GetDeckByIdQueryHandler } from '../Deck/queries/GetDeckByIdQueryHandler'; import { GetDecksByPageQueryHandler } from '../Deck/queries/GetDecksByPageQueryHandler'; import { GetOrganizationByIdQueryHandler } from '../Organization/queries/GetOrganizationByIdQueryHandler'; import { GetOrganizationsByPageQueryHandler } from '../Organization/queries/GetOrganizationsByPageQueryHandler'; import { GetOrganizationLoginUrlQueryHandler } from '../Organization/queries/GetOrganizationLoginUrlQueryHandler'; import { GetContactByIdQueryHandler } from '../Contact/queries/GetContactByIdQueryHandler'; import { GetContactsByPageQueryHandler } from '../Contact/queries/GetContactsByPageQueryHandler'; // Services import { JWTService } from './JWTService'; import { EmailService } from './EmailService'; import { GameTokenService } from './GameTokenService'; import { ContactEmailService } from './ContactEmailService'; import { DeckImportExportService } from './DeckImportExportService'; import { FieldEffectService } from './FieldEffectService'; import { CardDrawingService } from './CardDrawingService'; import { GamemasterService } from './GamemasterService'; import { RedisService } from './RedisService'; import { GameService } from '../Game/GameService'; import { BoardGenerationService } from '../Game/BoardGenerationService'; import { GenerateBoardCommandHandler } from '../Game/commands/GenerateBoardCommandHandler'; /** * Central Dependency Injection Container * Manages all repositories, command handlers, and query handlers as singletons */ export class DIContainer { private static instance: DIContainer; // Repositories - Using interfaces for better abstraction private _userRepository: IUserRepository | null = null; private _chatRepository: IChatRepository | null = null; private _chatArchiveRepository: IChatArchiveRepository | null = null; private _deckRepository: IDeckRepository | null = null; private _organizationRepository: IOrganizationRepository | null = null; private _contactRepository: IContactRepository | null = null; private _gameRepository: IGameRepository | null = null; // Services private _jwtService: JWTService | null = null; private _emailService: EmailService | null = null; private _gameTokenService: GameTokenService | null = null; private _contactEmailService: ContactEmailService | null = null; private _deckImportExportService: DeckImportExportService | null = null; private _cardDrawingService: CardDrawingService | null = null; private _gamemasterService: GamemasterService | null = null; private _fieldEffectService: FieldEffectService | null = null; private _gameService: GameService | null = null; private _boardGenerationService: BoardGenerationService | null = null; // Command Handlers private _createUserCommandHandler: CreateUserCommandHandler | null = null; private _loginCommandHandler: LoginCommandHandler | null = null; private _logoutCommandHandler: LogoutCommandHandler | null = null; private _updateUserCommandHandler: UpdateUserCommandHandler | null = null; private _deactivateUserCommandHandler: DeactivateUserCommandHandler | null = null; private _deleteUserCommandHandler: DeleteUserCommandHandler | null = null; private _verifyEmailCommandHandler: VerifyEmailCommandHandler | null = null; private _requestPasswordResetCommandHandler: RequestPasswordResetCommandHandler | null = null; private _resetPasswordCommandHandler: ResetPasswordCommandHandler | null = null; private _createChatCommandHandler: CreateChatCommandHandler | null = null; private _sendMessageCommandHandler: SendMessageCommandHandler | null = null; private _archiveChatCommandHandler: ArchiveChatCommandHandler | null = null; private _restoreChatCommandHandler: RestoreChatCommandHandler | null = null; private _createDeckCommandHandler: CreateDeckCommandHandler | null = null; private _updateDeckCommandHandler: UpdateDeckCommandHandler | null = null; private _deleteDeckCommandHandler: DeleteDeckCommandHandler | null = null; private _createOrganizationCommandHandler: CreateOrganizationCommandHandler | null = null; private _updateOrganizationCommandHandler: UpdateOrganizationCommandHandler | null = null; private _deleteOrganizationCommandHandler: DeleteOrganizationCommandHandler | null = null; private _processOrgAuthCallbackCommandHandler: ProcessOrgAuthCallbackCommandHandler | null = null; private _createContactCommandHandler: CreateContactCommandHandler | null = null; private _updateContactCommandHandler: UpdateContactCommandHandler | null = null; private _deleteContactCommandHandler: DeleteContactCommandHandler | null = null; private _generateBoardCommandHandler: GenerateBoardCommandHandler | null = null; private _activateUserCommandHandler: ActivateUserCommandHandler | null = null; // Query Handlers private _getUserByIdQueryHandler: GetUserByIdQueryHandler | null = null; private _getUsersByPageQueryHandler: GetUsersByPageQueryHandler | null = null; private _getUserChatsQueryHandler: GetUserChatsQueryHandler | null = null; private _getChatHistoryQueryHandler: GetChatHistoryQueryHandler | null = null; private _getArchivedChatsQueryHandler: GetArchivedChatsQueryHandler | null = null; private _getChatsByPageQueryHandler: GetChatsByPageQueryHandler | null = null; private _getDeckByIdQueryHandler: GetDeckByIdQueryHandler | null = null; private _getDecksByPageQueryHandler: GetDecksByPageQueryHandler | null = null; private _getOrganizationByIdQueryHandler: GetOrganizationByIdQueryHandler | null = null; private _getOrganizationsByPageQueryHandler: GetOrganizationsByPageQueryHandler | null = null; private _getOrganizationLoginUrlQueryHandler: GetOrganizationLoginUrlQueryHandler | null = null; private _getContactByIdQueryHandler: GetContactByIdQueryHandler | null = null; private _getContactsByPageQueryHandler: GetContactsByPageQueryHandler | null = null; private constructor() {} public static getInstance(): DIContainer { if (!DIContainer.instance) { DIContainer.instance = new DIContainer(); } return DIContainer.instance; } // Repository getters - Return interfaces for better abstraction public get userRepository(): IUserRepository { if (!this._userRepository) { this._userRepository = new UserRepository(); } return this._userRepository; } public get chatRepository(): IChatRepository { if (!this._chatRepository) { this._chatRepository = new ChatRepository(); } return this._chatRepository; } public get chatArchiveRepository(): IChatArchiveRepository { if (!this._chatArchiveRepository) { this._chatArchiveRepository = new ChatArchiveRepository(); } return this._chatArchiveRepository; } public get deckRepository(): IDeckRepository { if (!this._deckRepository) { this._deckRepository = new DeckRepository(); } return this._deckRepository; } public get organizationRepository(): IOrganizationRepository { if (!this._organizationRepository) { this._organizationRepository = new OrganizationRepository(); } return this._organizationRepository; } public get contactRepository(): IContactRepository { if (!this._contactRepository) { this._contactRepository = new ContactRepository(); } return this._contactRepository; } public get gameRepository(): IGameRepository { if (!this._gameRepository) { this._gameRepository = new GameRepository(); } return this._gameRepository; } // Services getters public get jwtService(): JWTService { if (!this._jwtService) { this._jwtService = new JWTService(); } return this._jwtService; } public get emailService(): EmailService { if (!this._emailService) { this._emailService = new EmailService(); } return this._emailService; } public get gameTokenService(): GameTokenService { if (!this._gameTokenService) { this._gameTokenService = new GameTokenService(); } return this._gameTokenService; } public get contactEmailService(): ContactEmailService { if (!this._contactEmailService) { this._contactEmailService = new ContactEmailService(this.contactRepository, this.emailService); } return this._contactEmailService; } public get deckImportExportService(): DeckImportExportService { if (!this._deckImportExportService) { this._deckImportExportService = new DeckImportExportService(this.deckRepository); } return this._deckImportExportService; } public get cardDrawingService(): CardDrawingService { if (!this._cardDrawingService) { this._cardDrawingService = new CardDrawingService(); } return this._cardDrawingService; } public get gamemasterService(): GamemasterService { if (!this._gamemasterService) { this._gamemasterService = new GamemasterService(); } return this._gamemasterService; } public get fieldEffectService(): FieldEffectService { if (!this._fieldEffectService) { this._fieldEffectService = new FieldEffectService( this.boardGenerationService, this.gamemasterService ); } return this._fieldEffectService; } public get gameService(): GameService { if (!this._gameService) { this._gameService = new GameService(); } return this._gameService; } public get boardGenerationService(): BoardGenerationService { if (!this._boardGenerationService) { this._boardGenerationService = new BoardGenerationService(); } return this._boardGenerationService; } // Command Handler getters public get createUserCommandHandler(): CreateUserCommandHandler { if (!this._createUserCommandHandler) { this._createUserCommandHandler = new CreateUserCommandHandler(this.userRepository, this.emailService); } return this._createUserCommandHandler; } public get loginCommandHandler(): LoginCommandHandler { if (!this._loginCommandHandler) { this._loginCommandHandler = new LoginCommandHandler(this.userRepository, this.jwtService, this.organizationRepository); } return this._loginCommandHandler; } public get logoutCommandHandler(): LogoutCommandHandler { if (!this._logoutCommandHandler) { this._logoutCommandHandler = new LogoutCommandHandler(this.userRepository); } return this._logoutCommandHandler; } public get updateUserCommandHandler(): UpdateUserCommandHandler { if (!this._updateUserCommandHandler) { this._updateUserCommandHandler = new UpdateUserCommandHandler(this.userRepository); } return this._updateUserCommandHandler; } public get deactivateUserCommandHandler(): DeactivateUserCommandHandler { if (!this._deactivateUserCommandHandler) { this._deactivateUserCommandHandler = new DeactivateUserCommandHandler(this.userRepository); } return this._deactivateUserCommandHandler; } public get activateUserCommandHandler(): ActivateUserCommandHandler { if (!this._activateUserCommandHandler) { this._activateUserCommandHandler = new ActivateUserCommandHandler(this.userRepository); } return this._activateUserCommandHandler; } public get deleteUserCommandHandler(): DeleteUserCommandHandler { if (!this._deleteUserCommandHandler) { this._deleteUserCommandHandler = new DeleteUserCommandHandler(this.userRepository); } return this._deleteUserCommandHandler; } public get verifyEmailCommandHandler(): VerifyEmailCommandHandler { if (!this._verifyEmailCommandHandler) { this._verifyEmailCommandHandler = new VerifyEmailCommandHandler(this.userRepository); } return this._verifyEmailCommandHandler; } public get requestPasswordResetCommandHandler(): RequestPasswordResetCommandHandler { if (!this._requestPasswordResetCommandHandler) { this._requestPasswordResetCommandHandler = new RequestPasswordResetCommandHandler(this.userRepository, this.emailService); } return this._requestPasswordResetCommandHandler; } public get resetPasswordCommandHandler(): ResetPasswordCommandHandler { if (!this._resetPasswordCommandHandler) { this._resetPasswordCommandHandler = new ResetPasswordCommandHandler(this.userRepository); } return this._resetPasswordCommandHandler; } public get createChatCommandHandler(): CreateChatCommandHandler { if (!this._createChatCommandHandler) { this._createChatCommandHandler = new CreateChatCommandHandler(this.chatRepository, this.userRepository); } return this._createChatCommandHandler; } public get sendMessageCommandHandler(): SendMessageCommandHandler { if (!this._sendMessageCommandHandler) { this._sendMessageCommandHandler = new SendMessageCommandHandler(this.chatRepository); } return this._sendMessageCommandHandler; } public get archiveChatCommandHandler(): ArchiveChatCommandHandler { if (!this._archiveChatCommandHandler) { this._archiveChatCommandHandler = new ArchiveChatCommandHandler(this.chatRepository); } return this._archiveChatCommandHandler; } public get restoreChatCommandHandler(): RestoreChatCommandHandler { if (!this._restoreChatCommandHandler) { this._restoreChatCommandHandler = new RestoreChatCommandHandler(this.chatRepository); } return this._restoreChatCommandHandler; } public get createDeckCommandHandler(): CreateDeckCommandHandler { if (!this._createDeckCommandHandler) { this._createDeckCommandHandler = new CreateDeckCommandHandler( this.deckRepository, this.userRepository, this.organizationRepository ); } return this._createDeckCommandHandler; } public get updateDeckCommandHandler(): UpdateDeckCommandHandler { if (!this._updateDeckCommandHandler) { this._updateDeckCommandHandler = new UpdateDeckCommandHandler(this.deckRepository); } return this._updateDeckCommandHandler; } public get deleteDeckCommandHandler(): DeleteDeckCommandHandler { if (!this._deleteDeckCommandHandler) { this._deleteDeckCommandHandler = new DeleteDeckCommandHandler(this.deckRepository); } return this._deleteDeckCommandHandler; } public get createOrganizationCommandHandler(): CreateOrganizationCommandHandler { if (!this._createOrganizationCommandHandler) { this._createOrganizationCommandHandler = new CreateOrganizationCommandHandler(this.organizationRepository); } return this._createOrganizationCommandHandler; } public get updateOrganizationCommandHandler(): UpdateOrganizationCommandHandler { if (!this._updateOrganizationCommandHandler) { this._updateOrganizationCommandHandler = new UpdateOrganizationCommandHandler(this.organizationRepository); } return this._updateOrganizationCommandHandler; } public get deleteOrganizationCommandHandler(): DeleteOrganizationCommandHandler { if (!this._deleteOrganizationCommandHandler) { this._deleteOrganizationCommandHandler = new DeleteOrganizationCommandHandler(this.organizationRepository); } return this._deleteOrganizationCommandHandler; } public get processOrgAuthCallbackCommandHandler(): ProcessOrgAuthCallbackCommandHandler { if (!this._processOrgAuthCallbackCommandHandler) { this._processOrgAuthCallbackCommandHandler = new ProcessOrgAuthCallbackCommandHandler(this.userRepository, this.organizationRepository); } return this._processOrgAuthCallbackCommandHandler; } public get createContactCommandHandler(): CreateContactCommandHandler { if (!this._createContactCommandHandler) { this._createContactCommandHandler = new CreateContactCommandHandler(this.contactRepository); } return this._createContactCommandHandler; } public get updateContactCommandHandler(): UpdateContactCommandHandler { if (!this._updateContactCommandHandler) { this._updateContactCommandHandler = new UpdateContactCommandHandler(this.contactRepository); } return this._updateContactCommandHandler; } public get deleteContactCommandHandler(): DeleteContactCommandHandler { if (!this._deleteContactCommandHandler) { this._deleteContactCommandHandler = new DeleteContactCommandHandler(this.contactRepository); } return this._deleteContactCommandHandler; } public get generateBoardCommandHandler(): GenerateBoardCommandHandler { if (!this._generateBoardCommandHandler) { this._generateBoardCommandHandler = new GenerateBoardCommandHandler(this.boardGenerationService, RedisService.getInstance()); } return this._generateBoardCommandHandler; } // Query Handler getters public get getUserByIdQueryHandler(): GetUserByIdQueryHandler { if (!this._getUserByIdQueryHandler) { this._getUserByIdQueryHandler = new GetUserByIdQueryHandler(this.userRepository); } return this._getUserByIdQueryHandler; } public get getUserChatsQueryHandler(): GetUserChatsQueryHandler { if (!this._getUserChatsQueryHandler) { this._getUserChatsQueryHandler = new GetUserChatsQueryHandler(this.chatRepository, this.chatArchiveRepository); } return this._getUserChatsQueryHandler; } public get getChatHistoryQueryHandler(): GetChatHistoryQueryHandler { if (!this._getChatHistoryQueryHandler) { this._getChatHistoryQueryHandler = new GetChatHistoryQueryHandler(this.chatRepository, this.chatArchiveRepository); } return this._getChatHistoryQueryHandler; } public get getArchivedChatsQueryHandler(): GetArchivedChatsQueryHandler { if (!this._getArchivedChatsQueryHandler) { this._getArchivedChatsQueryHandler = new GetArchivedChatsQueryHandler(this.chatArchiveRepository); } return this._getArchivedChatsQueryHandler; } public get getDeckByIdQueryHandler(): GetDeckByIdQueryHandler { if (!this._getDeckByIdQueryHandler) { this._getDeckByIdQueryHandler = new GetDeckByIdQueryHandler(this.deckRepository); } return this._getDeckByIdQueryHandler; } public get getOrganizationByIdQueryHandler(): GetOrganizationByIdQueryHandler { if (!this._getOrganizationByIdQueryHandler) { this._getOrganizationByIdQueryHandler = new GetOrganizationByIdQueryHandler(this.organizationRepository); } return this._getOrganizationByIdQueryHandler; } public get getOrganizationLoginUrlQueryHandler(): GetOrganizationLoginUrlQueryHandler { if (!this._getOrganizationLoginUrlQueryHandler) { this._getOrganizationLoginUrlQueryHandler = new GetOrganizationLoginUrlQueryHandler(this.organizationRepository); } return this._getOrganizationLoginUrlQueryHandler; } public get getContactByIdQueryHandler(): GetContactByIdQueryHandler { if (!this._getContactByIdQueryHandler) { this._getContactByIdQueryHandler = new GetContactByIdQueryHandler(this.contactRepository); } return this._getContactByIdQueryHandler; } public get getContactsByPageQueryHandler(): GetContactsByPageQueryHandler { if (!this._getContactsByPageQueryHandler) { this._getContactsByPageQueryHandler = new GetContactsByPageQueryHandler(this.contactRepository); } return this._getContactsByPageQueryHandler; } // New paginated query handlers public get getUsersByPageQueryHandler(): GetUsersByPageQueryHandler { if (!this._getUsersByPageQueryHandler) { this._getUsersByPageQueryHandler = new GetUsersByPageQueryHandler(this.userRepository); } return this._getUsersByPageQueryHandler; } public get getDecksByPageQueryHandler(): GetDecksByPageQueryHandler { if (!this._getDecksByPageQueryHandler) { this._getDecksByPageQueryHandler = new GetDecksByPageQueryHandler(this.deckRepository); } return this._getDecksByPageQueryHandler; } public get getOrganizationsByPageQueryHandler(): GetOrganizationsByPageQueryHandler { if (!this._getOrganizationsByPageQueryHandler) { this._getOrganizationsByPageQueryHandler = new GetOrganizationsByPageQueryHandler(this.organizationRepository); } return this._getOrganizationsByPageQueryHandler; } public get getChatsByPageQueryHandler(): GetChatsByPageQueryHandler { if (!this._getChatsByPageQueryHandler) { this._getChatsByPageQueryHandler = new GetChatsByPageQueryHandler(this.chatRepository); } return this._getChatsByPageQueryHandler; } } // Export singleton instance export const container = DIContainer.getInstance();