You generally don't inject transients into singletons. DI of
singletons is usually around injecting SIngletons into Transients -
for example having a User transient with a DAO or a Validator
singleton injected into it.
Usually if you DAO want's to return transients it's going to do
something like:
DAO.cfc
function getById ( Id ) {
var UserProperties = functionthatdoesthesql( Id );
var User = beanFactory.getTransient("User");
User.load( UserProperties );
return User;
}
Note the important points:
- var scope everything for thread safety in your singletons
- Always call the bean factory every time you want a bean. If you want
to loop over an array and create a bean for each of 200 records,
you're going to want to call beanfactory.getTransient() 200 times. If
you call it once, you only get 1 bean.
Best Wishes,
Peter
On Sep 14, 2009, at 2:22 PM, garrettjohnson wrote:
>
> Peter,
>
> Thanks for the help! I do not think it has anything to do with
> LightWire, more like my error!
>
> Your tests work fine...
>
> So:
>
> I inject my transient bean into my DAO via addSetterDependency(bean,
> dao)... I then defined my setHighschoolBean to take in my argument
> from the injector, so the getHighschoolBean() just returns that
> variable that gets set in the setter.
>
> Is that the correct way to inject a the transient into a singleton ?
>
>
>
>
>
> On Sep 14, 11:29 am, Peter Bell <[email protected]> wrote:
>> Hi Garrett,
>>
>> I'd be surprised if that is the issue. I can't see your
>> getHighSchoolBean() method, but if it's calling
>> getBean("highschoolbean") (or getTransient("highschoolbean") (either
>> is fine) then it should work just fine as LW *always* does a new
>> create object for transients. I'd check you haven't elsewhere done an
>> addSingleton("highschoolbean") (don't what would happen but it could
>> break things) and I'd also double check the query, etc. Also, feel
>> free to look at the lightwire code - if doesn't do much so it's
>> pretty
>> easy to understand and debug.
>>
>> I separately notice you're manually calling init on your bean - but
>> LightWire also calls that, so I'd consider having a separate load
>> method for your bean for loading up the data.
>>
>> If you *really* think the problem is in lightwire, create the
>> simplest
>> possible test case where you have code something like:
>> <cfscript>
>> HSBean1 = getBean("highschoolbean").load(title="title1");
>> HSBean2 = getBean("highschoolbean").load(title="title2");
>> </cfscript>
>>
>> HSBean1 Title = #HSBean1.getTitle()#<br/>
>> HSBean2 Title = #HSBean2.getTitle()#<br/>
>>
>> If that code DOES break, post sample code to the list including a
>> copy
>> of a bean with a load method and a getTitle method and I'll test, but
>> I'd be pretty surprised if it did.
>>
>> Best Wishes.
>> Peter
>>
>> On Sep 14, 2009, at 10:58 AM, garrettjohnson wrote:
>>
>>
>>
>>> I am trying out lightwire for the first time and I am noticing a
>>> small
>>> problem when it comes to returning an array of beans from my DAO. It
>>> seems that its my array is an array of the same exact bean, rather
>>> then multiple instances of my bean.
>>
>>> I have the following in my bean config:
>>
>>> addSingleton("educationService", "educationService");
>>> addSingleton("educationDAO", "educationDAO");
>>> addSingleton("educationGateway", "educationGateway");
>>> addTransient("highschool", "highschoolBean");
>>
>>> addSetterDependency("educationDAO", "highschoolBean");
>>> addConstructorDependency("educationService", "educationGateway");
>>> addConstructorDependency("educationService", "educationDAO");
>>
>>> then in my DAO layer:
>>
>>> <cffunction name="read" access="public" returntype="any"
>>> output="false" hint="I return a populated an arrray of highschool
>>> objects">
>>> <cfargument name="ID" type="numeric" required="true" />
>>
>>> <cfset var q = "" />
>>> <cfset var objects = arrayNew(1) />
>>
>>> <cfquery datasource="#getSettings().getDatasource()#"
>>> name="q">
>>> <!--- some query --->
>>> </cfquery>
>>> <!--- populate highschool Beans --->
>>> <cfloop query="q">
>>> <cfset objects[q.currentRow] =
>>> getHighschoolBean().init
>>> (q.highschoolID, q.graduationDate, q.highschoolGPA,
>>> q.highschoolClassRank, q.highschoolClassSize,
>>> q.highschoolPercentile,
>>> q.dateUpdated) />
>>> </cfloop>
>>
>>> <cfreturn objects />
>>> </cffunction>
>>
>>> Now I do not get an error or anything but if my query return 3
>>> records, lets say Foo high, Woo high, Moo high... then when I call
>>> my
>>> code in my cfm template.... loop array... #school.getName()#..... It
>>> will return Moo high 3 times, rather then what it should really be
>>> doing!
>>
>>> Any thoughts?
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CFCDev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cfcdev?hl=en
-~----------~----~----~----~------~----~------~--~---