You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Manish <pr...@gmx.net> on 2004/10/20 06:46:08 UTC

Conditional display - Approach question

This is not a strictly Velocity question, but putting it here because I
believe that I'd be able to get good inputs here.

I use Velocity as view, and struts for actions. Now I have several actions
which search DB for various searches and produce a list.
I have single velocity template "results' to which all these actions are
forwarded.

Now let's say I have a list of books which can be searched on various
parameters and various actions are defined for these searches. Now I need to
display only those books which satisfy condition - isAvailable (actual list
and condition is far more complex) and not otherwise. I have 2 approaches -

(1) Put a conditional check in velocity template while iterating though a
list to decide if the book should be displayed. It would be sth like this -

#foreach($book in booklist)
    #if($StockManager.isAvailable($book.number))
         ## do whatever here...
    #end
    ## don't display anything if book is not available
#end

(2) Another approach is to check this in VARIOUS actions while creating the
list itself if the book should be added to the list at all.

First approach sounds easier to implement, but it means that the view part
is taking decision about business logic, which sounds "not so nice"
Second approach is difficult because there are many actions that will have
to be changed, besides there are many programmers working on various
actions, so they all need to take care of that. So I am trying to avoid
this.... However, there's one more advantage here, if I do it this way I can
add a unit test to check that only "available" books are getting added.

What should be the best approach in this case, it's not just about getting
this done somehow.
I'd highly appreciate any inputs here.

TIA,
- Manish

p.s. BTW, is there anything similar to Java "continue" in VTL?




---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Conditional display - Approach question

Posted by Manish <pr...@gmx.net>.
> > What should be the best approach in this case, it's not just about
getting
> > this done somehow.
> > I'd highly appreciate any inputs here.
> First, I'd ask myself, "What business does the unavailable books have
> in booklist in the first place?".  If there is a darn good reason, I'd
> choose (1).  Otherwise, (2) is my choice.
> As for the many programmers, ask them, "Would you like to fix 100
> classes now, or 200 classes later?".  Postponing good design decisions
> increases the pressure upon the team.  Of course, it looks like you
> have the tests, so it wouldn't be so much pressure (IF they are
> automated).

Thanks for your inputs Shinobu,
I have indeed used approach (2) with some smart refactoring. My main concern
was the fact that every action now would require to go through this routine.
But the code I have rewritten now isolates this functionality and as long as
other actions use this module, they're safe. :)

The tests are automated indeed.

- Manish


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Conditional display - Approach question

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Manish,

> Now let's say I have a list of books which can be searched on various
> parameters and various actions are defined for these searches. Now I need to
> display only those books which satisfy condition - isAvailable (actual list
> and condition is far more complex) and not otherwise. I have 2 approaches -
> 
> (1) Put a conditional check in velocity template while iterating though a
> list to decide if the book should be displayed. It would be sth like this -
> 
> #foreach($book in booklist)
>    #if($StockManager.isAvailable($book.number))
>         ## do whatever here...
>    #end
>    ## don't display anything if book is not available
> #end
> 
> (2) Another approach is to check this in VARIOUS actions while creating the
> list itself if the book should be added to the list at all.
> 
> First approach sounds easier to implement, but it means that the view part
> is taking decision about business logic, which sounds "not so nice"
> Second approach is difficult because there are many actions that will have
> to be changed, besides there are many programmers working on various
> actions, so they all need to take care of that. So I am trying to avoid
> this.... However, there's one more advantage here, if I do it this way I can
> add a unit test to check that only "available" books are getting added.
> 
> What should be the best approach in this case, it's not just about getting
> this done somehow.
> I'd highly appreciate any inputs here.
First, I'd ask myself, "What business does the unavailable books have
in booklist in the first place?".  If there is a darn good reason, I'd
choose (1).  Otherwise, (2) is my choice.
As for the many programmers, ask them, "Would you like to fix 100
classes now, or 200 classes later?".  Postponing good design decisions
increases the pressure upon the team.  Of course, it looks like you
have the tests, so it wouldn't be so much pressure (IF they are
automated).

> p.s. BTW, is there anything similar to Java "continue" in VTL?
None that I know of, but it might be a good enhancement request.  ;)

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Conditional display - Approach question

Posted by Mike Kienenberger <mk...@alaska.net>.
Manish <pr...@gmx.net> wrote:
> (1) Put a conditional check in velocity template while iterating though a
> list to decide if the book should be displayed. It would be sth like this 
-
> 
> #foreach($book in booklist)
>     #if($StockManager.isAvailable($book.number))
>          ## do whatever here...
>     #end
>     ## don't display anything if book is not available
> #end
> 
> (2) Another approach is to check this in VARIOUS actions while creating 
the
> list itself if the book should be added to the list at all.
> 
> First approach sounds easier to implement, but it means that the view part
> is taking decision about business logic, which sounds "not so nice"
> Second approach is difficult because there are many actions that will have
> to be changed, besides there are many programmers working on various
> actions, so they all need to take care of that. So I am trying to avoid
> this.... However, there's one more advantage here, if I do it this way I 
can
> add a unit test to check that only "available" books are getting added.

If you want to be a purist, you probably need to decide if it's business 
logic or display logic.  Is there any chance you'll eventually want to 
display non-available books?

If you decide it's business logic, another option would be to forward 
everything to a filtering action which then forwards to your display action. 
 The "logic" would still be separate from the view logic, but you'd have 
chained them together in your config file.  This is probably how I'd do it.


> p.s. BTW, is there anything similar to Java "continue" in VTL?

If you're talking about #foreach loops, in standard velocity there's 
nothing.   There may-or-may-not be a #stop directive you can install as a 
patch in the bugzilla database, but I use an #if statement.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: Conditional display - Approach question

Posted by Tim Colson <tc...@cisco.com>.
> (1) Put a conditional check in velocity template while 
Most of the developers I work with do this because as you said, it's easier.
At least at first.

#if($StockManager.isAvailable($book.number))

Problems surface when the rule changes. Example -- show the book if it
isAvailable() or if the current user is an InventoryManager who might need
to update the inventory number.

#if($StockManager.isAvailable($book.number) ||
$UserManager.isInventoryManager())

And then, the rule always changes again... you get the picture. :-)

> (2) Another approach is to check this in VARIOUS actions 
> while creating the
> list itself if the book should be added to the list at all.

I think that is a solid step in the right direction. Personally, I try to
move the biz decisions further into the business layer. 

booklist = myStockManager.getAvailableBooksforUser(thisUser);

The less the template and Struts action knows about my [oft changing]
Business rules, the better IMHO. :-)

Cheers,
Timo




---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org