tags:

views:

1863

answers:

2

Hi,

I'm a little confused as to how the model should 'work' in my basic C++ implementation, or rather how the data from say the database backend should be encapsulated/worked with.

My thoughts at the moment are for a model with for example a static findById() method, which would return an instance of that same model, which would then represent a single 'row' of data (permitting manipulation etc etc) might be a suitable approach.

In terms then of creating a new row in the database, I suppose a new instance of the model would be created, populated with data, and effectively a save() method called?

Hopefully I'm on the right lines, but am just a little confused over the implementation/design here.

Any advice would be greatly appreciated.

Thanks in advance, James

+1  A: 

First advice: the "Gang of Four" Book - Design Patterns by Gamma et al. It explains MVC and the other necessary pattern: observer.

Wikipedia might help as well.

Basically: the model represents the data and it's possibilities to manipulate them. It's a class. The model must not know the view or the controller. It must have possibilities to notify others of changes without knowing them (observer pattern, signals in qt for example). The View displays the data and reacts on changes. It registers as observer for the model and knows the model in most cases (no need to decouple there). Several views may use the same model. Same for the controller. On most modern UIs, view and controller are tightly coupled, sometimes even the same object (e.g. a slider showing a value and at the same time changing it by moving it).

For historical reasons: lern Smalltalk (for example Cincom Smalltalk).

Tobias Langner
+4  A: 

So, I think you are asking what kind of interface would be appropriate to "translate" between a relational database and an object oriented application, particularly in the context of an MVC application written in C++.

A common approach is called object-relational mapping, or ORM. I'm only familiar with how Ruby on Rails implements ORM, but if you carry it over to C++ it looks like this:

  • A database table maps to a class
  • Operations on a table (such as queries) map to static member functions of the corresponding class
  • Rows in a table correspond to instances of the corresponding class
  • Fields in a table correspond to member variables of the corresponding class

There are probably C++ libraries to do the ORM mapping for you. I'm not familiar with any myself since I've never had to do this in C++.

Edit: This question asks about ORM libraries for C++.

Darryl