Persistent Application Settings In Model-Glue

Posted By : todd sharp Posted At : February 25, 2007 5:22 PM Posted In: Model-Glue, Project Learn, OO

6

My next stumbling block on my journey is the creation and persistence of application level data. Specifically the DSN for my application. Don't worry, when I get over these initial stumbling blocks I'm sure everything will run a lot smoother, but I keep sharing these items for a simple reason. I think these little things are the kind of things that trip up a lot of folks when trying to adopt new concepts and frameworks. I myself have let little things like this discourage me and I move on to things that are familiar and comfortable and forget all about the learning that I had done.

So back to my issue. Model-Glue ships with a way to create simple config beans in the ColdSpring.xml file. Very simple indeed - here's an example.
<bean id="datasource" class="ModelGlue.Bean.CommonBeans.Datasource" singleton="true">
    <property name="dsn">
        <value>dsn</value>
    </property>
    <property name="Username">
        <value>username</value>
    </property>
    <property name="Password">
        <value>password</value>
    </property>
</bean>
Simple enough. Now I can simply pass this bean to anything that needs it like so:
<bean id="userDAO" class="model.user.userDAO">
    <constructor-arg name="dsn"><ref bean="datasource" /></constructor-arg>
</bean>
This requires a little modification to the userDAO that Illudium generated for me since the constructor that it created expected a simple string for the dsn. In my case I modified the argument to except the datasource bean itself (ModelGlue.Bean.CommonBeans.Datasource) and used the getDSN() method of that to set the dsn.
<cffunction name="init" access="public" output="false" returntype="model.user.userDAO">
    <cfargument name="dsn" type="ModelGlue.Bean.CommonBeans.Datasource" required="true">
    <cfset variables.dsn = arguments.dsn.getDSN()>         <cfreturn this>
</cffunction>
I'm not 100% positive this is the best way to handle things. Time to hit the docs to confirm. Stay tuned.

Comments (6)

Fredo's Gravatar Gosh, that seems so much more intuitive than cfset application.dsn = 'fooBar'

Sean Corfield's Gravatar The benefit of using the Datasource bean is that you get DSN, username and password which you need in your query code on certain systems (for example, on HostMySite by default, DSNs do not have username / password in them as a security precaution so you need to put them in your own code). I've gotten into the habit now of *always* using username / password in my query code (and both Reactor and Transfer support this out of the box).

ed's Gravatar Being pragmatic, another advantage of passing beans is that you can add methods to your Datasource bean without needing to modify the arguments of your userDAO.
<p>For example, say you remodel your database and subsequently need to provide a means to retrieve a default schema for your remodeled datasource.
<p>If you pass a Datasource bean into your userDAO, no problem - just add a getDefaultSchema() method to your Datasource bean and there's no need to edit userDAO (other than to retrieve the value of that method, obviously).
<p>If you're passing Datasource variables as explicit arguments, you've got to modify your userDAO to add a new 'default schema' argument. Since the datasource is likely to be used throughout your application, you probably have to edit another bunch of files to add that argument too...

Lola LB's Gravatar Have you got this process confirmed? Got it working for you?

todd sharp's Gravatar Lola: Yes it does work well. I've been meaning to get another post up on this topic just been busy on a complete rebuild at work.

Part of what I'd like to discuss in my next post though is that there really is no "right way" of doing things - there are best practices, etc but we need to remember that things are cut in stone.

But this method did work. In fact, I've got an email from someone who has created a custom template for the Illudium generator that will create your code to accommodate this style. Looks very interesting and I may re-publish the entire email (with his permission). I hope to dig back in to all this within the next few days! ;)

Thanks for following the series and commenting! It helps reaffirm that "I'm not the only one"!

todd sharp's Gravatar Things are **NOT** cut in stone :D

Woops!