Skip to content

Instantly share code, notes, and snippets.

@actaneon
Created March 2, 2010 18:54
Show Gist options
  • Save actaneon/319792 to your computer and use it in GitHub Desktop.
Save actaneon/319792 to your computer and use it in GitHub Desktop.
Composition and IoC for OCP Adherence
//Using Composition and IoC to add a logging to the IService implementation without violating OCP.
//Instantiate with: For<IService>.Use<LoggingService>().Ctor<IService>().Is<SomeService>();
//
//create a LoggingService : IService that takes the *real* IService in it's c'tor
public class LoggingService : IService
{
private IService _realService;
private ILogger _log;
public LoggingService(IService realService, ILogger log)
{
_realService = realService;
_log = log;
}
public void DoStuff()
{
_log.Log("Doing some stuff");
_realService.DoStuff();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment