autofac / Autofac

An addictive .NET IoC container
https://autofac.org
MIT License
4.44k stars 836 forks source link

Resolving Circular Dependencies w/ Required Properties #1379

Closed TonyValenti closed 1 year ago

TonyValenti commented 1 year ago

Problem Statement

When required properties are used, it is possible for AutoFac to resolve circular dependencies. So it should!

How it should work

When required properties are used, it can be possible for AutoFac to resolve circular dependencies. It can do this by partially constructing the object and its dependencies via constructor injection And then filling in all the outstanding dependencies via property injection.

This could work by doing the following:

  1. Create the requested object and only inject constructor args.
  2. Create any dependent objects and only inject constructor args.
  3. Add items from 2 and 3 into a "pool" and resolve any required properties using items from the pool.

Desired Solution

AutoFac can resolve circular dependencies when injected via required properties.

Alternatives You've Considered

Leave it as is.

Additional Context

Resolving circular dependencies would be awesome.

TonyValenti commented 1 year ago

Actually, looking through the docs, it seems like AutoFac already has something like this when using PropertiesAutowired. I would like to suggest that this be carried over to required properties as well.