1

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!

Henrique
  • 602
  • 4
  • 18

1 Answers1

0

.Net Framework provides the System.IDisposable interface that should be implemented to provide the developer a manual way to release unmanaged resources as soon as they are not needed. Garbage Collector(GC) can not release the unmanaged resources automatically,it is designed to manage managed resources such as memory allocated using the C# operator new.

The pattern for disposing an object, referred to as a dispose pattern, imposes order on the lifetime of an object.

For better understanding, you can refer to following topics:

I don't think it is a good practice to keep RegistryManager instance as it uses unmanaged resources such as Network connections. Maybe the following code is better.

    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;
            }
        }
    }
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Thanks for your answer. Maybe I'm not clear on my problem, sorry, but this Dispose pattern is what i want to avoid. I would like to know what is the better approach for instances of RegistryManager. Instantiate it in each method or use one single instance in the class. – Henrique Mar 13 '18 at 10:42
  • The `using` statement ensures that `Dispose` is called even if an exception occurs while you are calling methods on the object.So i think the `using` statement approach is better. – Michael Xu Mar 15 '18 at 05:23