Category Selected:

tutorials

Blog

Importance of Var Scoping

Luis Majano |  September 24, 2010
I can honestly say that there are always requests on the ColdBox lists or work that say, "Hey the application is taking my server memory, why does ColdBox do that!".  First of all, the ColdBox platform when placed in memory is less than 1kb of memory.  The rest that gets loaded into an application is because of your application code.  Second, the biggest culprit is the misuse or the not using of var scoped variables in event handlers or doma...
Read More

Using the BaseORMService and VirtualEntityService Extras

Curt Gratz |  August 25, 2010

I thought I would take a moment and explain some of the different ways you can use the BaseORMService and VirtualEntityService extras that are included in Coldbox 3.0 M5 and above.

Before I get into the different uses for each, lets define them both and hopefully that will help clarify the differences

Base ORM Service:

The BaseORMService is an amazing service layer tool that can be used in any project. The idea behind this support class is to provide a very good base service layer that can interact with hibernate and entities inspired by Spring's Hibernate Template support. It provides tons of methods for query executions, paging, transactions, session metadata, caching and much more. You can either use the class on its own or create more concrete service layers by inheriting from this class. We also have a virtual service layer that can be mapped to specific entities and create entity driven service layers virtually. ColdBox also offers several integrations to this class via plugins and the autowire DSL.

Virtual Entity Service:

The virtual entity service is another support class that can help you create virtual service layers that are bounded to a specific 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 specific 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.

BaseORMService/ORMService Plugin

So first, how might you use the Base ORM Service extra

First, you could use it to create a concrete service that has all the BaseORMService methods in it.  For example. UserService.cfc might look like this

import coldbox.system.orm.hibernate.*

component extends="BaseORMService"{

 

  public UserService function init(){

      super.init(useQueryCaching=true);

      return this;       

  }

 

}

 

Then you can call things like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

Or any of the other available methods in the BaseORMSerivce.  Now with our UserService, we are free to add any of our own business logic to that service that is needed, but have a ton of really cool methods already available to us.

Another way to use the BaseORMService, and I think a simpler way if via the ORMService Plugin.  This plugin gives you access to all the methods in the BaseORMService.

Mainly, I don’t directly use the BaseORMService cfc.  I either create services based of the VirtualEntityService, via the AutowireDSL (which I will explain shortly), or use these features via the ORMService Plugin.

To use the ORMService plugin, we can modify our code above slightly and are ready to go.

function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = getPlugin("ORMService").list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

I find myself using the ORMService Plugin extensively, so I also always autowire it to my handler, making the code above even simpler.

// A handler
component{
  property name="ORMService" inject="coldbox:plugin:ORMService";

  function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = ORMService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }
}

 

How easy is that.   Tons of functionality right at your fingertips.

VirtualEntityService /Autowire Entity DSL

Generally when creating a concrete service I want to base off of the BaseORMService, I extend the VirtualEntityService and initialize it using the name of the entity that the service concerns. 

Basically, the VirtualEntityService inherits from the BaseORMService and allows you to template out its methods to a specific entity for more concrete uses.  This eliminates the need to type entityName=”someEntity” in your calls, and anything that saves me typing, saves me time and is a GOOD thing.

So, if I was creating that same UserService from above using the VirtualEntityService, it would look like this.

import coldbox.system.orm.hibernate.*;
component extends="VirtualEntityService"{

  public any function init(){
      super.init( "User", true, "user.query.cache" );
      return this;
  }

  // Override or add methods as you see fit now.
}

 

Pretty cool huh?  Now my list function looks like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService .list(sortBy="fname",offset=event.getValue("startrow",1),max=20);
event.setView("user/list"); }

So, removing the entityName=”User” from the call, but if you had to call the service even 10 times, that’s a lot of typing saved.

 

Autowiring

There is also an autowiring DSL to quickly create services based on a specific entity that you can use if don’t need any additional methods or need to override any methods in your service layer.

Per the documentation

Type

Description

entityService

Inject a BaseORMService object for usage as a generic service layer

entityService:{entity}

Inject a VirtualEntityService object for usage as a service layer based off the name of the entity passed in.

 

// Generic ORM service layer
property name="genericService" inject="entityService";
// Virtual service layer based on the User entity
property name="userService" inject="entityService:User";

 

So, to implement our UserService and list function we have had in our examples we could have a handler like this

