Andrew Clarke

systems architect, internet developer, team leader

Andrew Clarke

Factory

The factory object pulls and returns objects to the code.  An instance of this object will sit in application.factory.  Objects can either be singletons or not.  Singletons are cached in the factory object, which means that all users in the application share one instance of the object.  The factory will return a new instance of non-singletons every time a non-singleton object is requested.

You should know a couple things about the factory:

  1. How to change the default locations of objects if you want to create a subclass (i.e. create your own MetaData object), and
  2. How to set the location of your new Factory object if you need to subclass it to perform #1 above.

Change the default location of objects

In Factory.cfc, you'll see a line like this:

variables.s_objectMap.MetaData.location = "ca.clarke.cfscaffold.MetaData";

This tells the factory that when a "MetaData" object is requested, pull one from ca.clarke.cfscaffold.MetaData.  Let's say you've subclassed MetaData and want CFScaffold to pick up your version rather than the default version.  You'd change the line above to something like:

variables.s_objectMap.MetaData.location = "com.example.MetaData";

Re-initialize the Factory object in your application scope, and after that, the MetaData object returned will be your version rather than the default version.

Give CFScaffold the location of your Factory object

If you need to modify Factory.cfc, you'll probably want to create your own subclass of Factory.cfc too.  Let's say you've done this and have put it in your directory mapped to com.example as well.  As is explained on the settings.ini page, set mapping_factory_custom=com.example. (note the trailing dot) in settings.ini.  This tells the code in index.cfm (or wherever you've put it) to use com.example.Factory instead of ca.clarke.cfscaffold.Factory.

 

2 Comments

2 responses so far ↓

Leave a Comment

Leave this field empty