gpt4 book ai didi

azure - 保留RegistryManager实例是一个好习惯吗?

转载 作者:行者123 更新时间:2023-12-03 04:20:52 26 4
gpt4 key购买 nike

我想知道保留RegistryManager实例而不是在每个方法中创建新实例是否是一个好习惯。

只是为了提供更多解释,它会有所不同,因为如果我保留一个实例而不是在每个方法中创建一个实例,我必须向树的所有级别公开一个 Dispose 方法。

为了解决我的问题,下面的代码展示了两种方法:

1 - 处置模式方法(我想避免):

public class IOTHubDeviceService : IDispose {
private RegistryManager _registryManager;

public IOTHubFacade(string iotHubConnectionString)
{
_registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);
}

public async Task<Device> AddDeviceAsync(Device device)
{
return await _registryManager.AddDeviceAsync(device);
}

public void Dispose()
{
_registryManager.CloseAsync();
}
}


public class DeviceRegistration : IDisposable {
private IOTHUBDeviceService iotHubService;

public DeviceRegistration() {
iotHubService = new IOTHUbDeviceService("xxxx")
}

public void AddDevice(Device device){
iotHubService.AddDeviceAsync(device);
}

public void Dispose(){
iotHubService.Dispose();
}
}

2 - “using”语句方法:

public class IOTHubDeviceService {
private string _iotHubConnectionString;

public IOTHubFacade(string iotHubConnectionString)
{
_iotHubConnectionString = iotHubConnectionString;
}

public async Task<Device> AddDeviceAsync(Device device)
{
using(var registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString))
{
return await registryManager.AddDeviceAsync(device);
}
}
}


public class DeviceRegistration {
private IOTHUBDeviceService iotHubService;

public DeviceRegistration() {
iotHubService = new IOTHUbDeviceService("xxxx")
}

public void AddDevice(Device device){
iotHubService.AddDeviceAsync(device);
}
}

我想知道两种方法中哪一种更好。谢谢!

最佳答案

.Net Framework 提供 System.IDisposable应该实现该接口(interface),以便为开发人员提供一种手动方式,以便在不需要非托管资源时立即释放它们。垃圾收集器 (GC) 不能自动释放非托管资源,它旨在管理托管资源,例如使用 C# 运算符 new 分配的内存。

用于处置对象的模式,称为 dispose pattern ,对对象的生命周期施加顺序。

为了更好地理解,您可以引用以下主题:

我认为保留RegistryManager实例不是一个好的做法,因为它使用非托管资源,例如网络连接。也许下面的代码更好。

    public class IOTHubDeviceService : IDispose {
private RegistryManager _registryManager;

public IOTHubFacade(string iotHubConnectionString)
{
_registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);
}

public async Task<Device> AddDeviceAsync(Device device)
{
return await _registryManager.AddDeviceAsync(device);
}

public void Dispose()
{
if(_registryManager != null){
_registryManager.CloseAsync();
_registryManager.Dispose();
_registryManager = null;
}
}
}


public class DeviceRegistration : IDisposable {
private IOTHUBDeviceService iotHubService;

public DeviceRegistration() {
iotHubService = new IOTHUbDeviceService("xxxx")
}

public void AddDevice(Device device){
iotHubService.AddDeviceAsync(device);
}

public void Dispose(){
if(iotHubService != null){
iotHubService.Dispose();
iotHubService = null;
}
}
}

关于azure - 保留RegistryManager实例是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com