0

So, this isn't a super easy question to describe, as it's a bit conceptual in nature, but here goes:

I'm building a C# WPF application to manage Film Production and Scheduling. I'm using a Model-View-ViewModel design, and my question is solely related to the model. I'll try to keep this out of the problem domain, but I have to explain a little bit.

So basically, the program has the concepts(classes) of "ShootingDays", "Scenes", "Shots", and "FilmTasks". You define the days you plan on filming ("ShootingDays"), the details of your scenes ("Scenes"), and shots within that Scene ("Shots"). Then, within a "ShootingDay", you can create a schedule for that day which includes "FilmTasks", individual agenda items. These "FilmTasks" can be general tasks (IE Arrive to location) or they can be a specific scene. Those "Scenes" within "FilmTasks" then also contain the "Shots" within the scene.

To Summarize:

The Project has Shooting Days and Scenes

A Shooting Day has FilmTasks

A Scene has Shots

A FilmTask has one Scene or none

When I first approached this problem, I thought that this made a ton of sense as a relational database. Give every object a unique identifier and identifiers for the objects they belong to. Simple joins and I'm good. But I'm not making a web application, I'm reading from XML files on the desktop. So my first go at this, I just emulated a relational table by giving everything a unique ID from a static method that would always return a different integer. I did my "joins" based on these, but it seemed super messy, particularly when it came to serializing the project, which meant serializing the last unique integer returned, so I could continue from there.

So when I faced that, I decided to refactor everything to a more simple hierarchy, where Scenes have a list of Shot objects instead of a list of integers to do the "joins" on, and the other relations in the same way. But now I'm facing update anomalies, for example adding or deleting Shots from a Scene after it has been added to a FilmTask, the Scene object inside the FilmTask doesn't change. I could manually change all of them, searching through all the tasks to find the relevant ones, but that seems super messy too.

I hope this isn't too confusing. Basically I feel like both the approaches I've taken are sub-optimal and I'm looking to clean things up. What can I do here? And if any clarification is needed, I can provide.

  • You probably need a "hybrid". Scenes can contain Shots so you don't need a join there, but FilmTask should not contain any Scenes (and their Shots). FilmTask should only have a reference to the Scene that it is about. – Marjan Venema May 25 '15 at 13:37
  • Are you actually intending to store the data in a relational database, or were you just working in a way conceptually similar to a relational database? – Jo Douglass May 25 '15 at 21:10
  • Just a conceptually similar way. Objects are just in regular C# data structures while the program is running and get serialized to an xml file for saving and opening. – Steve Fallon May 25 '15 at 21:39
  • Ah well, my answer probably covers a bunch of stuff you don't need, in that case! Feel free to ignore those bits. ;) Might help someone else who finds this while looking for a solution that does require a relational database, so I'll leave it be. Hopefully the non-relational parts of my answer do cover your question. – Jo Douglass May 25 '15 at 21:48

1 Answers1

1

Relational models and object models are fundamentally incompatible. This issue is known as object-relational impedence mismatch, if you want to read up on the topic.

If you need to produce both a relational database and an object oriented program you should do pretty much what you started with - begin with concepts, and sketch out a model. This is a great starting point for both of the models you'll need. After that, they'll diverge - use the same conceptual model to produce one (normalized, relational) model for the database, and a separate class model for the OO program.

It sounds like you'd started to go the right way with the OO side of things - rather than trying to replicate the relational model, you were having one object hold a container full of other objects. I don't think the issue you were having is to do with your structure, but a more technical issue - it sounds like the Scene is holding a copy of your Shot objects instead of a reference to them. If you take a copy of an object, then that copy will continue to look however it did at the point it was copied, regardless of what happens to the original object.

Conceptually, holding a reference to the object will be much more similar to what you originally tried to do with the IDs. However, in the code, holding a copy or holding a reference can look very similar - read up on this (here's a related question you might find useful), and have a look at how you're passing the Shot objects to the Scene object. If you're still stuck after that, edit your question and include the code where this happens.

Coming back round to the object vs. relational issue, if you do need both a relational database and an OO program at some point, then you'll need to do some kind of object-relational mapping between them. You might consider a layer of classes which handle this transformation. You could use an ORM tool like the Entity Framework and let Microsoft do the hard work, but if you're learning, then in my experience it's well worth the time and effort to understand how to do it for yourself, and it gives you full control over both structures.

Fire me a comment if I'm misunderstanding you on any point and I'll be glad to revisit this.

Community
  • 1
  • 1
Jo Douglass
  • 2,055
  • 1
  • 19
  • 30