BusinessServiceFactory.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. {
  11. public interface IBusinessService
  12. {
  13. Task<IdTokenInfo> Authorize(string chargeBoxId, string idTag);
  14. Task NotifyFaultStatus(ErrorDetails details);
  15. Task NotifyConnectorUnplugged(string chargeBoxId,string data);
  16. }
  17. public class BusinessServiceFactory
  18. {
  19. public BusinessServiceFactory(IServiceProvider serviceProvider, IDbContextFactory<MainDBContext> mainDBContextFactory)
  20. {
  21. this.serviceProvider = serviceProvider;
  22. this.mainDBContextFactory = mainDBContextFactory;
  23. }
  24. private readonly IServiceProvider serviceProvider;
  25. private readonly IDbContextFactory<MainDBContext> mainDBContextFactory;
  26. public async Task<IBusinessService> CreateBusinessService(string customerId)
  27. {
  28. bool isCallOut = false;
  29. using (var db = await mainDBContextFactory.CreateDbContextAsync())
  30. {
  31. isCallOut = db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).SingleOrDefault();
  32. }
  33. //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId);
  34. if(isCallOut)
  35. {
  36. OuterBusinessService outerBusinessService = serviceProvider.GetService<OuterBusinessService>();
  37. outerBusinessService.CustomerId = customerId;
  38. return outerBusinessService;
  39. }
  40. LocalBusinessService toReturn = serviceProvider.GetService<LocalBusinessService>();
  41. toReturn.CustomerId = customerId;
  42. return toReturn;
  43. //return isCallOut ? new OuterBusinessService(customerId) :
  44. }
  45. }
  46. }