ochurlaud
ochurlaud

Reputation: 410

C++/OOP Associations model and database

Edit: I called it association because in my head it should be this, but it seems that I implement it as an aggregation... This can be also discussed...

Context

What you learn in IT is that when you do associations, you should use pointers.

For example: You have 2 objects: Person and Movie. One Person can do several Movie and one Movie can be done by/with several Person. You would then have:

class Person
{
    public:
        Person::Person();
        int id;
        vector<Movie*> movies;
};
class Movie
{
    public:
        Movie::Movie();
        int id;
};
main()
{
    Person person;
    Movie *movie = new Movie;
    person.movies.push_back(movie); // With a setter it would be better but...
}

or something like this (please correct me if I do something wrong =D)

Where the troubles appear

Now you have many persons and movies and you want to save it somewhere: in a database.

  1. You get your person
  2. You get all the movies it is associated with in order to construct the whole object.

But how do you get them?

I'm interested in simplified/pseudo code as examples, dissertation... Thx a lot !

Upvotes: 1

Views: 92

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Your question is very broad, and there's a number of approaches, how to bind database tables (and represent their foreign key connections).

It's not really only how to represent/handle that kind of Domain Model snippet, you're presenting in your code sample here.

@Martin Fowler provided the EAA pattern catalogue you could reasonably research, and apply appropriate patterns for these kind of object <-> relational mapping problems you address.

Upvotes: 1

Related Questions