Skip to content

Instantly share code, notes, and snippets.

@afifmohammed
Created May 4, 2015 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afifmohammed/04acf23e0bc2f0ed4f0e to your computer and use it in GitHub Desktop.
Save afifmohammed/04acf23e0bc2f0ed4f0e to your computer and use it in GitHub Desktop.
autofac docoration by hand
void Main()
{
var c = new ContainerBuilder();
c.RegisterInLifetimeScope<Handler, ConcreteReflectionActivatorData>(x => x.RegisterType<Handler>());
var scope = c.Build();
scope.Resolve<IHandler>().Dump();
}
interface IHandler {}
class Handler : IHandler
{
public override string ToString()
{
return "empty";
}
}
class HandlerWrapper : IHandler
{
readonly IHandler handler;
public HandlerWrapper(IHandler handler)
{
this.handler = handler;
}
public override string ToString()
{
return string.Format("Wrapped over an {0} {1}", this.handler, this.handler.GetType());
}
}
// Define other methods and classes here
static class Helpers
{
public static void RegisterInLifetimeScope<THandler, TRegistration>(
this ContainerBuilder container,
Func<ContainerBuilder, IRegistrationBuilder<THandler, TRegistration, SingleRegistrationStyle>> registration)
where TRegistration : IConcreteActivatorData
where THandler : IHandler
{
registration(container)
.InstancePerLifetimeScope()
.PropertiesAutowired()
.Named<THandler>("inner");
container.Register(c =>
{
"resolving..".Dump();
IHandler handler = c.ResolveNamed<THandler>("inner");
var decorated = handler is HandlerWrapper;
if(!decorated) "decorating..".Dump();
//if(decorated) return handler;
return new HandlerWrapper(handler);
})
.InstancePerLifetimeScope()
.PropertiesAutowired()
.AsImplementedInterfaces();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment