C# Inheritence: Choosing what repository based on type of inherited class
Posted
by Oskar Kjellin
on Stack Overflow
See other posts from Stack Overflow
or by Oskar Kjellin
Published on 2010-04-24T15:51:24Z
Indexed on
2010/04/24
15:53 UTC
Read the original article
Hit count: 367
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.
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.
© Stack Overflow or respective owner