73 votes

Business logic: Database vs code

See How much business logic should the database implement? for previous discussion. In general, everyone wants things done in the layer they control. Because then they control it. Every database ...
btilly's user avatar
  • 18.3k
37 votes
Accepted

Separating business logic from data tempts me to use instanceof

Your problem is that you're trying to use both rich domain models I always try to use polymorphism and anaemic domain models I try to separate data from business logic at the same time. Both are ...
Philip Kendall's user avatar
30 votes
Accepted

How exactly should a CQRS Command be validated and transformed to a domain object?

The following answer is in the context of the CQRS style promoted by the cqrs.nu in which commands arrive directly on the aggregates. In this architectural style the application services are being ...
Constantin Galbenu's user avatar
22 votes

Separating business logic from data tempts me to use instanceof

Though Philip Kendall's answer has a point, I think it is oversimplifying. One can separate data from behaviour and still use object oriented means and polymorphism. If you need your data (Bar, Baz) ...
Doc Brown's user avatar
  • 208k
20 votes

Business logic: Database vs code

I am very firmly of the view that when ever possible, business logic should be kept in the software layer and not the database layer. Note, that when ever possible falls far short of always. There ...
Michael Shaw's user avatar
  • 9,945
20 votes
Accepted

Business logic vs Service layer

"Service layer" is an architectural term. It refers to a portion of the system that sits somewhere in the middle of a multi-tier architecture, below the user interaction layer but above the data ...
John Wu's user avatar
  • 26.5k
17 votes
Accepted

Where should business logic sit in microservice architecture?

People often hear "micro-service" and think "nano-service", and this can cause some confusion. These are micro-services, so you don't need a separate service for every single entity. Everything you ...
TheCatWhisperer's user avatar
12 votes
Accepted

When should I use stored procedures?

You have a couple of perfectly good scenarios already. There are lots of other reasons too. EF is really good at CRUD and at pretty straight forward reporting. Sometimes, though, EF is not the ...
Joel Brown's user avatar
  • 2,398
12 votes
Accepted

Using static type checking to protect against business errors

You are essentially asking for a unit system (no, not unit tests, "unit" as in "physical unit", like meters, volts etc.). In your code Age represents time and Pounds represents mass. This leads to ...
null's user avatar
  • 3,566
11 votes
Accepted

Entity Framework and avoiding the Anemic Domain Model

The issue is, you are using EF objects as domain objects in the first place. EF objects are data models NOT business models. You need to declare business objects which give you the freedom to do as ...
TheCatWhisperer's user avatar
10 votes

Business logic: Database vs code

Two very important points are missing in your pro-database arguments: performance: database code is executed with direct access to the data, thus avoiding unnecessary transfers (be it across ...
Christophe's user avatar
  • 77.3k
10 votes

How exactly should a CQRS Command be validated and transformed to a domain object?

One fundamental premise of DDD is that domain models validate themselves. This is a critical concept because it elevates your domain as the responsible party for making sure your business rules are ...
user3347715's user avatar
  • 3,144
9 votes
Accepted

Domain Objects and How they Access the Database

No, domain objects should not have any idea of the existence of a repository, or even a repository interface. If you feel the need to do this, that means there is an underlying issue with your design (...
TheCatWhisperer's user avatar
9 votes

Separation of application logic and domain logic in Clean Architecture

You don't need a one-to-one mapping of interactors and entities. I believe such a design would be harmful. DDD is all about context boundaries and the ubiquitous language within those boundaries. ...
bitsoflogic's user avatar
8 votes
Accepted

How to keep track of growing catalog of business rules?

Requirements need to be documented. You are already attempting to do that, but requirements are often broken down into a number of categories: Business requirements — Things like "A blog post ...
Greg Burghardt's user avatar
8 votes

How to clean up a business logic class that does too many things without injecting a ton of classes into it?

There is nothing wrong per se with a class BusinessLogic which orchestrates the logic of calling methods of a huge list of other objects, as long as one does not mix up different levels of abstraction....
Doc Brown's user avatar
  • 208k
7 votes
Accepted

business rules, business proceses and business logic

A business process is a series of actions that are executed by a business to perform a particular task. For example, when a customer places an order with a company that company goes through a process ...
JDT's user avatar
  • 6,390
7 votes
Accepted

Design: Should Undo/Redo functionality be part of the business layer of an app?

Surely this totally depends on your business. Example 1: document editor with save functionality The editor itself might give you undo/redo as you edit, but those commands arent persisted via save. ...
Ewan's user avatar
  • 75.8k
7 votes

Is it good practice to put service business logic based on audit trail?

It depends (but I guess for most real-world situations the answer is no, you risk to run into trouble). A general-purpose audit trail may collect data which, depending on the details, might be ...
Doc Brown's user avatar
  • 208k
7 votes

What defines a business rule, as opposed to application or presentation logic?

A business rule is defined by business people and relates to business matters, regardless of the way these rules are implemented. Unambiguous examples: The change of the bank account of a ...
Christophe's user avatar
  • 77.3k
6 votes

REST is not appropriate for business applications because of necessary to distribute business logic accross layers. REST alternative required!

There is a gross misconception somewhere. I think it is near this idea. In Spring+JSF application all the business logic was encoded in special layer - Spring entities and services. In Spring+REST+...
VoiceOfUnreason's user avatar
6 votes

How acceptable is to keep business logic outside entities (in separate service classes)?

If you are literally talking about business logic as in, it's for a business and not domain logic in general, I find most entities have logic imposed on them by outside influencers or because of the ...
JeffO's user avatar
  • 36.8k
6 votes

Using static type checking to protect against business errors

In contrast to null's answer, defining a type for a "unit" can be beneficial if an integer isn't enough to describe the measurement. For instance, weight is often measured in multiple units within the ...
Greg Burghardt's user avatar
6 votes
Accepted

DDD - delegate business rule of domain object to external service

A Domain Object is not responsible for providing information outside of it's own context. Whether or not an API token is active is not something the token itself can answer, but is instead a question ...
e_i_pi's user avatar
  • 859
6 votes
Accepted

Is My Data Access Layer Really My Domain?

Usually the naming of the layers depend on the architectural approach: The traditional layering is presentation / business logic / data access layer. Another popular variant is Fowler’s ...
Christophe's user avatar
  • 77.3k
5 votes
Accepted

What should be in my business logic class

The "correct" way is the one that most effectively satisfies your application's non-functional requirements for maintainability, performance, etc. What you need is a Business Logic Layer or Service ...
Robert Harvey's user avatar
5 votes

Separation of retrieving data and business objects between DAL and BLL Layers

I cannot see how to determine what object the GetCustomerById in DAL should return? Should it return BllCustomer or DalCustomer? It should return a DalCustomer object, returning a BllCustomer ...
Derika's user avatar
  • 51
5 votes

How much business logic should the database implement?

After a few years, the question is still important... Simple rule-of-thumb for me: if it's a logical constraint or an ubiquitous expression (single statement), place it in the database (yes, foreign ...
Erik Hart's user avatar
  • 376
5 votes
Accepted

Can a default value for a business process be set in the database column definition?

You need to migrate some data. This logic belongs in that script. The script should change current entries to 15, but you absolutely shouldn't leave artifacts of that decision in your schema. After ...
Nathan's user avatar
  • 1,319
5 votes

How acceptable is to keep business logic outside entities (in separate service classes)?

I believe that the problem you referring to is connected to single class characteristics and level of class decomposition established for a considered problem. Each of your examples sounds reasonable ...
tom3k's user avatar
  • 77

Only top scored, non community-wiki answers of a minimum length are eligible