I have two modules, Notification which basically contains SignalR hubs and CoreSetup which contains core setup logic in it.
I have a class in CoreSetup module which keeps track of employee location . I want SignalR hubs to be notified and those locations to be on MAP when new data are inserted through Web API. I don't want CoreSetup module to have dependency on SignalR module. This is what I have done currently.
[Route("api/salesman-location")]
[ApiController]
public class SalesmanLocationController : BaseController
{
private readonly SalesmanLocationService _salesmanLocationService;
private readonly IHubContext<ChatHub> _hubContext;
public SalesmanLocationController(SalesmanLocationService salesmanLocationService,IHubContext<ChatHub> hubContext)
{
_salesmanLocationService = salesmanLocationService;
_hubContext = hubContext;
}
[Route("save")]
[HttpPost]
public IActionResult save([FromBody]List<SalesmanLocationDto> datas)
{
try
{
_hubContext.Clients.All.SendAsync("SalesManLocationfromHub", datas);
_salesmanLocationService.save(datas);
return Ok();
}
catch (CustomException ex)
{
return ApiResponse.getErrorResponseJson(ex.Message);
}
catch (Exception ex)
{
return ApiResponse.getErrorResponseJson("Failed to save data.");
}
}
}
It works pretty well but I have some logic inside save() method of SalesmanLocationService . It would be great if hub can subscribe to save() method of SalesmanLocationService without SalesmanLocationService knowing anything about Hub.Under some condition, I should not broadcast data through hub.
This is how my interface of SalesmanLocationService looks like:
public interface SalesmanLocationService
{
void save(List<SalesmanLocationDto> datas);
}
This is the actual implementation of SalesmanLocationService
public class SalesmanLocationServiceImpl : SalesmanLocationService
{
public SalesmanLocationServiceImpl()
{
}
public void save(List<SalesmanLocationDto> datas)
{
foreach (var data in datas)
{
save(data);
}
}
private void save(SalesmanLocationDto data)
{
if(someCondition){
//broadcast to hub
}
}
}