For version 1.0, Lamar's only support for interception is decorators. If you look in the Lamar codebase, you'll find dozens of tests that use a fake type named IWidget
:
public interface IWidget
{
void DoSomething();
}
Let's say that we have service registrations in our system for that IWidget
interface, but we want each of them to be decorated by another form of IWidget
like this:
public class WidgetDecorator : IWidget
{
public WidgetDecorator(IThing thing, IWidget inner)
{
Inner = inner;
}
public IWidget Inner { get; }
public void DoSomething()
{
}
}
We can configure Lamar to add a decorator around all other IWidget
registrations with this syntax:
var container = new Container(_ =>
{
// This usage adds the WidgetHolder as a decorator
// on all IWidget registrations
_.For<IWidget>().DecorateAllWith<WidgetDecorator>();
// The AWidget type will be decorated w/
// WidgetHolder when you resolve it from the container
_.For<IWidget>().Use<AWidget>();
_.For<IThing>().Use<Thing>();
});
// Just proving that it actually works;)
container.GetInstance<IWidget>()
.ShouldBeOfType<WidgetDecorator>()
.Inner.ShouldBeOfType<AWidget>();