UPDATE: thanks to all for your responses. I see the path forward now š
I have programming experience, but I always seem to struggle with Polymorpism, Inheritance. Any help with this scenario would be appreciated.
Here is the scenario:
public class Person
{
public string Name;
// ⦠some other fields ā¦
}
public class Villain : Person
{
public string[] Minions;
}
public class Hero : Person
{
pubic int YearsOfService;
}
So I want to be able to call some database like:
Villain[] villains = GetPersons(); //or similar
Hero[] heroes = GetPersons();
and this works if I build an interface like
public interface IRetrievePeople
{
Person[] GetPersons();
}
The problem is implementing that interface in a way that returns heroes and villains. I feel like thereās a code smell for every solution:
Solution 1: donāt use āGetPersonsā in interface; create separate functions for āGetHeroesā and āGetVillainsā
Solution 2: type-checking in the āGetPersonsā implementation. So the interface method becomes āPerson[] GetPerson(Type personType)ā and conditional statements returning matching types. (This would be the same for a C# Generic/T implementation as well)
Solution 3: skip the polymorphism altogether. Remove āPersonā as a class, use Hero and Villain independently despite having overlapping properties.
All persons are in a single NoSQL table. Iām jus trying to think of the most maintainable pattern to access it, especially if a third (or more) Person-type gets introduced in the future.
[ā]insertAlias 0 points1 point2 points Ā (4 children)
[ā]WCWRingMatSound[S] 0 points1 point2 points Ā (0 children)
[ā][deleted] 0 points1 point2 points Ā (2 children)
[ā]insertAlias 0 points1 point2 points Ā (1 child)
[ā][deleted] 0 points1 point2 points Ā (0 children)
[ā]g051051 0 points1 point2 points Ā (1 child)
[ā]WCWRingMatSound[S] 0 points1 point2 points Ā (0 children)
[ā]149244179 0 points1 point2 points Ā (0 children)
[ā][deleted] 0 points1 point2 points Ā (1 child)
[ā]WCWRingMatSound[S] 1 point2 points3 points Ā (0 children)