Kevin,

I have a few things for you to think about.  I believe your confusion begins
with the fact that XMaintainance has a validate method taking in the XForm.
Your assumption is correct that you should not "mingle" the XForm with
XMaintainance, this would break MVC.  Generally in Struts, forms act as Data
Transfer Objects.  You load them up by calling accessors to the model and
pass them to the view, or you load input from the view into the form and
then call appropriate mutators on the model using the forms values.

Generally, a form should only validate user input.  An example of "user
input" could be data from a form on a web page.  The forms validate method
should only perform simple input validations such as; make sure the field is
not empty or that it has enough characters etc.  This simple validation can
be done easily with the Struts Validator Extension.  I won't focus on simple
validation in this email.  

Business logic validations should be delegated to the model.  An example of
a business logic validation might be something like, does this user have
permission to do this task?

Two snippets to consider:

This code would be in your Edit action
-------------------------------------------
// Load your form with info from model and
// forward to view.

XForm xf = (XForm)form;

// If the data is coming from your model,
// you can assume that it is valid data.
xf.setX( XMaintainance.getX );

mapping.findForward("show"); 
-------------------------------------------


This code would be in your Save action
-------------------------------------------
// Load your form with info from the view,
// (Struts does this for you), then call
// mutators on you model and verify that the
// input passes business validation.

XForm xf = (XForm)form;

try {  
  XMaintainance.setX( xf.getX );
} catch (BusinessLogicException e) {
  errors.add("error", 
                 new ActionError("Xerrors"));
}

// After this you would need to save errors
// and forward depending on what happened.
-------------------------------------------
  
I guess my point is that forms should be used as DTO's inside the Struts
framework itself.  Don't let them stray into your model (or facade if you
use one).  Define strict Interfaces into you model, generally these
interfaces will have method signatures that take Strings or other basic
objects.  I hope this helped some.

Chad Westfall
Software Engineer
Deere & Company, Inc.



-----Original Message-----
From: Kevin HaleBoyes [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 20, 2002 9:05 AM
To: [EMAIL PROTECTED]
Subject: [MVC-Programmers] How best to split the layers of MVC


I have a struts application modelled after the struts-example.
It has
   XForm.java as the form bean,
   x.jsp to display the form, and
   EditXAction.java and SaveXAction.java to process the actions.

The Edit and Save actions need to use an XMaintainance class as
it provides the knowledge of the business logic.

In SaveXAction I've checked to make sure the request wasn't cancelled
and checked the transaction token (normal struts stuff).  I need to
do additional validations when an XForm is being created so I've
added a validate() method to XMaintainance.

So the SaveXAction code does something like the following:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

and later check to see if there have been any errors, and if so,
saveErrors() and saveToken() are called and we forward back to the
mapping.getInput().

That works great!

My question is:

XForm is considered to be a View component.
SaveXAction is considered to be a Controller component.
XMaintainance is a Model component.

By passing the XForm to XMaintainance haven't I mingled the two
components when there was no need?  Mingled is the wrong word.

Instead,
SaveXAction could mediate the interaction between the view and
the model by copying values from the view into the controller:

    xm.setXFormField1( xf.getField1() );
    ...
    if ( ! xm.validate() ) {
        errors.add("error", new ActionError("Xerrors"));
    }


Performance wise it would be wasteful to copy the values from
XForm to XMaintainance.  But isn't one of the goal of MVC to
separate the Model from the View by way of the Controller?

Where do we make the split between the three components?  

For instance,
how do report errors from XMaintainance?  I could change the
code above to be:

    ActionErrors errors = new ActionErrors();
    XForm xf = (XForm)form;
    XMaintainance xm = new XMaintainance();
    if ( ! xm.validate(xf, errors) ) {
        errors.add("error", new ActionError("Xerrors"));
    }

But now I've mingled a core Controller element (ActionError) to
the Business logic layer.  If I don't do this though, I'll have to
use something similar to ActionErrors that XMaintainance knows about.
SaveXAction would have to copy these errors to the Struts
ActionErrors.  It seems like a lot of copying is being done in order
to separate the components.

Does any of this make any sense?  I guess I'm looking for advice,
on how to split the three MVC components and pass information
between the layers.

Thanks,
K.



__________________________________________________
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
http://finance.yahoo.com
_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.netbean.net/mailman/listinfo/mvc-programmers

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.netbean.net/mailman/listinfo/mvc-programmers

Reply via email to