Skip to content

Instantly share code, notes, and snippets.

@gulbanana
Last active August 29, 2015 14:16
Show Gist options
  • Save gulbanana/505006a78cfc606bd5f3 to your computer and use it in GitHub Desktop.
Save gulbanana/505006a78cfc606bd5f3 to your computer and use it in GitHub Desktop.
disposables
interface IFooRepository : IDisposable
{
Foo GetFoo();
}
interface IFooRepositoryFactory
{
IFooRepository Create(DisposableCollection disposables);
}
class LoggingFooRepositoryFactory
{
ILoggerFactory LoggerFactory;
IFooRepositoryFactory InnerFactory;
IFooRepository Create()
{
// n+m disposables!
var logger = LoggerFactory.Create();
var inner = InnerFactory.Create();
return new LoggingFooRepository(logger, inner);
}
}
class LoggingFooRepository : IFooRepository {
void Dispose
{
_logger.Dispose();
_inner.Dispose();
}
}
class MemoryFooRepositoryFactory
{
IFooRepository Create(DisposableCollection disposables)
{
// no disposables!
return new MemoryFooRepository();
}
}
class SingletonFooRepositoryFactory
{
// this is a pre-stored instance of Some Type, not a particular one
public IFooRepository Instance;
IFooRepository Create(DisposableCollection disposables)
{
// no disposables, and also you shouldn't dispose even the return value
return Instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment