After the introduction to the Running Objects with the tutorial Movie Database in 2 Minutes (available here), I would like to demonstrate how Running Objects interprets the Associations where we will cover:
- Direct Association – A reference to another complex object.
- Aggregation – A collection of another complex object.
For those coming with a database perspective, by demonstrating these associations we will also exemplify the underline relationships such as 1 to Many and Many to Many relationships.
We will perform the following steps to accomplish our goal:
- Creating the Application Project
- Modeling our Domain Models
- Implementing our Classes
- Running the Objects ;)
Creating the Application Project
As in the Movie Database tutorial, the first step is to create a ASP.NET MVC 3 project and then select the Running Objects template as shown in the images below:
|Create a new ASP.NET MVC 3 Web Application ||Select the Running Objects project template |
| || |
Modeling our Domain Models
Now that we have created the project we can start working in our model. To demonstrate how to use associations between classes with Running Objects we will create the class model as the following figure:
Movie Database Class Model
We can see that we have an aggregation of Movies in Category and an aggregation of Categories in Movie, it represents our Many-to-Many relationship. In the other side, we have a 1 to Many relationship through a direct association from Movie to Director and we also have an aggregation of Movies in Director.
Implementing our Classes
Now that we have designed our model, we need to implement the classes. Lets take a look at the source code and see how it is implemented.
Basically this is what you need to develop to get the application running. But as we are using Entity Framework as our repository, we need to create the Data Context class for it. It is very simple and straight forward, we just need to tell Entity Framework about our model.
Entity Framework Data Context
Now that we have developed everything we need, what is next? How to configure Running Objects to understand this model?
Well, we don’t need to do anything. The previous configuration from the tutorial Movie Database in 2 Minutes is enough to make RO to understand our model. But for highlighting purposes, let see how is the configuration for our project.
Running Objects Configuration
Running the Objects
Now, we just need to run the application. In the first run it will create the database (Entity Framework does it automatically) and consequently take more time to run, after that everything will be fast and fine.
The following screen demonstrate constructor of movie receiving a collection of Categories and one Director.
New Movie using Associations
Download the Source
The source code of our application is available here.
Running Objects handles associations naturally, it uses the principles of Object Orientation and the power of .NET reflection to instruct and understand our model. We will cover more topics in how to use RO in the next posts.