Using Autofac, I can register a class to resolve against an interface using property injection, using the following code:
builder.RegisterType
Update: Building on the LogInjectionModule sample and how Autofac does property injection, I have extended the module to do both constructor and property injection.
Note: I've fixed the type passed to LogManager
in OnComponentPreparing
to use the declaring type. This makes e.g. Resolve
use the correct log type.
using System.Linq;
using log4net;
public class LogInjectionModule : Module
{
protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
{
registration.Preparing += OnComponentPreparing;
registration.Activating += OnComponentActivating;
}
private static void OnComponentActivating(object sender, ActivatingEventArgs
On how to use the module, here's a sample:
public class Service
{
public Service(ILog log) { ... }
}
var cb = new ContainerBuilder();
cb.RegisterModule();
cb.RegisterType();
var c = cb.Build();
var service = c.Resolve();