我正在查看一个DDD项目的开源示例代码DDD Sample.Net,并发现了一个“有趣”的SQLite基本测试夹具(链接中包含完整代码)。
通常我使用SQLite的连接字符串:
这样做的优点是什么?谢谢,Berryl。
编辑:
我怀疑这样做的好处可能是因为下面那个类中的代码平衡表明作者既不是粗鄙无知的人,也不是天真幼稚的人。优点可能不会是性能。我猜想这可能使SQLite在测试运行期间更加可靠,但这就是我问的原因:--)
通常我使用SQLite的连接字符串:
"Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1")
这位作者正在使用一个DbFile(在每个测试中设置/删除):
"Data Source={0};Version=3;New=True;", DatabaseFile)
这样做的优点是什么?谢谢,Berryl。
编辑:
我怀疑这样做的好处可能是因为下面那个类中的代码平衡表明作者既不是粗鄙无知的人,也不是天真幼稚的人。优点可能不会是性能。我猜想这可能使SQLite在测试运行期间更加可靠,但这就是我问的原因:--)
protected IDisposable Scope(bool transactional)
{
return new ScopeImpl(SessionFactory, transactional);
}
protected IDisposable Scope(bool transactional, string description)
{
Console.WriteLine(description);
return Scope(transactional);
}
private class ScopeImpl : IDisposable
{
private readonly ISessionFactory _sessionFactory;
public ScopeImpl(ISessionFactory sessionFactory, bool transactional)
{
_sessionFactory = sessionFactory;
ISession session = _sessionFactory.OpenSession();
if (transactional)
{
session.BeginTransaction();
}
CurrentSessionContext.Bind(session);
}
public void Dispose()
{
ISession session = CurrentSessionContext.Unbind(_sessionFactory);
if (!IsInExceptionContext())
{
if (session.Transaction != null)
{
session.Transaction.Commit();
session.Transaction.Dispose();
}
}
session.Close();
}
/// <summary>
/// Checks if current code is running in finally block ater throwing exception.
/// </summary>
private static Boolean IsInExceptionContext()
{
return Marshal.GetExceptionPointers() != IntPtr.Zero || Marshal.GetExceptionCode() != 0;
}