simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 154 forks source link

Introduce a NuGet package that supports owin+webapi #55

Closed bitbonk closed 8 years ago

bitbonk commented 9 years ago

When I want to use simpleinjector with owin and webapi I have to to do the integration myself, allthough SimpleInjector.Integration.WebApi.SimpleInjectorWebApiDependencyResolver helps a bit with WebApi, there is still quite some boilerplate I have to write myself, especially for the owin stuff.

Allow me to do the following (that's the owin way):

httpConfig.UseSimpleInjectorMiddleWare(...);
httpConfig.UseSimpleInjectorWebApi(...);

Here is an example on how Ninject does it: https://github.com/ninject/Ninject.Web.WebApi/blob/master/src/Owin-SampleApplication/Startup.cs

dotnetjunkie commented 9 years ago

I'm not sure this is really useful. If I'm not mistaking, the only thing you need to integrate Simple Injector with OWIN is to wrap the request in a scope. As a matter of fact, wrapping the request in a scope this is the only thing that Ninject's UseNinjectMiddleware extension method actually does.

As the OWIN integration page of the Simple Injector documentation shows, starting a scope is as simple as adding the following lines:

app.Use(async (context, next) => {
    using (container.BeginExecutionContextScope()) {
        await next();
    }
});

Am I missing anything? Is there more to it than just wrapping a request in a scope?

bitbonk commented 9 years ago

There are two pieces to the puzzle 1. integrating with owin only and 2. integrating with webapi hosted in owin:

1. integrating with owin only You are right, currently there is not much more to it than the few lines you mentioned. Thank you for pointing me to the documentation. I should have googled a bit more:) But for the sake of the "Super-Duper-Happy-Path" one might consider wrapping that up in an extension method and into a package. For an Owin noob and SimpleInjector noob that I am, it was not obvious to me what to do. Since I knew that a lot of these libs and frameworks that integrate with Owin have these special UseLibXyz() extension methods, I immediately searched for something like that for SimpleInjector.

2. integrating with webapi hosted in owin This is bit more complex because it requires wrapping the request in a scope and registering a SimpleInjectorWebApiDependencyResolver and maybe other things that I am not aware of. For a noob it is not quite clear whether the SimpleInjector.Integration.WebApi package is also the right package when running inside owin.

From a "marketing" and visibility perspective it's probably good when such supporting packages show up in the Nuget feed.

dotnetjunkie commented 9 years ago

For integrating Web API in an Application, there is indeed a SimpleInjector.Integration.WebApi. It contains several useful extension methods that can be used, but unless you know what to look for (i.e. you read the documentation), they are hidden. But for the really lazy developers (such as myself) the SimpleInjector.Integration.WebApi.QuickStart package exists. This simply depends on the SimpleInjector.Integration.WebApi package and adds some code to your Web API project to get you started quickly.

This quick start can be used for OWIN as well. The only thing missing for OWIN is again the wrapping the request with a scope. IF you actually need this; not everybody needs to have a scope from the start of the request. It is usually enough when there is a request when the ApiController is executed, which is something the Web API integration package does.

I think the problem lies not so much in the omission of a Web API hosted in OWIN integration package, but rather in the visibility of the quick start package. And this is primarily a problem that is caused by the completely broken search functionality of NuGet. Just type in 'simpleinjector' in the Package Manager search window and you'll understand what's wrong. Although we can create a SimpleInjector.Integration.Owin.QuickStart NuGet package, this is only useful if users can actually find this package.

The broken NuGet search functionality is a big frustration I have for years. The NuGet teams is promising for a long time to fix this, but they still didn't do this.