.Net 4.0 Zoptymalizowany kod do refaktoryzacji istniejących warunków „if” i „jest”
Mam następujący kod C #. To działa dobrze; aleGetDestination()
metoda jest zaśmiecona wielomaif
warunki za pomocąjest operatorem.
W .Net 4.0 (lub nowszym) jaki jest najlepszy sposób uniknięcia tych warunków „if”?
EDYCJA: Rola jest częścią modelu biznesowego, a miejsce docelowe jest wyłącznie artefaktem jednej konkretnej aplikacji korzystającej z tego modelu biznesowego.
KOD
public class Role { }
public class Manager : Role { }
public class Accountant : Role { }
public class Attender : Role { }
public class Cleaner : Role { }
public class Security : Role { }
class Program
{
static string GetDestination(Role x)
{
string destination = @"\Home";
if (x is Manager)
{
destination = @"\ManagerHomeA";
}
if (x is Accountant)
{
destination = @"\AccountantHomeC";
}
if (x is Cleaner)
{
destination = @"\Cleaner";
}
return destination;
}
static void Main(string[] args)
{
string destination = GetDestination(new Accountant());
Console.WriteLine(destination);
Console.ReadLine();
}
}
REFERENCJE
Słownik <T, Delegat> z Delegatami różnych typów: Czystsze, bez nazw metod?Jon Skeet: Robienie refleksji i eksplorowanie delegatówif-else vs. switch vs. Dictionary delegatówSłownik z delegatem lub przełącznikiem?Wyrażenie i delegowanie w c #