WebStreams / WebStreams.Server

Serve APIs returning & consuming observable streams over WebSockets using .NET
Apache License 2.0
80 stars 6 forks source link

Dependency Injection #2

Closed chriseldredge closed 10 years ago

chriseldredge commented 10 years ago

Does this project support external instantiation of controller types? It would be useful if projects can wire in their own DI container like Autofac or Ninject. Perhaps it is already supported and just needs documentation.

ReubenBond commented 10 years ago

Hi Chris,

Injecting pre-constructed controllers is not yet supported, but it's a valuable feature. How should dependency injection look?

Check out the construction of controllers in StreamControllerManager.GetStreamController. Very simple, one-line method which uses Activator.CreateInstance and stuffs that value into a concurrent dictionary:

public object GetStreamController(Type type)
{
    return this.streamControllers.GetOrAdd(type, Activator.CreateInstance);
}

I wonder how ASP.NET vNext does DI.

chriseldredge commented 10 years ago

At a minimum, allowing clients to provide their own subclass (or extracting an interface) for StreamControllerManager, and making GetStreamController virtual would be a start. That way clients can provide their own shims that hook into their container of choice.

chriseldredge commented 10 years ago

It may make sense to allow clients to decide if their controllers should be singleton or bound to request as well, so I would not cache the result of GetStreamController when a client overrides it.

ReubenBond commented 10 years ago

I've pushed an updated version which allows users to specify the delegate used to construct controllers when calling app.UseWebStream(...) - and I updated the wiki page to match.

Let me know what you think.

By the way, I renamed the package to Dapr.WebStreams.Server (s/WebStream/WebStreams/)

ReubenBond commented 10 years ago

Fixed