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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}