Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
5 Мб
Скачать
☆

17.5.4 Grouping Together Logically Related Functionality

If you have a dozen procedures and functions that all revolve around a particular feature or aspect of your application, put them all into a package so that you can manage (and find) that code more easily. This is most important when coding the business rules for your application. When implementing business rules, follow these important guidelines:

  • Do not hardcode them (usually repeatedly) into individual application components.

  • Do not scatter them across many different standalone, hard-to-manage programs.

Before you start building an application, construct a series of packages that encapsulate all of its rules. Sometimes these rules are part of a larger package, such as a table encapsulation package. In other cases, you might establish a package that contains nothing but the key rules. Here is one example:

/* File on web: custrules.pkg */

CREATE OR REPLACE PACKAGE customer_rules

IS

FUNCTION min_balance RETURN PLS_INTEGER; /* Toronto */

FUNCTION eligible_for_discount

(customer_in IN customer%ROWTYPE)

RETURN BOOLEAN;

FUNCTION eligible_for_discount

(customer_id_in IN customer.customer_id%TYPE)

RETURN BOOLEAN;

END customer_rules;

The "eligible for discount" function is hidden away in the package so that it can be easily managed. I also use overloading to offer two different interfaces to the formula: one that accepts a primary key and establishes eligibility for that customer in the database, and a second that applies its logic to customer information already loaded into a %ROWTYPE record. Why did I do this? Because if a person has already queried the customer information from the database, he can use the %ROWTYPE overloading and avoid a second query.

Of course, not all "logically related functionality" has to do with business rules. I might need to add to the built-in string manipulation functions of PL/SQL. Rather than create twelve different standalone functions, I will create a "string enhancements" package and put all of the functions there. Then I and others know where to go to access that functionality.

17.5.5 Caching Static Session Data

Takeadvantage of persistent package data to improve the response time of your application by caching (and not requerying) static data. You can do this at a number of different levels; for each of the following items, I've listed a few helpful code examples available on the O'Reilly site:

  • Cache a single value, such as the name of the session (returned by the USER function). Examples: thisuser.pkg and thisuser.tst.

  • Cache a single row or set of information, such as the configuration information for a given user. Examples: init.pkg and init.tst.

  • Cache a whole list of values, such as the contents of a static, reference code lookup table. Examples: emplu.pkg (employee lookup) and emplu.tst.

Use the test files to compare cached and noncached performance.

If you decide to take advantage of this technique, remember that this data is cached separately for each session that references the package. This means that if your cache of a row in a table consumes 20K and you have 1000 users, you have just used up 2 MB of real memory in your system—in addition to all the other memory consumed by Oracle.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]