Suppose I have the following:
public class Test
{
public IDependency Dependency { get; set; }
}
IDependency
and Test
are registered in my Autofac builder. My IDependency
resolution works fine, but what I'm trying to achieve is to be able to return new instances of Test
in the code (e.g. return new Test();
) and have it's IDependency
properties prepopulated by Autofac.
What I've tried in my container:
builder.Register(x => new Test {
x.Resolve<IDependency>()
});
However, everytime I new Test()
in the code, it's IDependency
property is null
. Is that possible?
(for reference, my train of thought leading to this attempt was that I initially did constructor injection for Test
but then realized that in some cases I need to manually construct new Test()
instances in the code, and couldn't figure out what to put to satisfy the constructor signature public Test(IDependency dependency)
and still have Autofac resolve that dependency)