Skip to content

Instantly share code, notes, and snippets.

@stevetalkscode
Last active Nov 14, 2020
Embed
What would you like to do?
Example of Wrapping a disposable class to hide the Dispose method
public interface IDoSomething
{
void DoSomething();
}
public class SomeDisposableWrapper : IDoSomething, IDisposable
{
private readonly SomeDisposable _instance;
private readonly ILogger _logger;
public SomeDisposableWrapper(ILogger<SomeDisposableWrapper> logger)
{
_instance = new SomeDisposable();
_logger = logger;
}
public void DoSomething()
{
_logger.LogTrace("DoSomething has been started");
_instance.DoSomething();
_logger.LogTrace("DoSomething has been ended");
}
public void Dispose()
{
_instance.Dispose();
}
}
public class StartUp
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddSingleton<IDoSomething, SomeDisposableWrapper>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment