So you want to: Use Taffy's built in Dependency Injection to resolve dependencies of your resources

atuttle edited this page Nov 19, 2014 · 6 revisions
Clone this wiki locally

Taffy comes with a lightweight dependency-injection class, simply referred to as its "factory". The default configuration of Taffy uses this factory to enable the use of the /resources folder convention.

/resources Folder Convention

When you create a Taffy API, you have two required files: Application.cfc and index.cfm; and the option of using the /resources folder convention. If you create a folder in the root of your API folder (does not necessarily need to be in the web-root of the domain) named "resources", Taffy looks for Taffy Resource CFC's in it; as well as any dependencies that the resources may have.

A Resource CFC is simply one which extends "taffy.core.resource" and has a "taffy:uri" (or in the case of script-components, "taffy_uri") attribute defining the URI pattern to which the resource will respond.

Dependency Resolution

Dependencies in Taffy Resources are defined by the existence of a setter method or property. If you want Taffy to inject an instance of /resources/Config.cfc, simply create a setter method for it like so:

function setConfig(configObj){
    variables.config = arguments.configObj;

The setter's argument name does not matter, and what you choose to do with the CFC instance passed to it is up to you; but the setter method-name is important. It must begin with "set", and whatever comes after "set" will be the name of the CFC (sans file extension). So in this case, "setConfig" looks for "config.cfc".

As of Taffy 1.3, you may also use a property instead of a setter. Values set by/into properties are available in the this scope (whereas with the setter you explicitly decided whether to save the value into this or variables). Here's an example resource CFC that uses a property to inject the config object and then uses it:

component extends="taffy.core.resource" taffy_uri="/foo"{

    property config;

    function get(){
        return representationOf( this.config.defaultValue );


Except for Serializers, which are treated as transients, all resources and other classes in the resources folder are treated as singletons for the purpose of dependency injection.

Taffy's Factory will not look anywhere other than the resources folder. It does look recursively into subfolders as well. There is no configuration for the factory -- it assumes and requires the use of a /resources folder or mapping.

A Note on Managing Serializers with your Bean Factory

If you choose to manage it with ColdSpring or another Dependency Injection framework, you need to ensure that your Serializer is configured as a transient. If you don't, you may experience thread-safety issues.