✅ Avoid casting with inheritance chain
Hello,
I'm facing a problem right now that I don't know how to handle without casting
Imagine having a simple chat system with users (premium and classic)
You want to display a dashboard score but only scoring users must be displayed in since there are no score for regular users.
This is what I'm thinking of so far :
Can I handle this without casting?
Maybe we could ask the user it's scoring, but I do not want to have any trace of scoring in regular users since they are not eligible for this feature.
What about if we want to get all scoring users?
Are there some edge cases where casting is just required?
6 Replies
I personally would not do this with separate types, it tends to be an antipattern
have a Type or IsPremium prop on the user
so you suggest having the Score prop for all users instead and check their types/flag instead?
yes
I kind of agree, but I'm not comfortable about having a useless property for most of users. Basically, the property will be available in the contract for the user so it can be used elsewhere without a check on user type
PremiumInfo
class that holds all information for a premium user. add as property to User class/interface. null for normal user, instance for premium user. boom composition (class naming TBD)Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View