Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SearchMulti() #145

Open
LordMike opened this issue Aug 16, 2015 · 8 comments
Open

Improve SearchMulti() #145

LordMike opened this issue Aug 16, 2015 · 8 comments

Comments

@LordMike
Copy link
Collaborator

This is a tricky one, as Multi-search will return one of three objects (movie, tvshow or person), depending on the result. We might be able to reduce the SearchMulti type to only contain the common fields, and then have a property for each of the three types (where one of them will be set with the actual content).

Inspired by PR #142

@LordMike
Copy link
Collaborator Author

Depends on issue #144 to be completed and merged first.

@danielnachtrub
Copy link

using proper inheritance on the returned items would propably be nicer
then SearchMulti can return an interface which may be nicer that an actual class
working with Movies and TvShows when the items are implementing interfaces (e.g. ISearchMovie implements ISearchResult).
Propagating SearchMulti has no direct benefits and causes incompatibility when using SearchMovie or SearchMulti in exchange to each other

=> using SearchMulti should be the same as SearchMovie with a mixed result set

@LordMike
Copy link
Collaborator Author

LordMike commented Aug 1, 2016

@danielnachtrub check out the new SearchMulti

@Filipowicz251
Copy link

What is the status of this 4 years old ticket?:) I've checked and currently i have only Id, MediaType, Popularity properties on Results. That is dissapointing, and forces me to use two requests instead of one (one: SearchMovies and one SearchTvShow request)

@LordMike
Copy link
Collaborator Author

There's been no update. Do you have a specific need?

As it is now, the SearchMulti's SearchBase type is a base class. You can cast it to one of three different types (https://github.com/LordMike/TMDbLib/blob/51df5d72d948764b2beae4df2f43b5880123faac/TMDbLib/Utilities/Converters/SearchBaseConverter.cs#L31-L44).

@Filipowicz251
Copy link

Filipowicz251 commented Nov 14, 2020

@LordMike - yeah, but have can i do that?

TMDbClient client = new TMDbClient("12345");

var results = await client.SearchMultiAsync("star wars");

var movie = (TMDbLib.Objects.Movies.Movie)results.Results.FirstOrDefault();

Will clearly not work :)

@Filipowicz251
Copy link

Filipowicz251 commented Nov 14, 2020

Ok, i've found some method to do it :) A little bit hacky but it works;)

class BaseConverter
{
protected T Convert<T, X>(X result)
{
var derivedClassInstance = Activator.CreateInstance();
var derivedType = derivedClassInstance.GetType();

    var properties = result.GetType().GetProperties();
    foreach (var property in properties)
    {
        var propToSet = derivedType.GetProperty(property.Name);
        if (propToSet.SetMethod != null)
        {
            propToSet.SetValue(derivedClassInstance, property.GetValue(result));
        }
    }
    return derivedClassInstance;
}

protected List<T> Convert<T, X>(List<X> listResult)
{
    var derivedList = new List<T>();
    foreach (var r in listResult)
    {
        //can cope with this - since there will not ever be many iterations
        derivedList.Add(Convert<T, X>(r));
    }
    return derivedList;
}

}

@LordMike
Copy link
Collaborator Author

LordMike commented Nov 15, 2020

Well, you can't cast it to a Movie as that's not the type. It would be one of SearchMovie, SearchTv, SearchPerson

So, f.ex.:

TMDbClient client = new TMDbClient("12345");

var results = await client.SearchMultiAsync("star wars");

var movies = results.Results.OfType<SearchMovie>().ToList();
var tvs = results.Results.OfType<SearchTv>().ToList();
var persons= results.Results.OfType<SearchPerson>().ToList();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants