Service used once doesnt sound good. How to write it correctly?

maciekrei's Avatar

maciekrei

02 Nov, 2012 01:12 AM

Hello,
I'm writing an application that uses sqlite database. To contact with database data, I've wrote suitable database service, which dispatches events with result or error from given query. The problem is, when application starts for the first time, I have to initilized it with create queries. And here is problem, how type it correctly?

Firstly, I thought about command. However, to init database, I have to make query with creates and listen for result event of the last query. Then I'm certain, that all tables were created. It seems, that it is wrong to listen in command for event (because it is hard to do, so I guess it is unproper).

Should I create specific service to make query, listen to it and then return proper response? Service, that is used only once in all application's life span doesn't sound good.

I could do this in mediator of first view, after context startup event, but I dont want to throw every initilize code into main view class.

What is the best way to type this database init? I'm bugged with it, so I'ld be glad with any advice.

EDIT. I'm using Flex 4.6 and Robotlegs 1.5.2.

  1. 1 Posted by Paul Robertson on 02 Nov, 2012 02:21 AM

    Paul Robertson's Avatar

    In my opinion I think it's still best to make it a service -- that's
    what we did (in my previous company) with all our Robotlegs apps that
    used a SQLite database.

    If it's the "it only runs one time" part that bothers you, you might
    want to take a look at this example:

    https://github.com/probertson/robotlegs-examples-AddressBook/

    In particular, look here:
    https://github.com/probertson/robotlegs-examples-AddressBook/blob/master/src/org/robotlegs/examples/addressbook/controller/setup/ConfigureServiceCommand.as#L27

    Joel Hooks wrote most of the example, and I wrote the database part of
    it.

    The line I linked to is in the ConfigureServiceCommand class. In this
    app there are a few bootstrapping commands that runs once at startup
    for setting up all the mappings. In the case of the service
    bootstrapping command, it also configures the shared database
    connection that is used by several of the services. (In this app we
    used a helper library for the SQLite so it's setting up the helper
    library rather than directly setting up the database connection -- but
    conceptually it's the same thing.)

    There's a class that it uses to create the database the first time you
    run the app. After the first time that code never runs again, so it
    seems silly to map it every time, so at Joel's suggestion we made it
    only create that "service" and map it the one time:

    if (!dbFile.exists)
    {
    var creator:DatabaseCreator = injector.instantiate(DatabaseCreator);
    creator.createDatabaseStructure();
    }

    (The reason we use the injector to create it is because the
    DatabaseCreator class has [Inject] tags in it, so that way the injector
    creates it and fulfills the injections.)

    Anyway, that sounds like it's similar to the situation your facing, so
    maybe it's helpful.

    Paul

  2. 2 Posted by maciekrei on 08 Nov, 2012 01:35 PM

    maciekrei's Avatar

    @Paul Thank you for your advice. It was very helpful. I ended up on making one-time-use service for application setup, although for database I used existing service, that was designed to run database queries. I guess one init function in service designed to handle sqlite shouldn't be inappropriet.

    BTW. Sorry, that I was so absent minded and responded week later after your response...

  3. Ondina D.F. closed this discussion on 13 Nov, 2012 10:41 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac