Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible to Implement Onion Architecture and DI with WebAPI on OWIN?

I'm attempting to follow an Onion Architecture for a WebAPI services hosted on OWIN/Katana.

Onion Architecture

I have a solution structure like this:

  • DependencyResolution: contains OWIN Startup class and IoC setup
  • WebApi: Web API controllers
  • Infrastructure: Interface Implementations
  • Core: Interfaces

I'd like the DependencyResolution project to inject the dependencies for the WebApi project. The DependencyResolution does have a build task to output to the WebApi project's output folder.

I'm following the approach outlined here, using Autofac and the DotNetDoodle.Owin.Dependencies NuGet package:

http://www.tugberkugurlu.com/archive/owin-dependencies--an-ioc-container-adapter-into-owin-pipeline

However, in registering the services in my Startup class, the call to RegisterApiControllers() will fail. The DepenedencyResolution assembly will be the first to execute so it won't be able to get the assembly containing the ApiContollers (WebAPI assembly):

public IContainer RegisterServices()
{
    ContainerBuilder builder = new ContainerBuilder();

    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    builder.RegisterType<MyRepo>()
           .As<IRepo>()
           .InstancePerLifetimeScope();

    return builder.Build();
}

Is resorting to Assembly.Load() the only viable option here, or should I just forgo the idea of keeping the DependencyResolution project isolated and just refer to it from the WebApi project (seems a little less-than-oniony)?

like image 527
Bullines Avatar asked Nov 11 '22 07:11

Bullines


1 Answers

You can use the name of the Web API assembly to get its instance:

builder.RegisterApiControllers(Assembly.Load("WebApiAssembly"));
like image 92
Ufuk Hacıoğulları Avatar answered Nov 14 '22 23:11

Ufuk Hacıoğulları