I am going to start doing a couple of Adobe AIR projects that will be using SQLite feature provided by AIR. Since this is the first time I am attempting to do this, I would appreciate some pointers, tips and Best Practices for development.
Since this application will be accessing a local db, I think I can open a connection to the database at the start of the app and keep it open till the application closes. Is this a right approach to use here?
How should design my application if I am using an MVC framework like Mate or Cairngorm?
Should I create some wrapper class to perform the db operations so I can reuse this in some other project? Looking forward to some valuable information…
The first and by far most important decision you have to make, is whether you’re going to access that database synchronously or asynchronously. They both have their pros and cons, so it will depend on your situation.
Synchronous
Good for small scale app
Asynchronous
LocalUserServiceandRemoteUserServiceboth implement an interface that forces them to have a methodsaveUser())Good for larger scale app and synchronizing local and remote data.
I tend to almost always opt for the asynchronous approach – since it’s more flexible – and abstract the complexity away in … a wrapper class. So that answers that part of your question.
Architecture
I don’t like frameworks, so I can’t answer your question about
Mateor -shudder-Cairngorm. But here’s what I consider a fairly good approach:UserDAOfor quering Users). That class should contain only queries.As for keeping the connection open. I’ve always done so and never had any issues with it. I don’t exactly know what happens when an application crashes and the connection wasn’t properly closed, but this is not Oracle or SQL Server we’re talking about. I don’t think SQLite will keep open pointers or something since it’s just a simple file, but I might be wrong.
Edit: some more details on the DAO / Factory pattern (as requested by OP).
An example of a UserDAO with one function:
As you can see, I’ve abstracted out the complexity into the
AsynchronousDAObase class, so we can see only the necessary information in UserDAO. ThehandleResultfunction is a callback that will be called whenever the query is ready, passing in the resultset as an argument. We’ll usually pass that resultset into the factory/builder class.An example of a UserBuilder:
This is obviously a simple example, but you can create more complex data structures in the builder. For some info on the difference between the Factory and Builder patterns I recommend Google.
Now let’s tie it together in the service class:
Finally, let’s put the trio to use:
For the example I constructed the classes with
newand passed the dao and the builder as constructor arguments, but you could use injection or singletons or whatever framework you like to do the construction for you.