// A handler
component{
  property name="UserService" inject="entityService:User";
function list(event){ var rc = event.getCollection(); //get a listing of all users with paging rc.users = UserService.list(sortBy="fname",offset=event.getValue("startrow",1),max=20); event.setView("user/list"); } }

 

For more information on the BaseORMService and VirtualEntityService, here are some links to the complete documentation.

http://wiki.coldbox.org/wiki/Extras:BaseORMService.cfm

http://wiki.coldbox.org/wiki/Extras:VirtualEntityService.cfm

Hopefully this post helps you understand how you can use these AWESOME extras in your environment.


 

Read More

Using the BaseORMService and VirtualEntityService Extras

Curt Gratz |  August 25, 2010

I thought I would take a moment and explain some of the different ways you can use the BaseORMService and VirtualEntityService extras that are included in Coldbox 3.0 M5 and above.

Before I get into the different uses for each, lets define them both and hopefully that will help clarify the differences

Base ORM Service:

The BaseORMService is an amazing service layer tool that can be used in any project. The idea behind this support class is to provide a very good base service layer that can interact with hibernate and entities inspired by Spring's Hibernate Template support. It provides tons of methods for query executions, paging, transactions, session metadata, caching and much more. You can either use the class on its own or create more concrete service layers by inheriting from this class. We also have a virtual service layer that can be mapped to specific entities and create entity driven service layers virtually. ColdBox also offers several integrations to this class via plugins and the autowire DSL.

Virtual Entity Service:

The virtual entity service is another support class that can help you create virtual service layers that are bounded to a specific 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 specific 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.

BaseORMService/ORMService Plugin

So first, how might you use the Base ORM Service extra

First, you could use it to create a concrete service that has all the BaseORMService methods in it.  For example. UserService.cfc might look like this

import coldbox.system.orm.hibernate.*

component extends="BaseORMService"{

 

  public UserService function init(){

      super.init(useQueryCaching=true);

      return this;       

  }

 

}

 

Then you can call things like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

Or any of the other available methods in the BaseORMSerivce.  Now with our UserService, we are free to add any of our own business logic to that service that is needed, but have a ton of really cool methods already available to us.

Another way to use the BaseORMService, and I think a simpler way if via the ORMService Plugin.  This plugin gives you access to all the methods in the BaseORMService.

Mainly, I don’t directly use the BaseORMService cfc.  I either create services based of the VirtualEntityService, via the AutowireDSL (which I will explain shortly), or use these features via the ORMService Plugin.

To use the ORMService plugin, we can modify our code above slightly and are ready to go.

function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = getPlugin("ORMService").list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

I find myself using the ORMService Plugin extensively, so I also always autowire it to my handler, making the code above even simpler.

// A handler
component{
  property name="ORMService" inject="coldbox:plugin:ORMService";

  function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = ORMService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }
}

 

How easy is that.   Tons of functionality right at your fingertips.

VirtualEntityService /Autowire Entity DSL

Generally when creating a concrete service I want to base off of the BaseORMService, I extend the VirtualEntityService and initialize it using the name of the entity that the service concerns. 

Basically, the VirtualEntityService inherits from the BaseORMService and allows you to template out its methods to a specific entity for more concrete uses.  This eliminates the need to type entityName=”someEntity” in your calls, and anything that saves me typing, saves me time and is a GOOD thing.

So, if I was creating that same UserService from above using the VirtualEntityService, it would look like this.

import coldbox.system.orm.hibernate.*;
component extends="VirtualEntityService"{

  public any function init(){
      super.init( "User", true, "user.query.cache" );
      return this;
  }

  // Override or add methods as you see fit now.
}

 

Pretty cool huh?  Now my list function looks like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService .list(sortBy="fname",offset=event.getValue("startrow",1),max=20);
event.setView("user/list"); }

So, removing the entityName=”User” from the call, but if you had to call the service even 10 times, that’s a lot of typing saved.

 

Autowiring

There is also an autowiring DSL to quickly create services based on a specific entity that you can use if don’t need any additional methods or need to override any methods in your service layer.

Per the documentation

Type

Description

entityService

Inject a BaseORMService object for usage as a generic service layer

entityService:{entity}

Inject a VirtualEntityService object for usage as a service layer based off the name of the entity passed in.

 

// Generic ORM service layer
property name="genericService" inject="entityService";
// Virtual service layer based on the User entity
property name="userService" inject="entityService:User";

 

So, to implement our UserService and list function we have had in our examples we could have a handler like this

