objectFactory

32 views
Skip to first unread message

Scott Conklin

unread,
Feb 22, 2011, 1:36:36 PM2/22/11
to framew...@googlegroups.com
up until now i have been using the objectfactory.cfc that is used in the user samples of FW/1 for all my DI needs.
I could not immediately see a way to parameterize the arguments to a bean definition. Would i need to get coldspring if i wanted to do that? 

Actually what i am trying to do is follow the practice of creating a globalconfig function that returns all my configurable settings much like the
the FW/1 config object.  I  thought I would put my dsn settings in this config struct and have my beans.xml.cfm file read this through the globalconfig 
function defined in app.cfc, but that of course is not working. 

i think i want to inject my services/gateways either in the init method or through setters/getters with the DSN and NOT just reference application.dsn like 
some have suggested for simplicity  sakes. 

can anyone tell me how they are best handling this? i did not really want to have to directly call something like a configure() method in setupapplication
to set this for each service or gateway that needed it. what about a base object? 

Javier Julio

unread,
Feb 22, 2011, 1:55:33 PM2/22/11
to framew...@googlegroups.com
Scott,

Awesome! Glad you're using it! I developed that to learn how recursion works and was the first pattern that really made sense to me. We had a competition at an old job several years ago (maybe 06) where we were trying to create a lightweight DI tool since we felt ColdSpring was to much for us.

The UserManager app in the /assets/config/beans.xml.cfm file you'll see a commented out block that details how you can specify default argument or property values for beans. These can be complex values too, for example an argument can have a default value of a struct or array. You can nest those as well (hence the recursion capability). I've pasted the commented out docs from that file below:

<bean> - Define a bean with an id (referenced using that value), the class path 
(can use a mapping), and whether its a singleton or not (boolean)
<argument> - Defines a constructor argument for a bean, name must match CFC argument name
<property> - Defines a property for a bean that is set via the set{propertyName} method
<ref> - Defines a reference to an existing configured bean, the name attribute 
would be the id of the bean being referenced
<array> - Define an array for a property or argument
<struct> - Define a structure for a property or argument
<element> - Define an element for an array or structure, you can define the value 
within the element using a value tag (<element><value>some value</value></element>) 
or using a value attribute, you can mix and match if you like
<value> - Define a value for an element in a structure or in an array
Examples:
<property values="names">
<array>
<value>Moe</value>
<element value="Curly" />
<element>
<value>Joe</value>
</element>
<element>
<array>
<element value="John" />
</array>
</element>
</array>
</property>
<argument name="states">
<struct>
<element key="NY" value="New York"/>
<element key="NJ">
<value>New Jersey</value>
</element>
</struct>
</argument>

I don't see why you can't have say a Config.cfc or Settings.cfc, define it as a singleton and just define the values for all parameters in the XML file for the object factory. Does this not solve your issue? This way you can just inject that Config or Settings object in any of your controllers or your services and reference any values you need. Hopefully haven't missed anything. Let me know if I can be of more help.

Ciao!
Javi


--
FW/1 on RIAForge: http://fw1.riaforge.org/
 
FW/1 on github: http://github.com/seancorfield/fw1
 
FW/1 on Google Groups: http://groups.google.com/group/framework-one

Scott Conklin

unread,
Feb 22, 2011, 2:21:30 PM2/22/11
to framew...@googlegroups.com
Javi--
 
thanks for your quick reply. i am aware of the commented out section on usage; i have referred to it often.
what i was trying to do was something like 

<argument name="dsn">
<struct>#getConfig('dsn')#</struct>
</argument>

but that did not work as you already now.  (i agree on CS; just don't need the overhead)

I believe your idea of  config.cfc directly in the beans file where i define the settings there is a good one. i will give that approach a try.
thanks.

I am also curious how the new DI/1 version coming out in v2.0 address this if at all? 


Javier Julio

unread,
Feb 22, 2011, 4:02:55 PM2/22/11
to framew...@googlegroups.com
Scott,

Yeah you can't actually inject specific values into a bean from another bean. Although that is a great idea. Since I work primarily with Flex I use Swiz and it has the capability not to only inject a bean, but inject a specific value. For now the ObjectFactory only has the capability to inject beans so as an example with a Configuration object you'd have something like the following:


<bean id="configuration" class="userManager.services.Configuration" singleton="true">
<argument name="dsn" value="myDatabaseDSN"/>
</bean>
<bean id="departmentService" class="userManager.services.Department" singleton="true">
<argument name="config">
<ref name="configuration" />
</argument>
</bean>
I would argue for injecting an object though in this case. It's just much more flexible since this way you have everything config related together and easily accessible. Although you could just have a DSN or DatabaseConfig object for example if you just want to only have your dsn(s)/db info there.

Just to make sure because your wording makes me think that you see this as the first attempt at DI/1 which it isn't. Just something I did myself and included in the demo app so it would have no dependencies. I too am very much looking forward to DI/1's release! Curious to see what approach Sean takes. Hoping he'll release soon along with FW/1 2.0.

Reply all
Reply to author
Forward
0 new messages