Skip to content

Instantly share code, notes, and snippets.

@sitereactor
Created May 17, 2013 07:55
Show Gist options
  • Save sitereactor/5597596 to your computer and use it in GitHub Desktop.
Save sitereactor/5597596 to your computer and use it in GitHub Desktop.
internal class ServiceContextManager : IDisposable
{
private readonly string _connectionString;
private readonly string _providerName;
private ServiceContext _serviceContext;
private readonly StandaloneApplication _application;
public ServiceContextManager(string connectionString, string providerName)
{
_connectionString = connectionString;
_providerName = providerName;
_application = StandaloneApplication.GetApplication();
_application.Start();
}
public ServiceContext Services
{
get
{
if (_serviceContext == null)
{
var dbFactory = new DefaultDatabaseFactory(_connectionString, _providerName);
var dbContext = new DatabaseContext(dbFactory);
Database.Mapper = new PetaPocoMapper();
_serviceContext = new ServiceContext(
new PetaPocoUnitOfWorkProvider(dbFactory),
new FileUnitOfWorkProvider(),
new PublishingStrategy());
//initialize the DatabaseContext
dbContext.Initialize(_providerName);
}
return _serviceContext;
}
}
public void Dispose()
{
((IDisposable)ApplicationContext.Current).Dispose();
_application.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment