markiodev / Networker

A simple to use TCP and UDP networking library for .NET. Compatible with Unity.
MIT License
478 stars 77 forks source link

Allow setting implementation of IServiceProvider used in ServerBuilder #24

Closed MorganMoon closed 6 years ago

MorganMoon commented 6 years ago

Added optional argument on IServerBuilder.SetServiceCollection() for creating the IServiceProvider used in ServerBuilder.

I may be missing something, but I wasn't sure how I could inject my own services into packet handlers using another IoC framework because I could't access what IServiceProvider was being used in the ServerBuilder to resolve them. Perhaps this is an acceptable solution? ;)

Example with DryIoc:

var dryIocContainer = new Container().WithCompositionRoot<CompositionRoot>();

var serviceCollection = new ServiceCollection();
var server = new ServerBuilder()
    .UseUdp(5000)
    .SetServiceCollection(serviceCollection, () =>
    {
        dryIocContainer = dryIocContainer.WithDependencyInjectionAdapter(serviceCollection);
        return dryIocContainer.BuildServiceProvider();
    })
    .RegisterPacketHandlerModule<DefaultPacketHandlerModule>()
    .UseZeroFormatter()
    .Build();

server.Start();
markiodev commented 6 years ago

This looks good to me, thanks @MorganMoon 👍

Can you please also add this to the ClientBuilder? There might be some code we can move to a new base class for both of the Builder classes to derive from.

MorganMoon commented 6 years ago

Cool, I'll make those changes aswell as soon as I can. :)