✅ Best approach for returning typed objects
Hello,
I am porting a popular UA parser library called DeviceDetector to .NET.
The library determines the client from the user agent, which can be a browser, media player, feed reader, etc.
With the current implementation, there is an interface that all client models implement, and the returned object after parsing is of type
IClientInfo
. This means that users would need to perform type checking and casting to determine the exact client type. As I see it, there are two possibilities, each providing the user with a different way of handling the concrete client.
Which of these approaches would be the best to follow?
1st approach
2nd approach - it's similar to the 1st approach, but it is extended with an enum that represents the concrete type
3 Replies
if i interpret your question correctly, you are determining the type during runtime. if thats the case there is no way.
think a bit further, how would the implementer/compiler know which type to use?
so they have to do the typechecking and cast appropriatly
Thanks, what about the design? Should I include an enum that represents the client type?
sounds resonable, maybe even include the version in the enum in case of multiple types that differ in api during versioning