Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Helper Pattern vs Service Method

Let's 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's the best place to add static helper methods (that require no state like the service class might) related to the ticket entity? (Keeping in mind this is one entity/service amongst many other entities/services in the entire system).

Right now I'm thinking of the following possibilities:

  1. Create a new class called TicketHelper in its own .cs file.
  2. Create a static Helper class directly on the Ticket class
  3. Create a static Helper class directly on the Service class
  4. Just add the methods directly onto the service class as any other service method
  5. ... a better solution than these?

For the sake of clarity, here's what 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
    }
  }
}

The API might look as follows in this case:

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);
like image 645
Adam Levitt Avatar asked Dec 23 '12 13:12

Adam Levitt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You could 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();

Also, using interfaces like IEnumerable<T>, IList<T> or IDictionary<K, V> is preferable than the concrete collection classes.

like image 170
Jordão Avatar answered Sep 27 '22 20:09

Jordão


Yet another possibility is to use the singleton pattern.

It has the advantages of:

  • being able to implement an interface
  • providing an object that you could pass to a method or constructor in a dependency injection scenario
  • providing a static behavior
public interface ITicketHelper
{
    void HelpThis(Ticket t);
    IList<Ticket> HelpThat();
}

public class TicketHelper : ITicketHelper
{
    public static readonly TicketHelper Instance = new TicketHelper();

    private TicketHelper()
    { ... }

    public void HelpThis(Ticket t)
    { ... }

    public IList<Ticket> HelpThat()
    { ... }
}

Usage

 var result = TicketHelper.Instance.HelpThat();
like image 43
Olivier Jacot-Descombes Avatar answered Sep 27 '22 21:09

Olivier Jacot-Descombes