tags:

views:

141

answers:

5

Basically I will have list of Directors and Actors defined in an xml for each Movie type instance.

  1. What type should I use to define the collection? (Built-in from the BCL or custom?)

  2. Should I use strings for Directors and Actors or define specific Director and Actor types?

  3. If yes (for #2), should I have a Person class that both Director and Actor derives from.

My model of this relationship will NOT be as deep as IMDb's.

I just need a way to list movies based on certain actors, directors, etc.

So I don't know how to best achieve this? Just asking for opinions.

+1  A: 

Again, a simple String might do for the first implementation, but you'd probably be better with a type along the lines of:

class Person
{
    public int ID { get; set; }
    public string ForeName { get; set; }
    public string SurName { get; set; }
}

You can then store the ID in your Movie class/table and your Person class is extendible for things like "real name", "also known as", etc. But these would only be implemented as and when you wanted to add them to your application.

The ID would be a unique number generated when you add the person. If you're application uses a database then you can set the Person table to autogenerate this value for you.

A List of these would do, though for actors you'd need to store both the character name and the actor's name.

ChrisF
Thanks Chris. What's the ID gonna contain? I am not sure.
Joan Venge
+1  A: 
  1. I would definitely look at the built-in type List. They have a bunch of useful features built-in and are pretty well tested.
  2. You should define a type for those, as it will make the code a lot clearer and will allow for easy future enhancements.
  3. I see no specific properties of Director or Actor, they are just people. So I would simply have a class Person, that is the type of field Diretor or Actor in a separate Movie Class. This also elegantly solves the problem of people that are actors and directors at the same time.

PS: This looks like a one of those first examples in OO books :-)

David Reis
THanks #3 is good. I asked about List because I was concerned that the list is gonna be short. I am not gonna include every single Actor but main ones. So less than 10 for most movies. So thought this could be an overhead.
Joan Venge
+2  A: 

1. I like exposing ReadOnlyCollection<> along with AddXXX/RemoveXXX methods. You seed the collection with an IList<> that is private; the add/remove methods modify the internal list, which is then reflected in the external, read-only list.

2. Director and Actor are entities, meaning they exist over time. They can be referenced from many places without being in those places, similar to how someone can have a conversation about you without you being in the room. Using multiple instances of a string breaks these analogies by equating distinct instances with the same entity. I can change my name, but am still the same person; that would not be true with string names.

3. Since there isn't a distinction between Director and Actor, you can probably just have Person and then collections named Directors and Actors on your Movie object.

Bryan Watts
Thanks good ideas. But for #1, you mean you modify the ReadOnlyCollection? I haven't used it before, so isn't it immutable?
Joan Venge
A ReadOnlyCollection is immutable, but only wraps an existing collection which instead is mutable: as specified in the original post, you'll have a private List<T> instance that keeps the instances, but then you'll expose a ReadOnlyCollection to the users of your class in order to prevent unsafe changes to the collection and provide some specialized methods to do it (AddXXX, RemoveXXX and so on).
Lck
@Lck Thanks for the clarification. The main idea behind the `AddXXX`/`RemoveXXX` methods is that the containing entity performs operations on its children, rather than exposing a list for an outside consumer to modify. This allows you to give those operations meaningful names from your problem domain and also control data modifications.
Bryan Watts
A: 

I'd recommend going with a type system, for Person and having Actor and Director as sub-types. That way you could have collections on the actor and director that can be used as shortcuts to useful data. Something like:

public class Actor : Person
{
    //Name and whatnot inherits from Person
    public List<Movie> PerformedIn
    {
        get
        {
            return myGlobalMovieList.Where(movie=>movie.Actors.Contains(this.Name));
        }
    }
}

This lets you very easily get a list of all the movies that an actor has been in once you have the actor, so if someone were to say, search for Tom Cruise in your app, you could load the "Tom Cruise" actor object, and if someone were to then click on the "Show All Movies For This Actor" button, you simply reference the TomCruise.PerformedIn property, and there you go!

