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:
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);
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 ...
%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.
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.
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.
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.
Yet another possibility is to use the singleton pattern.
It has the advantages of:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With