// A handler
component{
  property name="UserService" inject="entityService:User";
function list(event){ var rc = event.getCollection(); //get a listing of all users with paging rc.users = UserService.list(sortBy="fname",offset=event.getValue("startrow",1),max=20); event.setView("user/list"); } }

 

For more information on the BaseORMService and VirtualEntityService, here are some links to the complete documentation.

http://wiki.coldbox.org/wiki/Extras:BaseORMService.cfm

http://wiki.coldbox.org/wiki/Extras:VirtualEntityService.cfm

Hopefully this post helps you understand how you can use these AWESOME extras in your environment.


 

Read More

Using the BaseORMService and VirtualEntityService Extras

Curt Gratz |  August 25, 2010

I thought I would take a moment and explain some of the different ways you can use the BaseORMService and VirtualEntityService extras that are included in Coldbox 3.0 M5 and above.

Before I get into the different uses for each, lets define them both and hopefully that will help clarify the differences

Base ORM Service:

The BaseORMService is an amazing service layer tool that can be used in any project. The idea behind this support class is to provide a very good base service layer that can interact with hibernate and entities inspired by Spring's Hibernate Template support. It provides tons of methods for query executions, paging, transactions, session metadata, caching and much more. You can either use the class on its own or create more concrete service layers by inheriting from this class. We also have a virtual service layer that can be mapped to specific entities and create entity driven service layers virtually. ColdBox also offers several integrations to this class via plugins and the autowire DSL.

Virtual Entity Service:

The virtual entity service is another support class that can help you create virtual service layers that are bounded to a specific 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 specific 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.

BaseORMService/ORMService Plugin

So first, how might you use the Base ORM Service extra

First, you could use it to create a concrete service that has all the BaseORMService methods in it.  For example. UserService.cfc might look like this

import coldbox.system.orm.hibernate.*

component extends="BaseORMService"{

 

  public UserService function init(){

      super.init(useQueryCaching=true);

      return this;       

  }

 

}

 

Then you can call things like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

Or any of the other available methods in the BaseORMSerivce.  Now with our UserService, we are free to add any of our own business logic to that service that is needed, but have a ton of really cool methods already available to us.

Another way to use the BaseORMService, and I think a simpler way if via the ORMService Plugin.  This plugin gives you access to all the methods in the BaseORMService.

Mainly, I don’t directly use the BaseORMService cfc.  I either create services based of the VirtualEntityService, via the AutowireDSL (which I will explain shortly), or use these features via the ORMService Plugin.

To use the ORMService plugin, we can modify our code above slightly and are ready to go.

function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = getPlugin("ORMService").list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }

 

I find myself using the ORMService Plugin extensively, so I also always autowire it to my handler, making the code above even simpler.

// A handler
component{
  property name="ORMService" inject="coldbox:plugin:ORMService";

  function list(event){
    var rc = event.getCollection();

    //get a listing of all users with paging
    rc.users = ORMService.list(entityName="User",sortBy="fname",offset=event.getValue("startrow",1),max=20);

    event.setView("user/list");
  }
}

 

How easy is that.   Tons of functionality right at your fingertips.

VirtualEntityService /Autowire Entity DSL

Generally when creating a concrete service I want to base off of the BaseORMService, I extend the VirtualEntityService and initialize it using the name of the entity that the service concerns. 

Basically, the VirtualEntityService inherits from the BaseORMService and allows you to template out its methods to a specific entity for more concrete uses.  This eliminates the need to type entityName=”someEntity” in your calls, and anything that saves me typing, saves me time and is a GOOD thing.

So, if I was creating that same UserService from above using the VirtualEntityService, it would look like this.

import coldbox.system.orm.hibernate.*;
component extends="VirtualEntityService"{

  public any function init(){
      super.init( "User", true, "user.query.cache" );
      return this;
  }

  // Override or add methods as you see fit now.
}

 

Pretty cool huh?  Now my list function looks like this

function list(event){
    var rc = event.getCollection();
    var UserService = getModel("UserService");

    //get a listing of all users with paging
    rc.users = UserService .list(sortBy="fname",offset=event.getValue("startrow",1),max=20);
event.setView("user/list"); }

So, removing the entityName=”User” from the call, but if you had to call the service even 10 times, that’s a lot of typing saved.

 

Autowiring

There is also an autowiring DSL to quickly create services based on a specific entity that you can use if don’t need any additional methods or need to override any methods in your service layer.

Per the documentation

Type

Description

entityService

Inject a BaseORMService object for usage as a generic service layer

entityService:{entity}

Inject a VirtualEntityService object for usage as a service layer based off the name of the entity passed in.

 

// Generic ORM service layer
property name="genericService" inject="entityService";
// Virtual service layer based on the User entity
property name="userService" inject="entityService:User";

 

So, to implement our UserService and list function we have had in our examples we could have a handler like this

// A handler
component{
  property name="UserService" inject="entityService:User";
function list(event){ var rc = event.getCollection(); //get a listing of all users with paging rc.users = UserService.list(sortBy="fname",offset=event.getValue("startrow",1),max=20); event.setView("user/list"); } }

 

For more information on the BaseORMService and VirtualEntityService, here are some links to the complete documentation.

http://wiki.coldbox.org/wiki/Extras:BaseORMService.cfm

http://wiki.coldbox.org/wiki/Extras:VirtualEntityService.cfm

Hopefully this post helps you understand how you can use these AWESOME extras in your environment.


 

Read More

ColdBox ORM service changes from M5 to M6

Luis Majano |  August 12, 2010

There have been several changes from the ORM support classes from M5 to M6 that I will recap here thanks to muji in our google group and also to help out in the transition.  Our ORM support services are maturing every day and moving to a great solid release for 3.0.0 Final.  So let’s recap some of the changes:

 

  • Entity dependency injection are now controller and configured via the Autowire interceptor and not the ORM event handler anymore.  However, the ORM event hand...
Read More

ColdBox ORM service changes from M5 to M6

Luis Majano |  August 12, 2010

There have been several changes from the ORM support classes from M5 to M6 that I will recap here thanks to muji in our google group and also to help out in the transition.  Our ORM support services are maturing every day and moving to a great solid release for 3.0.0 Final.  So let’s recap some of the changes:

 

  • Entity dependency injection are now controller and configured via the Autowire interceptor and not the ORM event handler anymore.  However, the ORM event hand...
Read More

Combine/Merge ColdFusion arrays with Java Array addAll()

Curt Gratz |  July 21, 2010

Have you ever wanted a quick and easy way to combine arrays in ColdFusion.  I have done this a number of ways in the best, but came across this gem and thought I would share.

All ColdFusion arrays are of the Java vector type, we can use the underlying java.util.Vector class methods, specifically the addAll() method.

Keep in mind that this is not a "documented" use of arrays in ColdFusion, so at any version it may stop working.

Merging two arrays in ColdFusion with the java.util.vector.addAll()

ar1 = ["a","b","c"]; ar2 = ["d","e","f"]; ar1.addAll(ar2); writeDump(ar1);

 

Read More

Coldbox 3.0 and the CFC Config...

Curt Gratz |  June 29, 2010

One of my favorite new features of Coldbox 3.0 is the ability to have a CFC only config file. Thats right, no more XML. No don't get me wrong, XML has its place, but I love the ability to get funky with my config file. With it now being a CFC, I can do some crazy cool stuff.

For example

  1. I can now have my config files all extend a base config file or any other CFC, now thats cool.
  2. I can get creative with defining my enviromental variables, or create any other kind of helper functions.
  3. I can easily set a variable anywhere in the config file and EASILY refer to it.

These are just a few of the ways that I have already taken advantage of the Config.cfc.

What are some ways you have? Feel free to comment with any crazy or funky ideas you've used or plan to use with a fully ColdFusion-ifide (its a word, look it up) config file, I'd love to hear them.

Read More

ColdBox Plugins - A wealth of hidden treasure

Curt Gratz |  May 28, 2010

Today I was getting ready to send a file from my app to the browser for the user to download.  I was all set to use cfcontent to do this, but knowing ColdBox I thought I would do a quick check to see if the framework had any nice convenience methods for me to use to make life easier and sure enough... sendFile() in the core Utilities plugin.

getPlugin("Utilities").sendFile(file="test.txt");

So, this post is just a quick word of advice.  Don't reinvent the wheel.  If you are doing something, anything common, check the docs and see if ColdBox can do it for you.  You will be amazed at whats available in the core plugin set already for you to use.  

Plug it In

Plug IT IN

Here are just a few I use commonly.

ORMService - generic ORM Service helper for Hibernate
QueryHelper - all sorts of goodies in here, sorting queries is one of my favorite. Or the querySim.
Validator - all sorts of standard validation methods here. checkEmail and checkIPAddress are my favorites.
FileUtils - This is a utilities library that are file related. Lots of fun methods here.
Utilities - A good mix of a variety of handy methods, like todays gem sendFile()

 

Read More

Unit Testing with Mock Objects via MockBox

Luis Majano |  May 17, 2010

Once you get an appreciation for the importance of unit testing and integration testing is when we reach a new level in our development careers.  Testing is critical to mission critical applications, and even for our own little projects, where we test that our code should work as expected.  There’s that word again, expected.  Expectations in unit testing is like a nasty hamburger at a soccer match in El Salvador.  They go hand in hand :)

Read More