Hi Eric,
 
> class. What I'd like is to have my model (as in MVC) objects reuse the 
> process or maybe even server shared objects without doing any of these:
> 
> 1. Using a singleton utility class
> 2. Needing to pass objects to model objects' new() in teh controllers
> 3. Instantiating the objects in the model classes themselves

I'm not sure if this violates 3 (the models classes have to know what
resources they need, so i am not sure what wouldn't), but could you use
a singleton for the resource and a factory to access it? The model
classes call a "static" factory method that handles the configuration,
cache, etc...

This solves the problem of having configuration and resource allocation
code in your model objects. It does mean that you have to write factory
classes for your resources, but they ought to be quite simple. 

package MyModel::SomeObject;

...

sub doSomething {
  ...
  my $dbiHandle = MyDBIHandleFactory->getHandle();
  ...
}

1;

package MyDBIHandleFactory;

sub getHandle {
   if (defined $handle) {
        return $handle # implement as singleton
   }
   ... read config or something ...
   $handle = DBI->connect(...);
}

1;

hth, aaron


Reply via email to