2010年8月23日星期一

Re: [fw-mvc] Centralized Stack/Broker or something for Models

On Monday 23 Aug 2010 05:00:13 Michael Depetrillo wrote:
> I would think there is better performance by storing the data as a
> class property. Otherwise, you open/close the same cache file every
> time you access the cache from the front controller, controller,
> model, etc...

Most of the time you lookup the authenticated user. The user model acts with
Zend_Auth so in controllers only the api of the model is exposed:

- Application_Model_User::getAuthenticated()
- Application_Model_User::authenticate($username, $password)
- Application_Model_User::isAuthenticated()

And getAuthenticated() is obviously called my times during a request. The call
to the db is once made and then stored as static property. Look for a snippet
of my code at http://pastebin.com/iK1mfDmq.

Regards, Jurian
--
Jurian Sluiman
CTO Soflomo V.O.F.
http://soflomo.com

2010年8月22日星期日

Re: [fw-mvc] Centralized Stack/Broker or something for Models

I would think there is better performance by storing the data as a
class property. Otherwise, you open/close the same cache file every
time you access the cache from the front controller, controller,
model, etc...

On Sunday, August 22, 2010, Andreas Möller <localheinz@l8m.de> wrote:
>
>> I'm starting to run into this problem
>> quite frequently.
>>
>> 1. Fetch `user` row in a front controller
>> plugin.
>  > 2. Fetch the same `user` row in the
>> controller.
>>  3. Fetch the same `user` row during a Model call.
>
> Cache it.
>
>
> Best regards,
>
> Andreas
>
>

--
Michael DePetrillo
theman@michaeldepetrillo.com
Mobile: (858) 761-1605
www.michaeldepetrillo.com

Re: [fw-mvc] Centralized Stack/Broker or something for Models

> I'm starting to run into this problem
> quite frequently.
>
> 1. Fetch `user` row in a front controller
> plugin.
> 2. Fetch the same `user` row in the
> controller.
> 3. Fetch the same `user` row during a Model call.

Cache it.


Best regards,

Andreas

Re: [fw-mvc] Centralized Stack/Broker or something for Models

I'm starting to run into this problem quite frequently.  
  1. Fetch `user` row in a front controller plugin.
  2. Fetch the same `user` row in the controller.
  3. Fetch the same `user` row during a Model call. 
I have a couple different hacks to this problem.
  1. Store Zend_Db_Row in registry when you need to use it later.  
  2. Store Zend_Db_Row as a static property in the Zend_Db_Table class. 
  3. Write Row to cache.
I've created fetchRowRegistry() and fetchRowStatic() which use fetchRow() and fetchAll(). 

This works OK but it's difficult for other developers to remember and use these special methods.  I would also prefer a simpler approach.  

Michael DePetrillo
theman@michaeldepetrillo.com
Mobile: (858) 761-1605
www.michaeldepetrillo.com


On Sat, Aug 21, 2010 at 9:24 PM, Marko78 <marko.korhonen@gmail.com> wrote:
models here and there all over again during the request.
$contentModel = new Content_Model_Content; is been called in many places
during some request on my application. Only "design pattern" I use for
models is "fat models, thin controllers", so I try to write all operations
to my model instead of writing them to my controllers.

So I was thinking, am I wasting resources? Declaring the same class over and
over again in the same request. Should I have somekind of centralized stack
where to register my Models when needed and the next time the Model is
needed it could be get from this centralized stack?

RE: [fw-mvc] Re: Centralized Stack/Broker or something for Models

> At once I had my models as singletons.

Well, how much sense does that make?

> But then the wise men said that
singleton is evil. =)

It's not - it depends on whether you want to apply a design pattern for the mere application or whether you actually want to accomplish something - like, solve a problem. For example, your log and your mvc instance are singletons. Can't be that evil, then. Doesn't make much sense, though, to have singleton models, because in most cases you don't want something to happen just once, so should be able to juggle around with as many instances of a model class as you like, or some implemented logic allows for, but not just one.

> So I try not to use singletons though
> my current goal is basicly working
> similar as singeletons.

But what for? And why?

> But now I can any time make a new
> instance of my model if needed...

And what's up with that?

> Butbasicly I'm using
> my stack of models (array) in my
> application bootstrap.

And what for?


Best regards,

Andreas

RE: [fw-mvc] Re: Centralized Stack/Broker or something for Models

> And my models are NOT DbTable objects.
> So Model > DbTable -> Row and so
on...
> or some other datasource e.g. Model
> ACL or Model > Twitter account or
> Model > XML-file...

I'm just curious what you need a centralized broker for when your aim is to handle scarce resources wisely, and would like to know what the broker should do, then.


Best regards,

Andreas

Re: [fw-mvc] Re: Centralized Stack/Broker or something for Models

You should try implementing a service layer.

What I do in my ZF web apps, is have at least one application service
for each module. If you want to use a model, or really interact with
the domain layer, then you must go through this application service.
This service serves as the API for your module. Also, it is a great
place for caching, logging, etc.

On 8/22/2010 7:28 AM, Marko78 wrote:
> Hi Dolf,
>
> At once I had my models as singletons. But then the wise men said that
> singleton is evil. =)
> So I try not to use singletons though my current goal is basicly working
> similar as singeletons.
>
> But now I can any time make a new instance of my model if needed... But
> basicly I'm using
> my stack of models (array) in my application bootstrap.
>
> br, Marko
>
>
> -----
> br,
> --------------------------
> Marko Korhonen
> Technical Consultant