1

I admit I'm a newbie for C# and I have the following question.

Assume you have a solution with three projects (A, B, C). Project C references B, and both B and C reference A. B is where the entry point is. I have a list in one of the classes within project A. It goes like this:

Project A:

public class ProcessDuration
    {
        public List<TicketTimer> DurationList = new List<TicketTimer>();

        public void AddProcesstime(string Ticket, int Count, Process_Step Step, Process_Status Status, DateTime Time)
        {
            Ticket ccStart = new Ticket();
            ccStart.RecordTime(Ticket, Count, Step, Status, Time);
            DurationList.Add(ccStart);
        }
...
}

Project B:

class testB
{
          public TicketProcessDuration TD = new TicketProcessDuration();
          TD.StartProcessMonitor(ticketMain, request.DataRecords.Count);
}

Project C:

class testC
{
        TD.StartProcessMonitor(ticketMain, request.DataRecords.Count); //Error here because TD scope
}

I want to add to the list from projects B and C. So I create an object of class ProjectDuration within B. But I can't access it from project C because of the scope. I played around with making the list static and creating the object TD in project C but it gave me another problem - it maintains the list for app lifetime but i want the list to be emptied after each run. I dont want to define a function and pass the ProjectDuration object to C from B. Also know that it is a web service application so several connections may be iterating the list so I just cant clear it after one run.

What do you advice? How do i solve this problem? I also want to know how can I destroy the object TD before the garbage collector?

Any urgent help would be appreciated. Let me know if I need to read some areas to understand these situation better... but solution first!

Kind regards,

svick
  • 236,525
  • 50
  • 385
  • 514
  • If `B` is the "entry point" (whatever that may mean - not enough context), and nothing seems to reference `C`, how is it even involved? It's also not clear (to me) what "run"s are. – Damien_The_Unbeliever May 05 '12 at 15:47

1 Answers1

0

Does B create C? If so, have B supply the instance of ProcessDuration to the constructor of C when you create it. Even better would be to provide ProcessDuration to B as well via it's constructor so that each class can be tested with a mock or fake ProcessDuration.

  public class TestB
  {
      private TestC C { get; set; }
      private ProcessDuration TD { get; set; }

      public TestB(ProcessDuration td)
      {
           this.TD = td;
           this.C = new TestC(td);
      }

      public void DoSomething()
      {
           this.TD.StartProcessMonitor( ... );
           this.C.DoSomethingEsle();
      }
  }

  public class TestC
  {
      private ProcessDuration TD { get; set; }

      public TestC(ProcessDuration td)
      {
          this.TD = td;
      }

      public void DoSomethingElse();
      {
          this.TD.StartProcessMonitor(...);  // or just continue using it???
      }
 }

This is called constructor injection and it's arguably the best way to pass dependencies between related classes. Eventually, you can start using a dependency injection framework to manage these for you, but just starting out I would do it manually until you understand what it's doing, then move on to using a framework to do it for you.

Of course, this may not be your exact scenario. Perhaps both B and C are created by something else. In that case, you can just have that class inject the same ProcessDuration instance into both B and C (rather than have B create C and pass along the instance).

Since this is running in a web service, you'll need to make sure that all of the instances are thread-safe, i.e., no data in the classes is shared between multiple threads or, if it is, you've built in some locking mechanism to make sure that only one thread at a time can access the data. You'll want to avoid using static instances to help ensure thread safety if you're not sure what you're doing. It might help to do some research into both dependency injection and threading before you go much further.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795