BusinessServiceFactory.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using EVCB_OCPP.Domain;
  2. using EVCB_OCPP.Packet.Messages.SubTypes;
  3. using EVCB_OCPP.WSServer.Dto;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace EVCB_OCPP.WSServer.Service;
  10. public interface IBusinessService
  11. {
  12. Task<IdTokenInfo> Authorize(string chargeBoxId, string idTag);
  13. Task NotifyFaultStatus(ErrorDetails details);
  14. Task NotifyConnectorUnplugged(string chargeBoxId,string data);
  15. }
  16. public static class BusinessServiceFactoryRegistration
  17. {
  18. public static void AddBusinessServiceFactory(this IServiceCollection services)
  19. {
  20. services.AddSingleton<OuterBusinessService>();
  21. services.AddSingleton<LocalBusinessService>();
  22. services.AddSingleton<IBusinessServiceFactory, BusinessServiceFactory>();
  23. }
  24. }
  25. public class BusinessServiceFactory : IBusinessServiceFactory
  26. {
  27. public BusinessServiceFactory(
  28. IServiceProvider serviceProvider,
  29. IDbContextFactory<MainDBContext> mainDBContextFactory)
  30. {
  31. this.serviceProvider = serviceProvider;
  32. this.mainDBContextFactory = mainDBContextFactory;
  33. }
  34. private readonly IServiceProvider serviceProvider;
  35. private readonly IDbContextFactory<MainDBContext> mainDBContextFactory;
  36. public async Task<IBusinessService> CreateBusinessService(string customerId)
  37. {
  38. bool isCallOut = false;
  39. using (var db = this.mainDBContextFactory.CreateDbContext())
  40. {
  41. isCallOut = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).FirstOrDefaultAsync();
  42. }
  43. //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId);
  44. if (isCallOut)
  45. {
  46. OuterBusinessService outerBusinessService = serviceProvider.GetService<OuterBusinessService>();
  47. outerBusinessService.CustomerId = customerId;
  48. return outerBusinessService;
  49. }
  50. LocalBusinessService toReturn = serviceProvider.GetService<LocalBusinessService>();
  51. toReturn.CustomerId = customerId;
  52. return toReturn;
  53. //return isCallOut ? new OuterBusinessService(customerId) :
  54. }
  55. }