One question that developers often ask when they see Flex for the first time is: “how do I write a database application?”
The answer to this question is typically: You use an HTTPService, WebService, or RemoteObject to connect to a server-side component that provides an API to manipulate your data. This approach provides infinite possibilities, but it makes the assumption that you already have that entire backend infrastructure in place. If you don’t, and all you want to do is build a rapid prototype or a simple application, this might look like a lot of work.
In addition to the RPC services mentioned above, the Flex Data Management Services (FDMS) provide a virtually code-free approach to synchronize data between the client application and the middle-tier. That takes care of 50% of our challenge: no data synchronization code to write at the client-side.
FDMS also provides a pluggable adapter and assembler architecture to handle changes at the server-side. As an example of writing a generic assembler, I built a simple JDBC assembler that allows you to develop rapid prototypes and basic database-driven applications without writing a single line of server-side code. All you need to do is specify the database connection information and the table you want to work with in data-management-config.xml.
This assembler is by no means a full featured solution, but it shows the power and flexibility of the FDMS architecture: you could easily extend its capabilities, or build other generic assemblers for existing persistence solutions (a Hibernate assembler is already shipping with the product).
The point is that if FDMS provides a solid architecture to handle the most complex persistence scenarios (with virtually no data synchronization code at the client side), with the addition of specialized assemblers, it can also provide simple solutions to simple use cases, significantly improve your productivity, and deliver end-to-end code-free persistence.
Installing the SimpleJDBCAssembler:
- To use the SimpleJDBCAssembler, you need to install the Flex Data Services. If you haven’t already done so, you can download the Flex Data Services here, and follow the installation instructions.
- Download jdbcassembler.zip here and expand it in your root directory (the files will be unzipped in a directory called jdbcassembler)
- Copy SimpleJDBCAssembler.class in {context-root}/WEB-INF/classes/flex/samples/assemblers. For example, if you installed the integrated server, copy SimpleJDBCAssembler.class in /fds/jrun4/servers/default/samples/WEB-INF/classes/flex/samples/assemblers.
Running the sample application:
- Copy the /jdbcdemo directory in the context root of your web application. For example, if you installed the integrated server, copy the /jdbcdemo directory in /fds/jrun4/servers/default/samples.
- Open {context-root}/WEB-INF/flex/data-management-config.xml and add the following destination:
<destination id="jdbc-product">
<adapter ref="java-dao" />
<properties>
<source>flex.samples.assemblers.SimpleJDBCAssembler</source>
<scope>application</scope>
<metadata>
<identity property="PRODUCT_ID"/>
</metadata>
<database>
<driver>org.hsqldb.jdbcDriver</driver>
<url>jdbc:hsqldb:/jdbcassembler/db/flexdemo</url>
<table>product</table>
<autoincrement>true</autoincrement>
</database>
</properties>
</destination>
- This example uses an embedded HSQLDB database. Make sure hsqldb.jar is available in the classpath of your web application. For example in {context-root}/WEB-INF/lib. If you are using another database, make sure the JDBC driver for that database is in the classpath of your web application.
- Start the server
- Access http://localhost:8700/samples/jdbcdemo/simpleclient.mxml. (Change the port number and the context root as appropriate). This is a basic client built with just a few lines of code. You can edit the data inside the datagrid.
- Access http://localhost:8700/samples/jdbcdemo/app.mxml. (Change the port number and the context root as appropriate). This is a slightly more sophisticated client. You edit data in a separate form.
Also notice that changes are pushed in real time to all the clients connected to a destination. For example, open the application in two browser sessions, change some data in one session, and notice that the changes are automatically pushed to the second session.