Why delegate?

Sep 21, 2011 at 4:46 AM

Can you explain why do you need to use delegate in this particular instance?

[TraceFileProcessor.cs]

               UnitTestGenerator utg = new UnitTestGenerator(scenarioName, scenarioName, scenarioName + "Tests", TestMethodMode.ScenarioMethodOnly, OperationTimerMode.IncludeOperationTimers);

                TraceParser parser = new TraceParser(reader);
                parser.ProcessingTraceRecord += delegate(object sender, TraceRecordFilterEventArgs trfea)
                {
                    if (ProcessingTraceRecord != null)
                    {
                        ProcessingTraceRecord(null, trfea);
                    }
                };

                utg.GeneratingCall += delegate(object sender, GenerateCallEventArgs gcea)
                {
                    if (codeGen != null)
                    {
                        Console.WriteLine("   UnitTestGenerator.GenerateCall(2.0)");
                        gcea.Handled = codeGen.GenerateCall(gcea.Command, gcea.ScenarioMethod, gcea.StubType);
                    }
                };

                SqlParser sqlParser = new SqlParser();
                while (true)
                {
                    TraceRecord traceRecord = parser.Parse();
                    if (traceRecord == null)
                    {
                        break;
                    }

                    string lastSqlParsed = traceRecord.TextData;
                    if (ParsingSql != null)
                    {
                        ParsingSql(null, new SqlEventArgs(lastSqlParsed));
                    }

                    ParsedSqlCommand cmd = sqlParser.ParseTraceRecord(traceRecord);
                    try
                    {
                        utg.GenerateCall(cmd);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.Processor_ErrorProcessingParsedCommand, lastSqlParsed, ex.Message), ex);
                    }
                }

Coordinator
Sep 21, 2011 at 7:09 AM

That is the syntax for an anonymous delegate, I probably thought that it wasn't worth writing a whole new method for those brief bits of code. At the time I was not very familiar with lambdas, and indeed they may not even have been available when I wrote that code (can't remember now).

Regards

Rob

Oct 5, 2011 at 6:20 PM

Yeah, I am having a problem understanding which piece of code were executed for the anonymous delegate, thus making the project upgrade rather slow...

If I decided to release upgrade, would you review it? Maybe I broke something

Coordinator
Oct 6, 2011 at 8:10 AM

I am afraid it would be difficult for me to do a review. Sorry I can't help on this.

Regards

Rob

Oct 8, 2011 at 9:52 PM

I just noticed a suite of tests, will go through them.