WireBox

CBWIRE seamlessly integrates with WireBox, ColdBox's powerful dependency injection framework, allowing you to inject services, models, and other dependencies directly into your components. This separation of concerns keeps business logic out of your components and promotes clean, maintainable code architecture.

Basic Dependency Injection

The most common way to access dependencies is through property injection using the inject attribute:

// models/UserService.bx
class singleton {
    function getAll() {
        return queryExecute("select id, name, email from users");
    }
    
    function create(userData) {
        return queryExecute(
            "insert into users (name, email) values (:name, :email)",
            {
                name: {value: userData.name, cfsqltype: "cf_sql_varchar"},
                email: {value: userData.email, cfsqltype: "cf_sql_varchar"}
            }
        );
    }
    
    function delete(userId) {
        queryExecute(
            "delete from users where id = :id", 
            {id: {value: userId, cfsqltype: "cf_sql_integer"}}
        );
    }
}

Dynamic Instance Retrieval

For cases where you need to retrieve dependencies dynamically within action methods, use the getInstance() method:

Common Injection Patterns

Service Layer Injection

LogBox Logger Injection

ColdBox Settings Injection

Custom Provider Injection

getInstance() Method Signature

The getInstance() method provides flexible dependency retrieval:

Usage Examples

Lifecycle Integration

CBWIRE components fully participate in WireBox's lifecycle management. All WireBox lifecycle methods are available:

Benefits of WireBox Integration

  • Separation of Concerns: Keep business logic in dedicated service layers

  • Testability: Easily mock dependencies for unit testing

  • Flexibility: Switch implementations via WireBox mappings

  • Performance: Leverage WireBox's singleton and caching capabilities

  • Configuration: Externalize configuration through WireBox DSL

CBWIRE uses WireBox internally to instantiate your components, so all WireBox lifecycle methods and features are automatically available to your components.

Further Documentation

Learn about the full capabilities of WireBox at https://wirebox.ortusbooks.com/.

Was this helpful?