Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created August 8, 2012 07:29
Show Gist options
  • Save mikeminutillo/3293123 to your computer and use it in GitHub Desktop.
Save mikeminutillo/3293123 to your computer and use it in GitHub Desktop.
Use Fake implementations at debug time
var builder = new ContainerBuilder();
builder.AddModule(new ModuleA());
builder.AddModule(new ModuleB());
builder.AddModule(new DebugFakesModule());
var container = builder.Build();
// Will be FakeEmailService if compiled with DEBUG
var emailService = builder.Resolve<IEmailService>();
class DebugFakesModule : Module
{
protected override void Load(ContainerBuilder builder)
{
#if DEBUG
builder.RegisterAssemblyTypes(ThisAssembly)
.Where(x => x.Name.StartsWith("Fake"))
.AsImplementedInterfaces()
.SingleInstance();
#endif
}
}
// Only gets used when compiled as a DEBUG compiler switch
class FakeMailService : IEmailService
{
// Fake impl
}
class EmailService : IEmailService
{
// normal impl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment