我使用Xamarin Forms和Shared项目使用Azure NOSQL DocumentDB连接到DocumentDB。我有一个连接到数据库的服务:DocumentDb - DocumentClient生命周期管理
public class PaymentService : IPaymentService<Payment>, IDisposable
到目前为止,我一直保持着类级别属性为客户:
public DocumentClient Client { get; set; }
我在Dispose方法处置。
服务类有一次我叫一个连接方法,在我所有的方法GETALL,GetSingle,更新重用它的构造函数,删除等
public void Connect()
{
try
{
if (Client == null)
{
Client = new DocumentClient(new Uri(SUBSCRIPTION_URL), PRIMARY_KEY);
}
}
catch (DocumentClientException de)
{
...
}
catch (Exception e)
{
...
}
}
我已经看到了一些文章,其中DocumentClient是使用每个方法的语句在中按请求进行管理。
public async Task<bool> Delete(string guid)
{
using (var client = new DocumentClient(new Uri(SUBSCRIPTION_URL), PRIMARY_KEY))
{
var result = await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DATABASE_ID, COLLECTION_ID, guid));
var item = GetSingle(guid);
if (item != null)
{
return false;
}
return true;
}
}
我已经尝试了这两种方法,但发现使用using语句非常慢。
我的问题是:什么被认为是管理DocumentClient生命周期的最佳实践?
嘿那里谢谢你的答案 - 我走了你描述的路线。我看到的文章是从MS - https://msdn.microsoft.com/library/azure/microsoft.azure.documents.client.documentclient.upsertdocumentasync.aspx我可能已经把它取出了上下文。单实例是基于性能的方式。 –