GWLlosa
That's interesting too. Thanks.
Joan Venge
You also need to consider if a person can be both an actor and a director ? Implementing like this will make that impossible.
markt
Yes it can be both. Like Mel Gibson in Braveheart.
Joan Venge
It does not at all make it impossible. You can do an "actor search" for Mel Gibson, and it returns the Mel Gibson AS an actor. You can do a "director search" for Mel Gibson, and it returns the Mel Gibson AS a director. In both cases, nothing has gone wrong in the slightest.
GWLlosa
Worst case, you have a generic "person" search, and the results for "Mel Gibson" will contain 2 entries: Mel Gibson (actor) and Mel Gibson (director).
GWLlosa
So how do you create an object that is both an Actor and a Director ? In my opinion, this is not a good way to represent your domain. Your model should be able to match closely to the domain it is representing without forcing the other developers to think in an unnatural way to understand your code.
markt
What if you want to add writer, producer later as well - now worst case, you have 4 entries per actual person.
markt
You're correct that it becomes difficult to model things that are both an actor and a director simultaneously; I hadn't really considered that case at the time.
GWLlosa
+2  A: 

If the purpose of the actor and director lists in your XML is to identify the actor/director then I would focus on the form of identification rather than what sort of class hierarchy to use to represent the referenced identities.

Take for example, a mailing list - with a mailing list you would not concentrate on the types of buildings, but would concentrate on coming up with a form to identify the location.

The same probably applies here, particularly as it sounds to me like you are not (and probably should not) declaring properties for your actor/director 'persons' directly within your movie elements.

Instead it sounds like what you really want in the movie element is an identification which can be used to cross reference to a person.

The form of identification used is up to you, but a string which is used to contain the full name of the person might well be sufficient. Of course there's innumerable other options you could choose... e.g. you could use a 'Name' structure which is split into Forename, surname, middle initial components, or you could use a unique integer ID (assuming you then used this ID to lookup a Person object which has name properties etc.).

Here is an example of the xml structure, assuming that you were to store both persons and movies within a single document:

<yourDocument>
   <movies>
      <movie id="Bladerunner">
         <actorRoles>
            <actorRole id="Rutger Hauer" character="Roy Batty"/>
            <actorRole id="Harrison Ford" charactor="Rick Deckard"/>
         </actorRoles>
         <directorRoles>
            <directorRole id="Ridley Scott" category="Principle"/>
            <directorRole id="Jordan Cronenweth" category="Cinematography"/>
         </directorRoles>
      </movie>
      <movie id="The Room">
         <actorRoles>
            <actorRole id="Rutger Hauer" character="Harry"/>
         </actorRoles>
         <directorRoles>
            <directorRole id="Rutger Hauer" category="codirector"/>
            <directorRole id="Erik Lieshout" category="codirector"/>
         </directorRoles>
      </movie>
   </movies>

   <persons>
      <person id="Ridley Scott">
         <nationality>English</nationality>
         <birth>11/30/1937</birth>
      </person>
      <person id="Harrison Ford">
         <nationality>American</nationality>
         <birth>7/13/1942</birth>
      </person>
      <person id="Rutger Hauer">
         <nationality>Dutch</nationality>
         <birth>1/23/1944</birth>
      </person>
      <person id="Jordan Cronenweth">
         <nationality>American</nationality>
         <birth>2/20/1935</birth>
      </person
   </persons>
</yourDocument>

See what I mean?

What you really have is actor and director roles that use id's to cross reference to person objects - you could just as easily add a name property to the person objects and use unique integer IDs to cross reference to your Person objects where you can then retrieve their name and any other property. (using a unique non-changing integer ID would also allow support for updating a person's name without causing problems to your cross references)

Since there probably aren't any properties common to actor and director roles, I probably would avoid having a base class for 'roles'.

Phil
markt