I am trying to return a collection of one or multiple Notifications back to the front end for display. I've kicked the wall for a while, and have come up with two different yet similar solutions, posted below.
I'm curious if A: one of these is better than the other, or B: there is better more awesome solution out there and I'm too close to the problem to see it.
The Switch/Case Statement:
var dl = new Notification() { Type = "dl" };
var m = new Notification() { Type = "m" };
var t = new Notification() { Type = "t" };
var bb = new Notification() { Type = "bb" };
switch (type)
{
case "dl":
dl.Count = _dlRepository.GetNotificationCount(userId);
return Enumerable.Repeat(dl, 1);
case "m":
m.Count = _mRepository.GetNotificationCount(userId);
return Enumerable.Repeat(m, 1);
case "t":
t.Count = _tRepository.GetNotificationCount(userId);
return Enumerable.Repeat(t, 1);
case "bb":
bb.Count = _bbRepository.GetNotificationCount(userId);
return Enumerable.Repeat(bb, 1);
default:
dl.Count = _dlRepository.GetNotificationCount(userId);
m.Count = _mRepository.GetNotificationCount(userId);
t.Count = _tRepository.GetNotificationCount(userId);
bb.Count = _bbRepository.GetNotificationCount(userId);
var notifications = new List<Notification>();
notifications.Add(dl);
notifications.Add(m);
notifications.Add(t);
notifications.Add(bb);
return notifications;
}
The Ifs:
var notifications = new List<Notification>();
if (type == "dl" || type == null)
{
notifications.Add(new Notification()
{
Type = "dl",
Count = _dlRepository.GetNotificationCount(userId)
});
}
if (type == "m" || type == null)
{
notifications.Add(new Notification()
{
Type = "m",
Count = _mRepository.GetNotificationCount(userId)
});
}
if (type == "t" || type == null)
{
notifications.Add(new Notification()
{
Type = "t",
Count = _tRepository.GetNotificationCount(userId)
});
}
if (type == "bb" || type == null)
{
notifications.Add(new Notification()
{
Type = "bb",
Count = _bbRepository.GetNotificationCount(userId)
});
}
return notifications;
Any thoughts/opinions are appreciated.
Aucun commentaire:
Enregistrer un commentaire