C # Helper Pattern vs Service Method

Say we have the following classes:

public class Ticket { public int Id { get; set; } public string Event { get; set; } ... } public class TicketService { public void acceptTicket(Ticket ticket) { ... } ... } 

My question is: where is the best way to add static helper methods (which do not require a state like the service class) associated with the ticket object? (Keeping this in mind, this is one object / service among many other objects / services throughout the system).

Now I'm thinking about the following options:

  • Create a new TicketHelper class in your own .cs file.
  • Create a static helper class directly in the Ticket class
  • Create a static helper class directly in the service class
  • Just add methods directly to the service class like any other service method
  • ... a better solution than these?

For clarity, here I mean for # 2 or # 3.

 public class TicketService { public void acceptTicket(Ticket ticket) { ... } ... public static class Helper { public static Dictionary<string, List<Ticket>> groupByName(List<Ticket> tickets) { // returns the map of name/tickets } } } 

In this case, the API may look like this:

 Ticket ticket = new Ticket(); List<Ticket> tickets = new List<Tickets>(); TicketService service = new TicketService(); service.acceptTicket(ticket); // leaving out the creation of a list of tickets... var groups = TicketService.Helper.groupByName(tickets); 
+4
source share
3 answers

You can use extension methods for this. Create them in a separate TicketExtensions class.

 public static class TicketExtensions { public static Dictionary<string, List<Ticket>> groupByName(this List<Ticket> tickets) { // returns the map of name/tickets } } ... var ticket = new Ticket(); var tickets = new List<Tickets>(); var service = new TicketService(); service.acceptTicket(ticket); // leaving out the creation of a list of tickets... var groups = tickets.groupByName(); 

In addition, the use of interfaces such as IEnumerable<T> , IList<T> or IDictionary<K, V> is preferable to specific collection classes.

+2
source

I believe the answer depends on who you expect to call the method.

Personally, I would put a static method in the TicketService class, since it works on several objects and makes it private to the service itself.

Then I will create an instance method in the service to expose the functionality to all users of the service. This makes the API cleaner and allows people who need to know implementation details whether something is a static method or not.

+1
source

Another possibility is to use a singleton pattern.

It has the following advantages:

  • ability to implement an interface
  • providing an object that you could pass to a method or constructor in a dependency injection script
  • static behavior
 public interface ITicketHelper { void HelpThis(Ticket t); IList<Ticket> HelpThat(); } public class TicketHelper : ITicketHelper { public static readonly ITicketHelper Instance = new TicketHelper(); private TicketHelper() { ... } public void HelpThis(Ticket t) { ... } public IList<Ticket> HelpThat() { ... } } 

Using

  var result = TicketHelper.Instance.HelpThat(); 
+1
source

All Articles