Created
May 4, 2015 03:24
-
-
Save afifmohammed/04acf23e0bc2f0ed4f0e to your computer and use it in GitHub Desktop.
autofac docoration by hand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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