Reputation: 970
Here is a class that I have created. It currently returns an exception stating that it is in a loop - this is obvious now.
public class dirSearch : IDisposable
{
private bool disposed = false;
public bool searchSuccessful;
public string errStr;
List<string> resList = new List<string>();
public void getEmpDetails(string filStr, string varStr)
{
string strServerDNS = "ldap.<redacted>.com:389";
string strSearchBaseDN = "ou=People,o=<redacted>.com";
string strLDAPPath = "LDAP://" + strServerDNS + "/" + strSearchBaseDN;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + filStr + ")";
//make sure the order of the search is like so:
//UID
//empnum
//full name
searcher.PropertiesToLoad.Add(varStr);
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties[varStr][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
public void getEmpDetails(string uid)
{
string strLDAPServerAndPort = "ldap.<redacted>.com";
string strDNPrefix = "uid=" + uid + ", ";
string strLDAPContainer = "ou=people, o=<redacted>.com";
string strLDAPPath = "LDAP://" + strLDAPServerAndPort + "/" + strDNPrefix + strLDAPContainer;
DirectoryEntry objDirEntry = new DirectoryEntry(strLDAPPath, null, null, AuthenticationTypes.Anonymous);
DirectorySearcher searcher = new DirectorySearcher(objDirEntry);
SearchResultCollection results;
searcher.Filter = "(uid=" + uid + ")";
searcher.PropertiesToLoad.Add("uid");
//need conditions here for searching for more than one value, such as <redacted>Manager etc
try
{
results = searcher.FindAll();
foreach (SearchResult result in results)
{
string temStr = result.Properties["uid"][0].ToString();
resList.Add(temStr);
searchSuccessful = true;
}
}
catch (Exception e)
{
errStr = e.ToString();
searchSuccessful = false;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (errStr != null)
{
Dispose();
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}
As far as I am aware, the only two (technically) disposable objects here are the string and the list. Is this correct? Or, is there more, less, or other items entirely that I would be disposing of? Furthermore, what specifically makes them "Disposable" objects? Is it the fact that they are single objects that I have instantiated?
Upvotes: 0
Views: 185
Reputation: 186668
Usually we implement IDisposable
when we have something to dispose (i.e. unmanaged resources like files, RDBMS connections, other IDisposable
instances etc.). Technically, the implementation could be something like that:
// Now IDisposable is redundant: there're no fields to dispose
public class DirSearch : IDisposable {
// All these three fields don't implement iDisposable thus they can't be disposed
//TODO: change this field into (read-only) property
public bool searchSuccessful;
//TODO: change this field into (read-only) property
public string errStr;
List<string> resList = new List<string>();
// I've omitted some code
...
// Property: you may want to know if the instance has been dispose or not
public Boolean IsDisposed {
get;
protected set; // or even "private"
}
// "protected virtual" since this method should be able to be overridden in child classes
protected virtual Dispose(Boolean disposing) {
if (IsDisposed)
return;
if (disposing) {
//TODO: Dispose unmanaged resources here
// NO Dispose() call here! Beware Stack overflow
}
IsDisposed = true;
}
public Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
}
However, more natural is to remove all the IDisposable
stuff from the current DirSearch
implementation. If you want to use DirSearch
as placeholder, if I understand you right as a base class for searches, you'd rather change DirSearch
into something like BaseSearch
and make it abstract
.
Upvotes: 1