Extras:VirtualEntityService
<< Back to Dashboard | << Extras Viewer
|
Virtual Entity Service
( Base ORM Services | ORM Event Handling | ColdBox Criteria Builder )
Overview
The virtual entity service is another support class that can help you create virtual service layers that are bounded to a specific ORM entity for convenience. This class inherits from our Base ORM Service and allows you to do everything the base class provides, except you do not need to specify to which entityName you are working with. You can also use this class as a base class and template out its methods to more concrete usages.
The idea behind this virtual entity service layer is to allow you to have a very nice abstraction to all the CF ORM capabilities (hibernate) and promote best practices.

Usage
Please remember that you can use ANY method found in the Base ORM Service except that you will not pass an argument of entityName anymore as you have now bounded to that specific entity.
Base Properties
There are a few properties you can instantiate the virtual entity service with or set them afterward. Below you can see a nice chart for them:
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
| entityName | string | true | --- | The entity name you want to bound this virtual service to |
| queryCacheRegion | string | false | ORMService.defaultCache | The name of the secondary cache region to use when doing queries via this base service |
| useQueryCaching | boolean | false | false | To enable the caching of queries used by this base service |
| useTransactions | boolean | false | true | Enables hibernate safe transactions around all operations that either save, delete or update ORM entities |
Direct Instantiation
So if I was to instantiate a virtual service I can do the following:
import coldbox.system.orm.hibernate.* userService = new VirtualEntityService("User"); userService = new VirtualEntityService("User",true,"user.service.cache"); usersFound = userService.count(); user = userService.new(firstName="Luis",lastName="Majano",Awesome=true); userService.save( user ); user = userService.get("123");
Templating
I can also create my own service layer object in my model folder that inherits from the virtual service layer. This is our preferred approach so you can modify and template out any methods or add methods of your own. Below is a sample templated service layer:
/** * Service to handle author operations. */ component extends="coldbox.system.orm.hibernate.VirtualEntityService" accessors="true" singleton{ // User hashing type property name="hashType"; /** * Constructor */ AuthorService function init(){ // init it super.init(entityName="bbAuthor", useQueryCaching=true,queryCacheRegion="myapp.authors"); setHashType( "SHA-256" ); return this; } /** * Author search by name, email or username */ function search(criteria){ var params = {criteria="%#arguments.criteria#%"}; var r = executeQuery(query="from bbAuthor where firstName like :criteria OR lastName like :criteria OR email like :criteria",params=params,asQuery=false); return r; } }
Autowiring
I can also autowire virtual entity services in any handler, plugin, interceptor or even other model objects by using the entityService namespace injection DSL. What this does is that it will create a new virtual entity service layer and bind it to a specific entity for you. Thus, giving you a virtual service layer.
component{
// annotation based injections
property name="userService" inject="entityService:User";
property name="commentService" inject="entityService:Comment";
/**
* Good 'ol setter injection
* @inject entityService:Post
*/
setPostService( postService ){
instance.postService = arguments.postService;
}
}
If you will be templating out your own concrete virtual entity service layers, then you would inject them using standard model injection.
More From The Community
- Using the BaseORMService and VirtualEntityService Extras by Curt Gratz [Sep 12, 2010]
Related ORM Services

SideBar
User Login 




Comments (