Object-Oriented Design – What is Business Logic?

object-orientedobject-oriented-design

I'm working with web development since 2009, when I started with PHP. When I moved to ASP.NET, I've heard a lot about DDD and OOAD where a lot of focus is given to this "business logic" and "business rules". The point is that all the apps I've developed until now were all about CRUD operations and I've never seen these things in practice.

I simply can't imagine what those things can really be in practice. So, what really is this business logic and how does this fit into an app? I know these are implemented as methods in domain models, but what could those methods possibly be, and where in the application they could possibly be used?

Best Answer

CRUD is an acronym that stands for Create, Read, Update and Delete. Those are the four basic operations that you can perform on a database tuple. But there's always more to business applications than creating, reading, updating and deleting database records.

Let's start with some basic definitions, and then look at a couple of examples and see how those definitions map to the examples, and how they map to actual software.

Business logic or domain logic is that part of the program which encodes the real-world business rules that determine how data can be created, stored, and changed. It prescribes how business objects interact with one another, and enforces the routes and the methods by which business objects are accessed and updated.

Business Rules describe the operations, definitions and constraints that apply to an organization. The operations collectively form a process; every business uses these processes to form systems that get things done.

Now, let's work with some examples.

Transferring money from one checking account to another

First, what are the things that you need to know (input)?

  • The identity of the person making the transfer
  • The amount of money to be transferred
  • The source checking account number
  • The target checking account number

What are some of the "business rules" that must be applied?

  • The person making the request must have the authority to do so.
  • The transaction must be atomic.
  • The transaction may have reporting requirements to the government, if it is over a certain amount

By "atomic," I mean that the transaction must completely succeed or it must completely fail. You can't have account transactions where money is taken out of one account without arriving in the other (money disappears), or money is deposited into an account, but not debited from another account (money magically appears from nowhere).

Ordering something from Amazon.

What do you need to know?

  • The identity of the person ordering
  • Shipping information
  • Billing information
  • Method of Payment
  • Amount and quantity of each item to ship
  • How to ship (overnight, slow boat or super saver)
  • State Tax Rate

What happens after the order is placed?

  • Items are pulled from stock

  • On hand quantities are debited

  • Items are packaged for shipment

  • Out of stock items are backordered

  • Items that drop below minimum quantities are ordered

  • One shipment or two?

  • An invoice/shipping list is printed, and placed with the order

    ..etc.