Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

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

Open hcoona opened this issue 8 years ago • 4 comments

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>

hcoona avatar Jan 24 '17 08:01 hcoona

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

khellang avatar Jan 24 '17 10:01 khellang

Cool! Exactly what I want.

It could be somehow painful but was worth it. 👍

hcoona avatar Jan 24 '17 10:01 hcoona

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

khellang avatar Jan 24 '17 12:01 khellang

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

caesay avatar Jul 05 '19 18:07 caesay