jeudi 14 juillet 2016

Clean way to handle method that can return two different types

So I have some code that looks like this:

                Stream result;
                if (type.Equals("one", StringComparison.OrdinalIgnoreCase))
                {
                    result = GetOneType();
                }
                else if (type.Equals("two", StringComparison.OrdinalIgnoreCase))
                {
                    result = GetTwoType();
                }
                else
                {
                    result = GetOtherType();
                }
                response = Request.CreateResponse(HttpStatusCode.OK);

Now, we're adding a query string that is associated with type 'two'. Pattern wise, we are not allowed to work with this query string within this section of the code, so I need to have another method that checks it.

For instance, it can now be type 'two' with or without this new query string, and the return type is different based on whether or not the query string is present because it takes a completely different code path.

What is the cleanest way to deal with this design wise? I was thinking I would create a wrapper class that returns an object but that requires boxing/unboxing and these return values can be quite large (media files). Also, the Request.CreateResponse would then have to have another check in there to determine the response headers (because they would also be different).

Is there a cleaner way to do this?

Aucun commentaire:

Enregistrer un commentaire