BusinessServiceFactory.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 : IBusinessServiceFactory
  18. {
  19. public BusinessServiceFactory(
  20. IServiceProvider serviceProvider,
  21. IDbContextFactory<MainDBContext> mainDBContextFactory)
  22. {
  23. this.serviceProvider = serviceProvider;
  24. this.mainDBContextFactory = mainDBContextFactory;
  25. }
  26. private readonly IServiceProvider serviceProvider;
  27. private readonly IDbContextFactory<MainDBContext> mainDBContextFactory;
  28. public async Task<IBusinessService> CreateBusinessService(string customerId)
  29. {
  30. bool isCallOut = false;
  31. using (var db = this.mainDBContextFactory.CreateDbContext())
  32. {
  33. isCallOut = await db.Customer.Where(x => x.Id == new Guid(customerId)).Select(x => x.CallPartnerApiOnSchedule).FirstOrDefaultAsync();
  34. }
  35. //return isCallOut ? new OuterBusinessService(customerId) : new LocalBusinessService(customerId);
  36. if (isCallOut)
  37. {
  38. OuterBusinessService outerBusinessService = serviceProvider.GetService<OuterBusinessService>();
  39. outerBusinessService.CustomerId = customerId;
  40. return outerBusinessService;
  41. }
  42. LocalBusinessService toReturn = serviceProvider.GetService<LocalBusinessService>();
  43. toReturn.CustomerId = customerId;
  44. return toReturn;
  45. //return isCallOut ? new OuterBusinessService(customerId) :
  46. }
  47. }
  48. }