-
-
Save stevetalkscode/e09c7c679b6117ee6ec2f9c636f439d9 to your computer and use it in GitHub Desktop.
Example of Wrapping a disposable class to hide the Dispose method
This file contains 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
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