Microsoft Office Automation with Visual FoxPro

Why wrap the servers?

A wrapper class, as the name implies, goes around another class, or sometimes around code that isn t object-oriented (for example, something like FoxTools) to give it an object structure. The goal, generally, is to provide a cleaner interface and to offer the opportunity of subclassing. (If you re familiar with design patterns, you may recognize wrapper classes as an implementation of the Adaptor pattern. To learn about patterns, a very useful tool in class design, we recommend the classic Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissides.)

When dealing with Automation, wrapper classes do several things for you.

First, they let you simplify. Many methods of the Office servers expect tons of parameters. In your wrapper classes, you can hide all those extra parameters. The methods you choose to expose accept only the parameters you really need your code can deal with the rest internally. Alternatively, you can expose a set of properties to allow the calling program to set those parameters it needs, and then use your method call to set default values for any unspecified parameters.

Second, wrapper classes protect you from Microsoft s changes. If the order of parameters to a server method does change, you only have to fix it in one place your wrapper class not everywhere you ve used that method.

Third, wrapper classes let you centralize error handling. For example, rather than writing all your application code to worry about whether you have an instance of the server available, your application code calls on methods of your wrapper class and those methods worry about whether the server is available. In the same way, your wrapper class methods can worry about whether there s an open document and what to do if a call to the server fails.

 

Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved

Категории