refactor: change singleton registrations to lazySingleton for improved performance

This commit is contained in:
Achintha Isuru
2026-03-09 15:01:18 -04:00
parent 46ca10933a
commit 7a5c130289
10 changed files with 41 additions and 41 deletions

View File

@@ -13,35 +13,35 @@ class CoreModule extends Module {
@override
void exportedBinds(Injector i) {
// 1. Register the base HTTP client
i.addSingleton<Dio>(() => DioClient());
i.addLazySingleton<Dio>(() => DioClient());
// 2. Register the base API service
i.addSingleton<BaseApiService>(() => ApiService(i.get<Dio>()));
i.addLazySingleton<BaseApiService>(() => ApiService(i.get<Dio>()));
// 3. Register Core API Services (Orchestrators)
i.addSingleton<FileUploadService>(
i.addLazySingleton<FileUploadService>(
() => FileUploadService(i.get<BaseApiService>()),
);
i.addSingleton<SignedUrlService>(
i.addLazySingleton<SignedUrlService>(
() => SignedUrlService(i.get<BaseApiService>()),
);
i.addSingleton<VerificationService>(
i.addLazySingleton<VerificationService>(
() => VerificationService(i.get<BaseApiService>()),
);
i.addSingleton<LlmService>(() => LlmService(i.get<BaseApiService>()));
i.addSingleton<RapidOrderService>(
i.addLazySingleton<LlmService>(() => LlmService(i.get<BaseApiService>()));
i.addLazySingleton<RapidOrderService>(
() => RapidOrderService(i.get<BaseApiService>()),
);
// 4. Register Device dependency
i.addSingleton<ImagePicker>(() => ImagePicker());
i.addLazySingleton<ImagePicker>(() => ImagePicker());
// 5. Register Device Services
i.addSingleton<CameraService>(() => CameraService(i.get<ImagePicker>()));
i.addSingleton<GalleryService>(() => GalleryService(i.get<ImagePicker>()));
i.addSingleton<FilePickerService>(FilePickerService.new);
i.addSingleton<AudioRecorderService>(AudioRecorderService.new);
i.addSingleton<DeviceFileUploadService>(
i.addLazySingleton<CameraService>(() => CameraService(i.get<ImagePicker>()));
i.addLazySingleton<GalleryService>(() => GalleryService(i.get<ImagePicker>()));
i.addLazySingleton<FilePickerService>(FilePickerService.new);
i.addLazySingleton<AudioRecorderService>(AudioRecorderService.new);
i.addLazySingleton<DeviceFileUploadService>(
() => DeviceFileUploadService(
cameraService: i.get<CameraService>(),
galleryService: i.get<GalleryService>(),

View File

@@ -24,20 +24,20 @@ class BillingModule extends Module {
@override
void binds(Injector i) {
// Repositories
i.addSingleton<BillingRepository>(BillingRepositoryImpl.new);
i.addLazySingleton<BillingRepository>(BillingRepositoryImpl.new);
// Use Cases
i.addSingleton(GetBankAccountsUseCase.new);
i.addSingleton(GetCurrentBillAmountUseCase.new);
i.addSingleton(GetSavingsAmountUseCase.new);
i.addSingleton(GetPendingInvoicesUseCase.new);
i.addSingleton(GetInvoiceHistoryUseCase.new);
i.addSingleton(GetSpendingBreakdownUseCase.new);
i.addSingleton(ApproveInvoiceUseCase.new);
i.addSingleton(DisputeInvoiceUseCase.new);
i.addLazySingleton(GetBankAccountsUseCase.new);
i.addLazySingleton(GetCurrentBillAmountUseCase.new);
i.addLazySingleton(GetSavingsAmountUseCase.new);
i.addLazySingleton(GetPendingInvoicesUseCase.new);
i.addLazySingleton(GetInvoiceHistoryUseCase.new);
i.addLazySingleton(GetSpendingBreakdownUseCase.new);
i.addLazySingleton(ApproveInvoiceUseCase.new);
i.addLazySingleton(DisputeInvoiceUseCase.new);
// BLoCs
i.addSingleton<BillingBloc>(
i.addLazySingleton<BillingBloc>(
() => BillingBloc(
getBankAccounts: i.get<GetBankAccountsUseCase>(),
getCurrentBillAmount: i.get<GetCurrentBillAmountUseCase>(),

View File

@@ -16,14 +16,14 @@ class CoverageModule extends Module {
@override
void binds(Injector i) {
// Repositories
i.addSingleton<CoverageRepository>(CoverageRepositoryImpl.new);
i.addLazySingleton<CoverageRepository>(CoverageRepositoryImpl.new);
// Use Cases
i.addSingleton(GetShiftsForDateUseCase.new);
i.addSingleton(GetCoverageStatsUseCase.new);
i.addLazySingleton(GetShiftsForDateUseCase.new);
i.addLazySingleton(GetCoverageStatsUseCase.new);
// BLoCs
i.addSingleton<CoverageBloc>(CoverageBloc.new);
i.addLazySingleton<CoverageBloc>(CoverageBloc.new);
}
@override

View File

@@ -13,7 +13,7 @@ import 'presentation/pages/client_main_page.dart';
class ClientMainModule extends Module {
@override
void binds(Injector i) {
i.addSingleton(ClientMainCubit.new);
i.addLazySingleton(ClientMainCubit.new);
}
@override

View File

@@ -27,7 +27,7 @@ class ViewOrdersModule extends Module {
i.add(GetAcceptedApplicationsForDayUseCase.new);
// BLoCs
i.addSingleton(ViewOrdersCubit.new);
i.addLazySingleton(ViewOrdersCubit.new);
}
@override

View File

@@ -33,7 +33,7 @@ class StaffHomeModule extends Module {
);
// Presentation layer - Cubits
i.addSingleton(
i.addLazySingleton(
() => HomeCubit(
repository: i.get<HomeRepository>(),
getProfileCompletion: i.get<GetProfileCompletionUseCase>(),

View File

@@ -68,7 +68,7 @@ class StaffProfileModule extends Module {
// Presentation layer - Cubit as singleton to avoid recreation
// BlocProvider will use this same instance, preventing state emission after close
i.addSingleton<ProfileCubit>(
i.addLazySingleton<ProfileCubit>(
() => ProfileCubit(
i.get<GetStaffProfileUseCase>(),
i.get<SignOutStaffUseCase>(),

View File

@@ -17,17 +17,17 @@ class FaqsModule extends Module {
@override
void binds(Injector i) {
// Repository
i.addSingleton<FaqsRepositoryInterface>(
i.addLazySingleton<FaqsRepositoryInterface>(
() => FaqsRepositoryImpl(),
);
// Use Cases
i.addSingleton(
i.addLazySingleton(
() => GetFaqsUseCase(
i<FaqsRepositoryInterface>(),
),
);
i.addSingleton(
i.addLazySingleton(
() => SearchFaqsUseCase(
i<FaqsRepositoryInterface>(),
),

View File

@@ -25,29 +25,29 @@ class PrivacySecurityModule extends Module {
@override
void binds(Injector i) {
// Repository
i.addSingleton<PrivacySettingsRepositoryInterface>(
i.addLazySingleton<PrivacySettingsRepositoryInterface>(
() => PrivacySettingsRepositoryImpl(
Modular.get<DataConnectService>(),
),
);
// Use Cases
i.addSingleton(
i.addLazySingleton(
() => GetProfileVisibilityUseCase(
i<PrivacySettingsRepositoryInterface>(),
),
);
i.addSingleton(
i.addLazySingleton(
() => UpdateProfileVisibilityUseCase(
i<PrivacySettingsRepositoryInterface>(),
),
);
i.addSingleton(
i.addLazySingleton(
() => GetTermsUseCase(
i<PrivacySettingsRepositoryInterface>(),
),
);
i.addSingleton(
i.addLazySingleton(
() => GetPrivacyPolicyUseCase(
i<PrivacySettingsRepositoryInterface>(),
),

View File

@@ -26,12 +26,12 @@ class StaffMainModule extends Module {
@override
void binds(Injector i) {
// Register the StaffConnectorRepository from data_connect
i.addSingleton<StaffConnectorRepository>(
i.addLazySingleton<StaffConnectorRepository>(
StaffConnectorRepositoryImpl.new,
);
// Register the use case from data_connect
i.addSingleton(
i.addLazySingleton(
() => GetProfileCompletionUseCase(
repository: i.get<StaffConnectorRepository>(),
),