- To be able to use the
Hub
in both Domain.Core
and App projects, how should I wire it all up?
Domain layer:
IMyNotifier
interface
NullMyNotifier
null implementation
public interface IMyNotifier
{
Task SendMessage(IUserIdentifier user, string message);
}
public class NullMyNotifier : IMyNotifier
{
public static NullMyNotifier Instance { get; } = new NullMyNotifier();
private NullMyNotifier()
{
}
public Task SendMessage(IUserIdentifier user, string message)
{
return Task.FromResult(0);
}
}
Web layer:
- SignalR hub implementation, e.g.
MyChatHub
SignalRMyNotifier
concrete implementation
public class SignalRMyNotifier : IMyNotifier, ITransientDependency
{
private readonly IOnlineClientManager _onlineClientManager;
private readonly IHubContext<MyChatHub> _hubContext;
public SignalRMyNotifier(
IOnlineClientManager onlineClientManager,
IHubContext<MyChatHub> hubContext)
{
_onlineClientManager = onlineClientManager;
_hubContext = hubContext;
}
public async Task SendMessage(IUserIdentifier user, string message)
{
var onlineClients = _onlineClientManager.GetAllByUserId(user);
foreach (var onlineClient in onlineClients)
{
var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId);
await signalRClient.SendAsync("getMessage", message);
}
}
}
Usage, in any layer that references Domain layer:
public class MyDomainService : DomainService, IMyManager
{
public IMyNotifier MyNotifier { get; set; }
public MyDomainService()
{
MyNotifier = NullMyNotifier.Instance;
}
public async Task DoSomething()
{
// Do something
// ...
var hostAdmin = new UserIdentifier(null, 1);
var message = "Something done";
await MyNotifier.SendMessage(hostAdmin, message);
}
}
- Can I move entire SignalR hub to a new module and reference it from App, Domain and Web layers?
You can, but the new module that contains the SignalR hub (and thus your App and Domain layers) would depend on Microsoft.AspNetCore.SignalR
, which depends on Microsoft.AspNetCore.Http.Connections
. Domain layer should not depend on Http
.