[coldbox-3.8.1] Inject/Autowire Virtual Entity Service and configure useQueryCaching at same time?

If I don’t need to create a special UserService.cfc because I think using the BaseORMService is good enough (via VirtualEntityService) is there something I could do here to enable useQueryCaching:
component{ // Virtual service layer based on the User entity property name="userService" inject="entityService:User"; }

I saw direct instantiation, but was trying to use dependency injection:

userService = new VirtualEntityService(entityName="User",useQueryCaching=true);

Trying to avoid this if not needed:
UserService.cfc
`
component extends=“coldbox.system.orm.hibernate.VirtualEntityService” singleton {
/**

  • Constructor
    */
    public CommentService function init(){
    super.init(entityName=“User”,useQueryCaching=“true”);
    return this;
    }
    }
    `

I’ve been just reading the documentation found here. Any helpful thoughts are appreciated – thanks!

Wes

Can you just turn query caching on in your inDIComplete()?

component{
// Virtual service layer based on the User entity
property name=“userService” inject=“entityService:User”;

function onDIComplete() {
userService.setUseQueryCaching( true );
}

}

If you are going to be injecting this virtual entity service all over your app, you could create an explicit mapping in /config/WireBox.cfc that configures the constructor args.

map( ‘userService’ )
.to( ‘coldbox.system.orm.hibernate.VirtualEntityService’ )
.asSingleton()
.initWith( {
entityname = ‘User’,
useQueryCaching = true
} );

Then just inject “userService” like a normal model:

property name=“userService” inject;

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Dude. Thank you for that! I just learned two new things right there.

Wes

I got this working using the following In my Wirebox.cfc:

map( 'userService' ) .to( 'coldbox.system.orm.hibernate.VirtualEntityService' ) .asSingleton() .initWith( entityname = 'User', useQueryCaching = true );

I removed the curly braces within the .initWith() method which caused an error: The ENTITYNAME argument passed to the init function is not of type string.

Follow-up question Different scenario now but still related to VirtualEntityService specifically.

Let’s say I do not go the WireBox.cfc mapping route and do create a specific UserService.cfc file which extends VirtualEntityService because I want to create some extra methods, like my getCoolUsers() below :

UserService.cfc
`
component extends=“coldbox.system.orm.hibernate.VirtualEntityService” singleton {

// Dependency Injection
property name=“userService” inject=“entityService:User”;

/**

  • Constructor
    */
    UserService function init(){
    super.init(entityName=“User”, useQueryCaching=true);
    return this;
    }

/**

  • Get the cool users, using the VirtualEntityService injected above
    */
    function getCoolUsers() {
    // Not cached because the userService instance used here was not configured with useQueryCaching?
    userService.list(criteria={ isCool=true }, sortOrder=“lastName asc”, asQuery=false);
    }
    }
    `

If from another service or handler I inject the above UserService.cfc

SomeHandler.cfc

`
component {

// Dependency Injection
property name=“userService” inject=“UserService”;

/**

  • Show all users
    */
    function allUsers() {
    // Ultimately calls a method provided by the BaseORMService
    prc.allUsers = userService.list();
    }

/**

  • Show the cool users
    */
    function coolUsers() {
    // Ultimately calls a custom method
    prc.coolUsers = userService.getCoolUsers();
    }
    }
    `

Is the following true?

  1. The allUsers() action above will result in a cached query.

  2. The coolUsers() action above will NOT result in a cached query…unless:

  • If I wanted to have getCoolUsers() or any other custom methods cached when called then I’d need to add to UserService.cfc:

    function onDIComplete() { userService.setUseQueryCaching( true ); }

Thanks!

Wes