autofac / Autofac.Owin

OWIN integration for Autofac
MIT License
23 stars 15 forks source link

Allow DI for IAppBuilder.Run #6

Closed srogovtsev closed 8 years ago

srogovtsev commented 9 years ago

Now, after we got rid of one unpleasantry, let's move to another one.

Terminal call.

app
  .UseBasicAuthentication()
  .Use(AuthenticatedOnly)
  .Run(container.Resolve<Uploader>().InvokeAsync);

I want to be able to specify end-of-pipeline delegate (unfortunately, only delegates are allowed) without explicitly resolving the dependencies needed inside.

Option 1

app.Run<Uploader>((uploader, owinContext) => uploader.InvokeAsync(owinContext));

Which should be more or less equivalent to

app.Run(owinContext =>
  owinContext.GetAutofacLifetimeScope().Resolve<Uploader>().InvokeAsync(owinContext)
  );

Option 2

Task UploadAsync(IOwinContext, Dependency1, Dependency2);

app.Run(UploadAsync);

Equivalent to

app.Run(owinContext => {
  var lifetimeScope = owinContext.GetAutofacLifetimeScope();
  return UploadAsync(
    owinContext,
    lifetimeScope.Resolve<Dependency1>(),
    lifetimeScope.Resolve<Dependency2>()
    );
  });

This one is actually beautiful from the user point of view - if you not take into account the necessity to provide for 15 variants of Func<Task,IOwinContext,Dependency1,...,DependencyN>.

I could do either - or both - if you deem this appropriate.

tillig commented 9 years ago

I think option 1 sounds like the simplest thing to implement and would avoid that 15-overload-nightmare you mention at the end.

To disambiguate, I'm curious if it should be called something like RunFromContainer<T> to go along with the UseMiddlewareFromContainer<T> method we have. I don't feel super strongly about it, just that I could see Microsoft.Owin adding some sort of simple generic in the future and messing method bindings up.

srogovtsev commented 9 years ago

Okay, I agree with the naming: I thought that way myself, but settled first on the shorter option.

Will try to implement option 1 in a couple of days.

tillig commented 8 years ago

Fixed by merging in PR #8.