== Writing Decorators == A 'decorator' is used when you wish to write your own CFC to be used to represent data, in place of the Transfer generated Object. An example of this being to write a User.cfc to represent a User record within a database. The 'decorator' that has been written wraps around the TransferObject that is generated by Transfer and automagically extends any public method generated on the decorated TransferObject. === Authoring a Decorator === Decorators are set using the 'decorator' attribute on the 'object' element in the [[Transfer Configuration File]] Decorators must extend [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html transfer.com.TransferDecorator] === Creating your own methods === You can create your own methods inside a decorator, which can reference the automagically generated methods (or not). For example, if you wanted to return the full name of a user, you could write: Where 'firstname' and 'lastname' are properties on the User definition. === Overwriting methods === If a method exists in the Decorator, it will overwrite the method of the generated [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferObject.html TransferObject]'s method. If you wish to have access to the TransferObject stored within the decorator, and any of its original generated methods, you can use [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html#getTransferObject() getTransferObject()] to return the original TransferObject An example of an overwritten method that utilises these methods, is if a User TransferObject has a 'setHomePageURL()' method generated, that you wanted to ensure had 'http://' at the beginning, you could put in your User.cfc decorator
This overwrites the generated setHomePageURL() method, and extends its functionality. === Access to Transfer === The method of [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html#getTransfer() getTransfer()] is available to the Decorator CFC, and gives access to the [http://docs.transfer-orm.com/html/transferapi/transfer/com/Transfer.html transfer.com.Transfer] CFC. === Access to the Datasource === The method [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html#getDatasource() getDatasource()] is available to the Decorator CFC, and gives access to the [http://docs.transfer-orm.com/html/transferapi//transfer/com/sql/Datasource.html transfer.com.sql.Datasource] CFC. === Access to the Transaction === The method [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html#getTransaction() getTransaction()] is available to the Decorator CFC, and gives access to the [http://docs.transfer-orm.com/html/transferapi//transfer/com/sql/transaction/Transaction.html transfer.com.sql.transaction.Transaction] CFC. It is advised that you use the [http://docs.transfer-orm.com/html/transferapi//transfer/com/sql/transaction/Transaction.html#execute() execute()] method in the Transaction CFC to execute Transfer based Transactions, as the AOP [http://docs.transfer-orm.com/html/transferapi//transfer/com/sql/transaction/Transaction.html#advise() advise()] methods are tailored more towards singletons. === Setting up values in the Decorator === [http://docs.transfer-orm.com/html/transferapi/transfer/com/TransferDecorator.html#configure() configure()] on the TransferDecorator will be run when the object is first created. You can overwrite this when you implement your own Decorator CFC, and is a perfect place to set default values for your Transfer Object. It should be noted that configure() is run ''before'' the object is populated, and therefore, any values it sets will be overwritten by those retrieved from the database. For example, this will set the 'name' property in this TransferObject to 'foo' when it is first initialised: [[Category:Decorators]]