haydnD
haydnD

Reputation: 2293

What is the best way to build a string over multiple classes

I have a situation where I'm needing to build a message throughout the duration of a process. In this process there are several functions and classes where this message has to collect information. What is the best way to build this message?

Example:

public class process{
   public StringBuilder message = new StringBuilder();

   private void DoStep1
   {
      AddNote("start");

      var p2 = new process2();
      p2.DoStuff();

      var p3 = new process3();
      p3.DoStuff();

      SendEmailMethod(message);
   }

   private void AddNote(string msg)
   {
      //do stuff
      message.Append(msg);
   }
}

public class process2{
   public void DoStuff()
   {
      //need to append msg to that variable
   }

}

public class process3{

   public void DoStuff()
   {
      //need to append msg to that variable
   }

}

Upvotes: 0

Views: 245

Answers (4)

James World
James World

Reputation: 29776

What you are describing is basically a workflow. You can typically set this up using a chain of responsibility implementation, or more generally a pipeline. Have each element of the pipeline implement the same interface, which includes the ability to set the next element in the pipeline and call the next element in the pipeline.

Have a think about whether you can set up the pipeline once and continually re-use it. This is typically what you would do with this kind of approach.

.NET offers Windows Workflow Foundation (which may be overkill unless your scenario is complex), Dataflow in the Task Parallel Library is also good for pipelines - but also simple objects may do the trick, don't over-engineer it!

You are looking to pass some kind of context (like the initializes StringBuilding others have mentioned - or maybe something richer as you require) down the pipeline, building it up as you go.

Upvotes: 0

Szymon
Szymon

Reputation: 43023

You can pass an initialised StringBuilder to all those methods in other classes.

public class process{
   public StringBuilder message = new StringBuilder();

   private void DoStep1
   {
      AddNote("start");

      var p2 = new process2();
      p2.DoStuff(message);

      var p3 = new process3();
      p3.DoStuff(message);

      SendEmailMethod(message);
   }

   private void AddNote(string msg)
   {
      //do stuff
      message.Append(msg);
   }
}

public class process2{
   public void DoStuff(StringBuilder stringBuilder)
   {
      //need to append msg to that variable - use stringBuilder
   }

}

public class process3{

   public void DoStuff(StringBuilder stringBuilder)
   {
      //need to append msg to that variable - use stringBuilder
   }

}

You should document those classes that they expect an initialised StringBuilder or maybe throw an exception if it's null.

public class process3{

   public void DoStuff(StringBuilder stringBuilder)
   {
      if (stringBuilder == null)
          throw new ArgumentException("stringBuilder must be initialised");

      //need to append msg to that variable - use stringBuilder
   }

}

Upvotes: 6

Algamest
Algamest

Reputation: 1529

You could have as static StringBuilder in a static method. Say:

public static StringBuilder builder;
static void AddToString(string s)
{
    if(builder == null)
        builder = new StringBuilder(s);

    builder.Append(s);  // new line?: s + "\r\n"
}

This could be in a static class if you have one or just a static variable in the class.

Upvotes: 0

TheJediCowboy
TheJediCowboy

Reputation: 9222

I would handle this with some form of a builder pattern, so that you are at least passing around a common reference and not just simply taking in a StringBuilder. Obviously this is not required, but it would definietely keep your code cleaner and allow it to grow and shrink if needed.

Check out Builder Pattern

Upvotes: 0

Related Questions