Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

FW/1, WireBox and ValidateThis

DZone's Guide to

FW/1, WireBox and ValidateThis

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

I've been trying out two new frameworks today, FW/1 and WireBox, along with ValidateThis.

FW/1 is a really nice lightweight MVC framework and WireBox is a Dependancy Injection framework. Getting FW/1 has a handy setBeanFactory method which allows you to use any BeanFactory that responds to containsBean and getBean calls. WireBox doesn't have these methods, but it does ship with an adapter which lets you expose those methods. These adapters are primary for replacing LightWire or ColdSpring with WireBox, but as this is a new applicaiton I'm building, I'm using the bundled wirebox.system.ioc.adapters.WireBoxAdapter. In my Application.cfc I use the setupApplication() method of FW/1 to load and configure WireBox.

// executes when application starts
function setupApplication()
{
  var beanFactory = new wirebox.system.ioc.adapters.WireBoxAdapter( definitionFile="model.config.binder" );
  beanFactory.createFactory();
  setBeanFactory( beanFactory );
}

You may notice that I'm passing the definitionFile argument into the WireBoxAdapter. This is the dotted path to my binder, which is where I setup my relationships and configure ValidateThis. My binder looks like:

component extends="wirebox.system.ioc.config.Binder" 
{
  function configure()
  {
    // map all cfcs in this directory and subdirectories
    mapDirectory( "model" );
    // define and configure validatethis
    mapPath("validatethis.ValidateThis").asSingleton().initWith(
      {
        JSIncludes = "false"
      }
    );
  }
}

I can now have ValidateThis injected into my controller by simply creating a setValidateThis method like so:

component
{
  variables.fw = '';
  
  /* CONSTRUCTOR
  ------------------------------------------------------------------------------ */
  function init( required any fw )
  {
    variables.fw = arguments.fw;
  }
  
  /* EXPLICIT
  ------------------------------------------------------------------------------ */
  function dosave( required struct rc )
  {
    rc.foo = EntityNew( "Foo" );
    variables.fw.populate( rc.foo );
    rc.validationresult = variables.validatethis.validate( foo );
    if ( rc.validationresult.hasErrors() )
    {
      variables.fw.redirect('foo.maintain','validationresult,foo');
    }
    else
    {
      transaction
      {
        EntitySave( foo );
      }
      rc.message = ["foo saved"];
      variables.fw.redirect('foo.bar','message');
    }
  }

  /* DI
  ------------------------------------------------------------------------------ */
  function setValidateThis( ValidateThis )
  {
    variables.ValidateThis = arguments.ValidateThis;
  }
  
}

It took me a while to figure this out as the docs, but as you can see it's pretty simple and a very powerful combination.

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}