|
Hello,
This seems to be a design problem to me (but could
also be ignorance) and maybe somone can see a better way:
I'm working with a DAO and a Gateway for, lets say,
multiple article retrieve, I have a DAOFactory that instantiates the DAO
depending on the type (eg: MSSQL, ORA etc).
I want a struct of article objects, so what I do is
send a getArticles() to the gateway, it runs a query, loops over it and creates
one object for each row, storing them in the struct:
[articleGateway.cfc getArticles()]:
cfset stArticles = structNew()
[loop over articlesGateway]
stArticles[key] =
createObject("component","article").init(stConfig).getArticle(query
values)
[/end loop]
return stArticles
Inside the bean I init the DAO from the
factory:
[article.cfc init()]:
cfset variables.articleDAO =
createObject("component","model.factory.DAOFactory").init(arguments.stConfig).getDAOFactory(factoryType:arguments.stConfig.dbtype).getArticleDAO(arguments.stConfig)
First I had this in the constructor, but then I
decided to move the "stConfig" from the constructor to the init, in order to
avoid
duplication of the XML functions that are parsed
everytime I instatiated the config.cfc.init() method
(actually, I got it under the application scope,
but didn't feel right to have dozens of calls to the method, so I decided moving
it with every call
to init methods on my app, like Mach-ii dos with
the appManager).
The problem is that everytime an articleBean is
instantiated it has to call the DAOFactory, and eventually this will start
decreasing performance at runtime.
So if I have to instantiate, lets say, 20 articles,
I would have to init the DAOFactory 20 times, it dosen't seem
right.
To resolve the problem, I could instantiate the DAO once, and send it to the init() method
of the article everytime, but this seems an overkill:
(eg: stArticles[key] =
createObject("component","article").init(stConfig,articleDAO).getArticle(query
values)
I would also have the article bean init() depending
on receive the DAO, which I don't want, since sometimes I want to create a blank
article,
or even an article populated by a form, and I dont
need a DAO for doing this.
What I could do, if I could use my old CF
skills woul be:
[article.cfc init()]:
<cfif
NOT isDefined("request.articleDAO")>
<cfset request.articleDAO =
createObject("component","model.factory.DAOFactory"). ... />
</cfif>
<cfset variables.articleDAO =
request.articleDAO >
This way I wouldn't have 20 instantiations of the
DAO, but it doesn't seem right too ... I shouldn't be allowd to use the
request scope, should I ?
I guess this could be done with a singleton for
managing the DAOFactory, and instead of creating it everytime I could get it
from the singleton, but is this right design ?
Is there any better way of doing this
?
[]s Marcantonio
Silva |
- [CFCDev] How to avoid duplication of objects Marcantonio Silva
- Re: [CFCDev] How to avoid duplication of objects Phil Cruz
- Re: [CFCDev] How to avoid duplication of object... Marcantonio Silva
- Re: [CFCDev] How to avoid duplication of objects Patrick McElhaney
- Re: [CFCDev] How to avoid duplication of object... Marcantonio Silva
