tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/673-service-used-once-doesnt-sound-good-how-to-write-it-correctlyRobotlegs: Discussion 2012-11-13T10:42:00Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/203166942012-11-02T02:21:34Z2012-11-02T02:21:34ZService used once doesnt sound good. How to write it correctly?<div><p>In my opinion I think it's still best to make it a service --
that's<br>
what we did (in my previous company) with all our Robotlegs apps
that<br>
used a SQLite database.</p>
<p>If it's the "it only runs one time" part that bothers you, you
might<br>
want to take a look at this example:</p>
<p><a href=
"https://github.com/probertson/robotlegs-examples-AddressBook/">https://github.com/probertson/robotlegs-examples-AddressBook/</a></p>
<p>In particular, look here:<br>
<a href=
"https://github.com/probertson/robotlegs-examples-AddressBook/blob/master/src/org/robotlegs/examples/addressbook/controller/setup/ConfigureServiceCommand.as#L27">
https://github.com/probertson/robotlegs-examples-AddressBook/blob/m...</a></p>
<p>Joel Hooks wrote most of the example, and I wrote the database
part of<br>
it.</p>
<p>The line I linked to is in the ConfigureServiceCommand class. In
this<br>
app there are a few bootstrapping commands that runs once at
startup<br>
for setting up all the mappings. In the case of the service<br>
bootstrapping command, it also configures the shared database<br>
connection that is used by several of the services. (In this app
we<br>
used a helper library for the SQLite so it's setting up the
helper<br>
library rather than directly setting up the database connection --
but<br>
conceptually it's the same thing.)</p>
<p>There's a class that it uses to create the database the first
time you<br>
run the app. After the first time that code never runs again, so
it<br>
seems silly to map it every time, so at Joel's suggestion we made
it<br>
only create that "service" and map it the one time:</p>
<p>if (!dbFile.exists)<br>
{</p>
<pre>
<code>var creator:DatabaseCreator = injector.instantiate(DatabaseCreator);
creator.createDatabaseStructure();</code>
</pre>
<p>}</p>
<p>(The reason we use the injector to create it is because the
DatabaseCreator class has [Inject] tags in it, so that way the
injector<br>
creates it and fulfills the injections.)</p>
<p>Anyway, that sounds like it's similar to the situation your
facing, so<br>
maybe it's helpful.</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/203166942012-11-08T13:35:46Z2012-11-08T13:35:46ZService used once doesnt sound good. How to write it correctly?<div><p>@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.</p>
<p>BTW. Sorry, that I was so absent minded and responded week later
after your response...</p></div>maciekrei