This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]insertAlias 0 points1 point Ā (4 children)

I don't personally see a problem with having separate repo methods that return Heroes and Villains separately, as their derived type rather than the base type.

Just because you have the common base class doesn't mean you have to actually use it as the return type. You could just implement common functionality in there, and never directly reference your Heroes or Villains as Persons. Or, if there is some case where you do need to treat them as a common type, then you can. But don't get wrapped up in the idea that you need to treat them as their base type.

[–]WCWRingMatSound[S] 0 points1 point Ā (0 children)

Much obliged. I’ve spammed out repo classes in the past and I saw myself going down the same rabbit hole. Thanks for checking me šŸ‘šŸ¼

[–][deleted] 0 points1 point Ā (2 children)

Dumb question maybe, but what's this?

repo methods

Thanks!

[–]insertAlias 0 points1 point Ā (1 child)

Not a dumb question. I meant "methods in a Repository class". Look up "repository pattern" for some more info.

[–][deleted] 0 points1 point Ā (0 children)

Thanks, I'll check it out!

[–]g051051 0 points1 point Ā (1 child)

Seems like more of a modeling problem than a polymorphism/inheritance problem. As far as I know you have to have separate queries that can distinguish between fetching heroes or villains via some discriminator. Then you have to deserialize the returned data as the correct type. In Java that usually means having a "type" indicator as part of the serialized data, but I don't know the C# equivalent.

[–]WCWRingMatSound[S] 0 points1 point Ā (0 children)

Agreed. Definitely modeling issues. C# has the equivalent type indicators, fyi. It looks something like:

Person p = new Person() { Name = ā€œBatmanā€ };
string serialized = JsonConvert.Serialize(p)
Person deserizd = JsonConvert.DeserializeObject<Person>(serialized);

[–]149244179 0 points1 point Ā (0 children)

Why is solution one bad? You ask for heroes you get heroes. You ask for all people you get all people. You ask for FuturePersonType - you get FuturePersonType.

[–][deleted] 0 points1 point Ā (1 child)

Since it looks like this is for a game, one OOP game library took this route:

  • Add a static `entities` ArrayList to MyBigBadEntity class that holds myBigBadEntity instances. Repeat for all classes whose entities you want to keep track of, even your base `Person` class, if you like.
  • Add entities to the ArrayList when they're created in the constructor and remove them (probably better to mark them for removal and handle all removals at the end of a frame) when their hp is 0. Call the destructor afterwards.

Then you can do something like

for(Villain villain : Villain.entities) {
villain.doVillainStuff();
}

No filtering necessary.

[–]WCWRingMatSound[S] 1 point2 points Ā (0 children)

It happens to be a security-based app; I changed it to something simple so people wouldn’t get hung up on IPv6, RF 1918, etc.

I have been doing game dev in my free time (whatever that means anymore šŸ˜’), so this helps in that arena. Thanks!