我正在使用AutoFixture自定义来测试访问SQL Compact DB的存储库.
测试完成后立即删除此数据库对我非常有帮助.因为db是在自定义构造函数中创建的,所以我认为删除它的最佳位置是dispose方法.
我想的代码是:
internal class ProjectRepositoryCustomization : ICustomization
{
private readonly String _dbLocation;
public ProjectRepositoryCustomization()
{
var tempDbLocation = Path.Combine(Path.GetTempPath(), "TempDbToDelete");
if (!Directory.Exists(tempDbLocation))
{
Directory.CreateDirectory(tempDbLocation);
}
_dbLocation = Path.Combine(tempDbLocation, Guid.NewGuid().ToString("N") + ".sdf");
}
public void Customize(IFixture fixture)
{
DataContextConfiguration.database = _dbLocation;
var dataContextFactory = new BaseDataContextFactory();
var projRepository = new ProjectRepository(dataContextFactory);
fixture.Register(() => projRepository);
}
public void Dispose()
{
if (File.Exists(_dbLocation))
{
File.Delete(_dbLocation);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有可能做类似的事情吗?