我想知道 MvvmCross 容器是否可以处理 SQLite 连接的处理,或者是否应该在存储库中手动完成。
我见过如下代码示例,它们不会手动处理或关闭 SQLite 连接:
public class ExampleRepository
{
private readonly ISQLiteConnection _connection;
public ExampleRepository(ISQLiteConnectionFactory factory)
{
_connection = factory.Create("Example.db");
_connection.CreateTable<Example>();
}
public IEnumerable<Example> All()
{
return _connection.Table<Example>().ToList();
}
}
这会是更好的选择吗?
public class ExampleRepository
{
private readonly ISQLiteConnectionFactory _factory;
public ExampleRepository(ISQLiteConnectionFactory factory)
{
_factory = factory;
}
public IEnumerable<Example> All()
{
using (var connection = _factory.Create("Example.db"))
{
connection.CreateTable<Example>();
return connection.Table<Example>().ToList();
}
}
}
每次在存储库中使用连接时,使用 using 块来处理连接会更好吗?
还是继续创建和处理连接的开销更大。因此在第一个示例中将其打开的原因是什么?