Created
May 17, 2013 07:55
-
-
Save sitereactor/5597596 to your computer and use it in GitHub Desktop.
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
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