保留 RegistryManager 实例是一种好习惯吗?
Is a good practice to keep RegistryManager instance?
我想知道保留一个 RegistryManager 实例而不是在每个方法中创建一个新实例是否是一个好习惯。
只是为了给出更多的解释,它会有所不同,因为如果我保留一个实例而不是在每个方法中创建一个实例,我必须向我的树的所有级别公开一个 Dispose 方法。
为了解决我的问题,下面的代码展示了两种方法:
1 - Dispose 模式方法 (我想避免):
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 接口,以便为开发人员提供一种手动方式来在不需要时尽快释放非托管资源。垃圾收集器(GC) 不能自动释放非托管资源,它旨在管理托管资源,例如使用C# 运算符分配的内存new
。
用于处置对象的模式称为 dispose pattern,对对象的生命周期施加顺序。
为了更好地理解,您可以参考以下主题:
- When the Dispose method is called
- GC.Collect, Dispose and Destructor
我认为保留 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;
}
}
}
我想知道保留一个 RegistryManager 实例而不是在每个方法中创建一个新实例是否是一个好习惯。
只是为了给出更多的解释,它会有所不同,因为如果我保留一个实例而不是在每个方法中创建一个实例,我必须向我的树的所有级别公开一个 Dispose 方法。
为了解决我的问题,下面的代码展示了两种方法:
1 - Dispose 模式方法 (我想避免):
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 接口,以便为开发人员提供一种手动方式来在不需要时尽快释放非托管资源。垃圾收集器(GC) 不能自动释放非托管资源,它旨在管理托管资源,例如使用C# 运算符分配的内存new
。
用于处置对象的模式称为 dispose pattern,对对象的生命周期施加顺序。
为了更好地理解,您可以参考以下主题:
- When the Dispose method is called
- GC.Collect, Dispose and Destructor
我认为保留 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;
}
}
}