So I had not touched this project in 2 months and decided to get back into it seeing that the Habbo flash client isnt dead and they're actually bringing snowstorm back :D
Couple things that I noticed was the total clusterfuck of message subscriptions which is what I spend the last couple days on working on refactoring.
For example given an ExampleService which would require some additional dependencies and registers subscriptions on messages would look like something like this:
Code:
public class ExampleService : IExampleService
{
public ExampleService(IGameServerConnectionRegistry connectionRegistry)
{
connectionRegistry.Subscribe<UserUsernameUpdatedNotification>(OnUserUsernameUpdatedNotification);
}
private Task OnUserUsernameUpdatedNotification(UserUsernameUpdatedNotification payload)
{
return DoSomething();
}
public Task DoSomething()
{
return Task.CompletedTask;
}
}
Now, this in itself isnt that bad. It just gets bad when you have a lot of subscriptions as the class file would grow in since quite quickly and any dependecies for those message handlers would also have to be injected.
I changed this around so that I now use a NotificationHandler for Events / Notifications and RequestHandler for Requests (Requires reply)
Code:
public class UserUsernameUpdatedNotificationHandler : NotificationHandler<UserUsernameUpdatedNotification>
{
private readonly IExampleService _exampleService;
public UserUsernameUpdatedNotificationHandler(IExampleService exampleService)
{
_exampleService = exampleService;
}
public override Task Handle(UserUsernameUpdatedNotification payload)
{
return _exampleService.DoSomething();
}
}
All handlers will be automatically registered. I also have to only inject the dependencies it really needs whereas the services before would get bloated quickly with multiple dependencies.
The other upside of this is that they're smaller in general, standalone and dont have any dependencies on their own. Its very easy to test them due to the minimal dependencies.
There is no need to manually register any handlers as they'll be automatically scanned.
Code:
services.Scan(scan => scan.FromAssemblies(Assembly.GetAssembly(typeof(CoreModule))).
AddClasses(classes => classes.Where(c => c.IsAssignableTo(typeof(IRequestHandler))))
.AsImplementedInterfaces()
.WithScopedLifetime());