Business Logic: I Know It When I See It!

So we all know that business logic is supposed to be separate from any other layer in your application.  Other logic like database access and user interface logic should not be mixed in with the business logic.  This is all well and good but it begs the question – what is business logic?  How do I recognize it in code?

Is this business logic?

String sql = "Select * from Customer";

The answer is no – it is database access logic?

Is this business logic?

String sql = String.Format("Select * from Customer where customerId = {0}", customerId);

It might be.  If the application’s UI is simply displaying a customer’s record – then this is not business logic? However, if the application does something different, does a calculation, etc.. depending on the result of this statement, then it is business logic?

How about this?

Int32 customerTypeId=0;
customerTypeId = this.customerTypeDropDownList.SelectedIndex;
if(customerTypeId == 1)
{
    Response.Redirect("RegularCustomer.aspx");
}
else
{
    Response.Redirect("SpecialCustomer.aspx");
}

Is basic form navigation considered  business logic? 

And what about this?

if(customerTypeId == 1)
{
    InsertNewOrder();
}
else
{
    UpdateNewOrder();
}

Is basic control of flow of the application considered business logic?

Finally, what about?

foreach (LineItem lineItem in order.LineItems)
{
    total += lineItem.Price;
}

total += total * salesTax;

That sure looks like business logic – because it is doing a calculation?

Examples aside, can every line of code be categorized?  If so, what are the categories?

  • User Navigation?
  • Input Validation?
  • Database Interaction?
  • Business Logic?
    I went and did a search on “business logic” and there is little out there –and certainly nothing canonical.
    I would assume that business logic is context-free.  So If I took a line of code from one place in my application and put it in another, it still would be business logic.

I assume that code can morph into (or away from) business logic.  For example, that simple sql select statement might not be business logic, but once I all all these crazy where clauses, it becomes business logic – or if I add an order by statement it becomes presentation logic.

Another consideration is that the visual database controls that comes our of the box in Win and Web Forms

image

Automatically mix database and presentation logic together and if you add any conditional statements to the control, if becomes a three-headed monster of database, presentation, and business logic.

Similarly, all stored procedures that have an “IF” or “CASE” in them are probably business logic – business logic that is tightly coupled (indeed embedded) in the database CRUD.

Does that mean that any application that uses a visual control or a stored procedure has an incorrect architecture?  Maybe.  Perhaps separation of concerns is like the Open/Closed principle – you never get there but it is worth trying.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: