The Model layer represents your data structures and business logic. It is the domain-specific representation of the information that the application operates on. Many applications also use a persistent storage mechanism (such as a database) to store and retrieve data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model Layer. This is the most important part of your application and it is usually modeled by ColdFusion components (CFCs). You can even create the entire model layer in another language or physical location (Java, web services).
All you need to understand is that this layer is the layer that runs the logic show! For the following example, I highly encourage you to also do UML modeling, so you can visualize class relationships and design.
There are tons of great UML resources and tools. Here are some great tools for you:
- ColdFusion generation templates for Enterprise Architect - https://github.com/lmajano/EA-ColdFusion-CodeGeneration
Since some of the examples in this section require the usage of ColdFusion ORM and the ColdBox ORM Extensions module (https://github.com/ColdBox/cbox-cborm), let's use CommandBox to install them in our application in two easy steps:
1. Install Module
2. Add Mapping Open your
Application.cfcand add a mapping to this module:
this.mappings[ "/cborm" ] = COLDBOX_APP_ROOT_PATH & "modules/cborm";
Unfortunately, we cannot use the ColdBox Module CFMappings to do this because the ColdFusion ORM loads before anything in our system.
ColdBox has a core validation module you can install to provide you with robust server-side validation of model data. You can install it via CommandBox:
You can find the documentation and source in its repository: https://github.com/ColdBox/cbox-validation
Let's say that I want to build a simple book catalog and I want to be able to do the following:
- List how many books I have
- Search for a book by name
- Add Books
- Remove Books
- Update Books
There are several ways I can go about this and your design will depend on the tools you use. If you use an ORM like ColdFusion ORM you most likely will use either an ActiveEntity approach or build virtual service layers or build a service layer based on our Base ORM services. If you are not using an ORM, then most likely you will have to build all object, service and data layers manually. We will concentrate first on the last approach in order to do our modeling and then build them out in different ways.