Looking for maybe some design advice here. I've run into this issue a couple of times now and feel like my approaches have been sub par up until now.
I've got for example a method that I want to create called GetConversation. Now GetConversation will return the same data whether you give it an id or an alternate address parameter. Just a matter of preference or whatever data you have available that makes it a little more flexible and easy to use. This is great but the problem I'm running into is that these are both string parameters. Ideally it would look something like this,
public object GetConversation(string id)
{
// Get Conversation by id
}
public object GetConversation(string address)
{
// Get Conversation by address
}
Now of course this is going to fail because of ambiguity. The compiler doesn't know which function to access when the user drills into GetConversation.
This brings me to my second approach at a flexible solution,
public object GetConversation(string id, string address)
{
// Get Conversation by id or address
if (id != null)
{
// Get by id
}
else if (address != null)
{
// Get by address
}
return null;
}
But now I am starting to make a preference of always searching by id first. Isn't necessarily a bad thing but I feel like this just isn't the most cohesive solution and I have to explicitly set a return outside of my checks for failures on both checks.
Any thoughts on a better approach?
GetConversationById
andGetConversationByAddress
.