But let me start at the beginning. I believe that it is quite a good idea to have a playground, an abstract idea or project, where you can try out new technologies. If you implement this idea with different technologies this gives you a good overview about the advantages and drawbacks of the different approaches. Well, my playground is called the "Moviebase". Long, long time ago when I made my apprenticeship I learned a bit about web servers, PHP and databases and so I came up with the idea to develop a small application to manage my movies.
O.k. the desing was not perfect but actually stable enough to work for several years. Later in university I learned a lot about fancy new technologies like servlet containers, web services, AJAX, RSS, Hibernate (at this time they were new) and all the other interesting technologies which let the buzzword catalogue grow bigger and bigger. So I decided to re-implement the Moviebase using the new stuff.
Well, this is also some years ago, but I remember having some trouble with the following scenario:
A movie has actors. So far so good. That's actually true for most movies - except documentations. But to be honest, documentations are not the majority in my movie catalogue. Because I haven't heard about modelling or EMF at all I started coding this specific classes and lists for it by hand. I quick typing action and I was able to get all actors from the movie. But movies have directors, too. O.k. this brought up the base class Person and it's subclasses Actor and Director. Added getter and setter and it was done. Hmmm, who wrote the sound track could be interesting, too (especially if you intent to write a MusicBase and combine it with the MovieBase). So again some typing actions brought the needed code.
That's enough for the model. And now it is time were the trouble starts. Getting/setting actors and so on is nice. But what if you would like to see all persons involved in a movie? O.k. writing a method called "getPersons" would do the trick - if you take care that the persons are not doubled - some actors are also playing director or compose the music for the movie. And deleting a person is even worst. But that is not the real problem. For now we took to a look at movies. But what about the Person object? Don't you want to know in which movies he played or where she was director? Or maybe you want to retrieve every movie the person was involved? Writing this and taking care for the integrity of all lists could be quite troublesome.
But to cut the long story short, all these problems are easily solved with EMF. To be exact - with FeatureMaps. To be honest I never liked FeatureMaps before I used them. ;)
But you can easily model the above mentioned scenario Just create an attribute with type FeatureMapEntry in your movie called "persons". Then add actors, directors and composers as may-valued references and combine them using the EAnnotations described in the EMF book. Do the same for the persons and let them point the the specific opposite in the movie. E.g. Person.moviesAsActors point to the actors feature in the movie. That's it. Now you have bidirectional FeatureMaps. Just generate the code and EMF will care for the rest. You can add movies to actors or directors to movies and the whole synchronisation will be done by EMF - groovy. Just see the snippet below for an examples
Person person = MediabaseFactory.eINSTANCE.createPerson();
Movie movie = MediabaseFactory.eINSTANCE.createMovie();
System.out.println("persons:" + movie.getPersons().size()); //prints 2
System.out.println("actors:" + movie.getActors().size());//prints 1
System.out.println("directors:" + movie.getDirectors().size());//prints 1
System.out.println("composers:" + movie.getComposers().size());//prints 0
System.out.println("MyMovies: " + person.getMovies().size());//prints 2
System.out.println("MyMovies as actor: " + person.getMoviesAsActor().size());//prints 1
System.out.println("MyMovies as director : " + person.getMoviesAsDirector().size());//prints 1
System.out.println("MyMovies as composer: " + person.getMoviesAsComposer().size());//prints 1
And that is not only reliable and stable but can also be modelled quite fast. If you know what to do you can define this model in just a few minutes. Not yet convinced using EMF? Try it out and I guess you will ;)
This was a small insight into the advantages EMF offers. Unfortunately my time is limited at the moment, so the MovieBase development slowed down. But I already had some time to play with EMF Databinding - and hey - it rocks too ;)
So, wherever you are - Have fun and enjoy the springtime. :)