0
votes

This is probably a newb question so i'll come right out and say that. This is my first time creating an IDisposable class, I want to make sure that I created my class correctly, called it correctly, and Dispose of it correctly. Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crawler
{
    class LoggingClass
    {
        public class LoggingDisposeable : IDisposable
        {
            public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
            {
                string Loggingall = " insert into tblLogs " +
                                "set SystemMsg='" + systemMsg.Replace("'", "''") + "'" +
                                ",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
                                ",SystemSection = '" + SystemSection.Replace("'", "''") +
                                ",ID = '" + CarID.Replace("'", "''") + "'" +
                                ",FixID = '" + FixID.Replace("'", "''") + "'" +
                                ",baseurl = '" + baseURL.Replace("'", "''") + "'" +
                                ",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") +
                                ",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
                                ",Site = 'AutoTrader'" +
                                ",TimeStamp = Now()";
                using (var MYSQLP = new MySQLProcessing.MySQLProcessor())
                {
                    MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");
                }
            }

            public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }

            protected virtual void Dispose(bool disposing)
            {
                if (!this.Disposed)
                {
                    if (disposing)
                    {
                        // Perform managed cleanup here.

                    }

                    // Perform unmanaged cleanup here.

                    this.Disposed = true;
                }
            }

            protected bool Disposed { get; private set; }

        }
    }
}

And this is how I call it.

 var options = new ParallelOptions();
options.MaxDegreeOfParallelism = 5;
                Parallel.ForEach(urlTable.AsEnumerable(),options, drow =>
            {

 using (var logClass = new LoggingClass.LoggingDisposeable())
                                {
                                    logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode",rDealerLink, "", "");
}
}</pre>
1
You don't need to implement IDisposable on this class...you aren't disposing of anything. - Eric Dahlvang
@EricDahlvang This exists inside of a Parallel foreach loop and I call this class, please see edit - user222427
Why are you partially implementing the finalize pattern that Microsoft only recommends when handling unmanaged resources? You should either implement it correctly or not at all. - ChaosPandion
@ChaosPandion The main reason is because I honestly am not positive on how to construct/use IDisposable - user222427
using () { } blocks internally call Dispose() when code exists the block, so your resource is already being disposed in this method as you've posted it and you don't need to implement IDisposable separately. - Jim Schubert

1 Answers

2
votes

I think what you intend to do is something like this:

public class LoggingDisposeable : IDisposable
{
    MySQLProcessing MySQLP;

    public LoggingDisposeable()
    {
        MYSQLP = new MySQLProcessing.MySQLProcessor();
    }

    public void GenericLogging(string systemMsg, string SystemClass, string SystemSection, string ID, string FixID, string baseURL, string mysqlQueryName, string mysqlQuery)
    {
        string Loggingall = " insert into tblLogs " +
                        "set SystemMsg='" + systemMsg.Replace("'", "''") + "'" +
                        ",SystemClass = '" + SystemClass.Replace("'", "''") + "'" +
                        ",SystemSection = '" + SystemSection.Replace("'", "''") +
                        ",ID = '" + CarID.Replace("'", "''") + "'" +
                        ",FixID = '" + FixID.Replace("'", "''") + "'" +
                        ",baseurl = '" + baseURL.Replace("'", "''") + "'" +
                        ",mysqlqueryName = '" + mysqlQuery.Replace("'", "''") +
                        ",mysqlquery = '" + mysqlQuery.Replace("'", "''") + "'" +
                        ",Site = 'AutoTrader'" +
                        ",TimeStamp = Now()";

            MYSQLP.MySQLInsertUpdate(Loggingall, "Loggingall");                
    }

    public void Dispose()
    {
        MYSQLP.Dispose();
    }
}

Then use it like this:

using (var logClass = new LoggingDisposeable())
{
    var options = new ParallelOptions();
    options.MaxDegreeOfParallelism = 5;
    Parallel.ForEach(urlTable.AsEnumerable(), options, drow =>
        {
            logClass.GenericLogging("ZipCode not available for this dealerL", "DealershipRequest", "checkExistingDealers", dealerID, "DealerShipZipCode", rDealerLink, "", "");
        });
}