I have been making a program that downloads information about movies from the internet. I have a base class Title
, which represents all titles. Movie
, Serie
and Episode
are inherited from that class.
To save them to the database I have 2 services, MovieService
and SerieService
. They in turn call repositories, but that is not important here.
I have a method Save(Title title)
which I am not very happy with. I check for what type the title is and then call the correct service. I would like to perhaps write like this:
ITitleService service = title.GetService();
title.GetSavedBy(service);
So I have an abstract method on Title
that returns an ITitleSaver
, which will return the correct service for the instance. To do this the services would have to implement ITitleSaver
My question is how should I implement ITitleSaver
? If I make it accept Title
I will have to cast it to the correct type before calling the correct overload. Which will lead to having to deal with casting once again.
What is the best approach to dealing with this? I would like to have the saving logic in the corresponding class.