Search code examples
.netwpfobservablecollectiondata-modelingclass-design

Modelling an entity with a collection property


I want to design a Movie class. Now a movie will have a number of genres assigned to it. Ex. The movie Avengers has genres : Action, Adventure, Sci-Fi. Now my application maintains a list of all the genres from which a subset can be assigned to a movie.

My existing model design has a Movie entity with a collection of the entire Genre list. The Genre class simply has an IsSelected property to help identify the subset of genres that have been assigned to a particular movie. This offers an advantage as when I design the user interface to create a new movie, I display all available genres out of which the user selects/checks the ones applicable to the movie.

public class Movie
{
    public ObservableCollection<Genre> MovieGenres { get; set; }
}

However, I believe this one downside to this approach. Supposing I have around 25 list of genres and around 500 movies, then around 25 x 500 genre objects are created.

Do you think this should be avoided ?

Is there any better approach to associate the genres with a movie object which will reduce the need to create so many genre objects as well as offer the same UI advantage ?

Or is there any better approach to design my user interface that I can leverage from by model design ?

(I am not using any ORM tool, neither planning to use in the future)


Solution

  • Your approach is certainly not the recommended one for a problem like yours.

    Normally, one would define a multi-valued reference property genres in the Movie class, and this property would hold only the genres assigned to a specific movie.

    Notice that, logically/conceptually, you deal with a unidirectional association between Movie and MovieGenre, assigning genres to movies.