I would like to know if it's a good practice to keep a RegistryManager instance instead of create a new instance inside each method.
Just to give more explanation, it makes diference because if I keep an instance instead of create an instance inside each method I have to expose a Dispose method to all levels of my tree.
Just to clear my problem the code below shows off the two approachs:
1 - Dispose pattern approach (I want to avoid):
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" statement approach:
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);
}
}
I'd like to know which of two approachs is better. Thanks!