Thursday, March 3, 2011

Is business logic a cross-cutting concern?

As a consultant and architect I have the opportunity to see a lot of different applications and architectures. Most of these applications have a kind of so called business logic layer. It states the idea of a layer in any way capable of handling all business logic all by itself.  Although these architectures claim that all business logic “belongs” to this layer, I always find business logic in at least at least two places and sometimes three.
Is there something wrong with these architectures or do we have to say that Business Logic really is a cross-cutting concern?

Let me explain this a little bit more…

A definition of business logic

Let’s state that business logic includes schema (types and constraints), derived values (timestamps, userstamps, calculations, histories), non-algorithmic compound operations (like batch billing) and algorithmic compound operations, those that require looping in their code. This encompasses everything we might do from the simplest passive things like a constraint that prevents discounts from being over 100% to the most complex hours-long business process, along with everything in between accounted for.

A sample application

Consider an admin interface to a database, where the user is entering or modifying prices for the price list. Now, if the user could enter plain text as the price, that would be kind of silly, so of course the numeric inputs only allow numeric values. Same goes for dates.

Now consider the case where the user is typing in a discount rate for this or that, and a discount is not allowed to be over 100%. The UI really ought to enforce this, otherwise the user's time is wasted when she enters an invalid value, finishes the entire form, and only then gets an error when she tries to save. In the database world we call this a constraint, so the UI needs to know about constraints to better serve the user.

Is this user allowed to change a price? If not, the button should either be grayed out or not be there at all. The UI needs to know about and enforce some security.

So in fact the UI layer not only knows the logic but is enforcing it. It is enforcing it for two reasons, to improve the user experience with date pickers, lists, and so forth, and to prevent the user from entering invalid data and wasting round trips.

Do you see the business logic leaking in?

You Cannot Isolate What Must be Duplicated

The UI layer is completely useless unless it is also enforcing as much logic as possible, and even when we leave the Database Server as the final enforcer of business logic (types, constraints, keys), it is still often good engineering to do some checks to prevent expensive wasted trips to the server.

This explains why a lot of people struggle with where to put the business logic. That struggle and its frustrations come from the mistake of imposing abstract conceptual responsibilities on each tier instead of using the tiers as each is able to get the job done. Databases are wonderful for type, entity integrity (uniqueness), referential integrity, ACID compliance, and many other things. Use them! Code is often better when the problem at hand cannot be solved with a combination of keys and constraints, but even that code can be put into the DB or in the application.

1 comment:

aliya seen said...

innovation management software is simply the best business performance management software as it provides the easiest way to visualize all the data you could ever manage and then really do something about your findings.