Question

I'm trying to figure out how separation should work with ASP.NET and dependency injection.

I have four projects:

  • ASP.NET Client
  • BusinessLogic class lib (BL)
  • Integration class lib, calling Service references
  • Shared (Interfaces, Models)

In Integration I have a repository, which calls other services. The repository "MyRepository" implements an interface "IMyRepository", which is placed in Shared, and returns objects which are also placed in Shared.

Strictly speaking, I would not like a reference/dependency from the Client to the Integration-project, but all communication should happen through BL.

In the Client's Global.asax, I register my types with Autofac

var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacWebTypesModule());
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<MyRepository>().As<IMyRepository>().InstancePerHttpRequest();

However this requires the Client-project to reference the Integration-project for the implementation of MyRepository. Should it be this way?

How can I not have a reference from Client to Integration and keep the separation clean?

My real world project is on a much larger scale than this, so I'm trying to untangle the dependencies.

Thanks for your time!

Was it helpful?

Solution

My normal approach is:

  1. Define a way to discover types in each individual assembly. Easiest approach (if you do not mind referencing Autofac in each) is to put an Autofac Module in each assembly. If you do not like that, you can define your own discovery abstraction (for example, MEF-like attributes).

  2. Discover all assemblies. Easiest approach is to scan the bin folder, but you can use some custom configuration. I haven't checked latest versions of Autofac add anything in terms of module discovery, but previously I did it manually.

  3. Use previously defined type discovery approach to register all relevant types from each discovered assembly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top