using CncRepository.Impl; using CncRepository.Impl.Dashboard; using CncRepository.Impl.Log; using CncService.Impl; namespace CncService.Tests { /// /// Service工厂 —— 封装Repository创建,提供所有Service实例化方法 /// 所有Service使用真实数据库连接串,进行集成测试 /// public static class ServiceFactory { private static readonly string ConnStr = TestDb.ConnectionString; private static readonly string ConnStrLog = TestDb.ConnectionString; // ======== Repository 创建(业务库) ======== private static WorkshopRepository NewWorkshopRepo() => new WorkshopRepository(ConnStr); private static BrandRepository NewBrandRepo() => new BrandRepository(ConnStr); private static BrandFieldMappingRepository NewMappingRepo() => new BrandFieldMappingRepository(ConnStr); private static CollectAddressRepository NewCollectAddressRepo() => new CollectAddressRepository(ConnStr); private static MachineRepository NewMachineRepo() => new MachineRepository(ConnStr); private static WorkerRepository NewWorkerRepo() => new WorkerRepository(ConnStr); private static WorkerMachineRepository NewWorkerMachineRepo() => new WorkerMachineRepository(ConnStr); private static SysConfigRepository NewSysConfigRepo() => new SysConfigRepository(ConnStr); private static AlertRepository NewAlertRepo() => new AlertRepository(ConnStr); private static DailyProductionRepository NewDailyProductionRepo() => new DailyProductionRepository(ConnStr); private static ProductionSegmentRepository NewProductionSegmentRepo() => new ProductionSegmentRepository(ConnStr); private static ProductionAdjustmentRepository NewProductionAdjustmentRepo() => new ProductionAdjustmentRepository(ConnStr); private static ScreenConfigRepository NewScreenConfigRepo() => new ScreenConfigRepository(ConnStr); private static ScreenFilterRepository NewScreenFilterRepo() => new ScreenFilterRepository(ConnStr); private static DashboardRepository NewDashboardRepo() => new DashboardRepository(ConnStr); private static SystemLogRepository NewSystemLogRepo() => new SystemLogRepository(ConnStrLog); // ======== Repository 创建(日志库) ======== private static CollectorHeartbeatRepository NewCollectorHeartbeatRepo() => new CollectorHeartbeatRepository(ConnStrLog); private static CollectRawRepository NewCollectRawRepo() => new CncRepository.Impl.Log.CollectRawRepository(ConnStrLog); // ======== Service 创建 ======== /// 创建AuthService,需指定JWT密钥 public static AuthService CreateAuthService(string jwtSecret = "test-jwt-secret-key-for-unit-testing-2024") { return new AuthService(NewSysConfigRepo(), jwtSecret); } /// 创建WorkshopService public static WorkshopService CreateWorkshopService() { return new WorkshopService(NewWorkshopRepo()); } /// 创建BrandService public static BrandService CreateBrandService() { return new BrandService(NewBrandRepo(), NewMappingRepo(), NewCollectAddressRepo()); } /// 创建MachineService public static MachineService CreateMachineService() { return new MachineService(NewMachineRepo(), NewCollectAddressRepo(), NewWorkerMachineRepo(), NewBrandRepo()); } /// 创建CollectAddressService public static CollectAddressService CreateCollectAddressService() { return new CollectAddressService(NewCollectAddressRepo(), NewMachineRepo(), NewBrandRepo(), NewWorkshopRepo(), NewCollectRawRepo()); } /// 创建WorkerService public static WorkerService CreateWorkerService() { return new WorkerService(NewWorkerRepo(), NewWorkerMachineRepo(), NewMachineRepo()); } /// 创建ProductionService public static ProductionService CreateProductionService() { return new ProductionService(NewDailyProductionRepo(), NewProductionSegmentRepo(), NewProductionAdjustmentRepo()); } /// 创建AlertService public static AlertService CreateAlertService() { return new AlertService(NewAlertRepo()); } /// 创建ScreenService public static ScreenService CreateScreenService() { return new ScreenService(NewScreenConfigRepo(), NewScreenFilterRepo(), NewWorkshopRepo()); } /// 创建SystemLogService public static SystemLogService CreateSystemLogService() { return new SystemLogService(NewSystemLogRepo()); } /// 创建DashboardService public static DashboardService CreateDashboardService() { return new DashboardService(NewDashboardRepo(), NewCollectorHeartbeatRepo()); } /// 创建CollectDataService public static CollectDataService CreateCollectDataService() { return new CollectDataService(NewCollectRawRepo()); } } }