NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.46k forks source link

[Discuss] DependencyInjection design didn't work with MS extensions #2689

Open hcoona opened 7 years ago

hcoona commented 7 years ago

This is not a bug report but a discussion about the design of DI componments in Nancy.

I notice Nancy work perfectly with Ninja, Autofac, Unity, etc. However I found it impossible to use Microsoft.Extensions.DependencyInjection framework in Nancy.

I recommend to support it because it provide a common interface for different DI frameworks just like what Common.Logging does. It's valuable for users to use a abstract interface against diverse frameworks.

The key point MS DI framework cannot fit into Nancy is it use a 2-step way to build a service resolver. The main idea is to use IServiceCollection for service register and IServiceProvider for the service resolving. IServiceProvider also maintain the scoped lifetime for resolved service from it.

Nancy regard them as one unified thing IContainer, which made it impossible to work with MS DI framework. I suggest to refactor our code to seperate the IContainer into 2 parts as different actors, which would work perfactly with MS DI framework. It would also work fine with the existing codes if we fill in the same type for the 2 actors.

To make it clear, I want

class NancyBootstrapperWithRequestContainerBase<TServiceCollection, TServiceProvider>

instead of

class NancyBootstrapperWithRequestContainerBase<TContainer>

And we are able to keep the compatibility with existing code by

class NinjaBootstrapper : NancyBootstrapperWithRequestContainerBase<TContainer, TContainer>
khellang commented 7 years ago

You mean like this and this? Seriously, we're painfully aware of this :smile:

hcoona commented 7 years ago

Cool! Exactly what I want.

It could be somehow painful but was worth it. 👍

khellang commented 7 years ago

Also related; https://github.com/NancyFx/Nancy.Bootstrappers.Autofac/issues/64

caesay commented 5 years ago

@khellang hey, can you elaborate on how you suggest using Microsoft DI with Nancy? has there been any movement on this since 2017?