Execute Around Method??

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Execute Around Method??

Fernando Rodriguez
Hi,

        I just saw this mentioned in a SUnit tutorial I found
(http://www.xprogramming.com/testfram.htm). What is it? O:-)


Reply | Threaded
Open this post in threaded view
|

Re: Execute Around Method??

Syver Enstad-5
Fernando Rodriguez <[hidden email]> writes:

> Hi,
>
> I just saw this mentioned in a SUnit tutorial I found
> (http://www.xprogramming.com/testfram.htm). What is it? O:-)


Example of typical code not using Execute Around Method:

conn := db open.
" do stuff with connection "
conn close.


execute around method on db

whileOpenConnection: aBlock
    " you should use exception handling to
    ensure that conn is closed no matter what happens in aBlock value:
    """
    | conn |
    conn := self open.
    aBlock value: conn.
    conn close.

So you write your code like this instead:

db whileOpenConnection:
    [:conn | " do stuff with connection "].

Doing stuff in an execute around method eliminates the duplicate code
that opens and closes the resource (connection) and also exception
handling (which I omitted for brevity).