I'm using the singleton design pattern for my Business Layer and Data Access Layer classes in C#.
My Class looks as below:
using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
public class CabBookingDao: IDisposable
{
private static volatile CabBookingDao _instance;
private static readonly object SyncRoot = new Object();
private readonly DatabaseProviderFactory _factory;
private readonly Database _db;
}
public static CabBookingDao Instance
{
get
{
if (_instance == null)
{
lock (SyncRoot)
{
if (_instance == null)
_instance = new CabBookingDao();
}
}
return _instance;
}
}
private CabBookingDao()
{
_factory = new DatabaseProviderFactory();
_db = _factory.Create("MyDbContext");
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
Here, I'm trying to Implement IDisposable for this class and I'm getting Warning from VS Code Analyzer like:
CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'CabBookingDao' or mark the type as sealed. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources CabBookingDao.cs
By this warning, I understood that I suppose to create a method as below:
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
}
// free native resources if there are any.
}
But, I'm confused about what objects do I need to dispose here? If I dispose _instance, will it affect the Singleton behavior?