I've never encountered a well written business layer. Any advice?
Asked Answered
V

9

12

I look around and see some great snippets of code for defining rules, validation, business objects (entities) and the like, but I have to admit to having never seen a great and well-written business layer in its entirety.

I'm left knowing what I don't like, but not knowing what a great one is.

Can anyone point out some good OO business layers (or great business objects) or let me know how they judge a business layer and what makes one great?

Thanks

Vedavedalia answered 14/10, 2008 at 3:8 Comment(0)
C
5

Good business layers have been designed after a thorough domain analysis. If you can capture the business' semantics and isolate it from any kind of implementation, whether that be in data storage or any specific application (including presentation), then the logic should be well-factored and reusable in different contexts.

Just as a good database schema design should capture business semantics and isolate itself from any application, a business layer should do the same and even if a database schema and a business layer describe the same entities and concepts, the two should be usable in separate contexts--a database schema shouldn't have to change even when the business logic changes unless the schema doesn't reflect the current business. A business layer should work with any storage schema provided that it's abstracted via an intermdiate layer. For example, the ADO.NET Entity framework lets you design a conceptual schema which maps to the business layer and has a separate mapping to the storage schema which can be changed without recompiling the business object layer or conceptual layer.

If a person from the business side of things can look at code written with the business layer and have a rough idea of what's going on then it might be a good indication that the objects were designed right--you've succesfully conveyed a solution in the problem domain without obfuscating it with artifacts from the solution domain.

Cartagena answered 14/10, 2008 at 6:51 Comment(1)
I have started reading up on Domain Driven Design and the approach of creating entities that mean something seems to be pretty key; I think it is called "ubiquitous fluency". The idea seems sound, but I'm unsure of the implementation. Great response, thanks.Vedavedalia
A
14

I’ve never encountered a well written business layer.

Here is Alex Papadimoulis's take on this:

[...] If you think about it, virtually every line of code in a software application is business logic:

  • The Customers database table, with its CustomerNumber (CHAR-13), ApprovedDate (DATETIME), and SalesRepName (VARCHAR-35) columns: business logic. If it wasn’t, it’d just be Table032 with Column01, Column02, and Column03.
  • The subroutine that extends a ten-percent discount to first time customers: definitely business logic. And hopefully, not soft-coded.
  • And the code that highlights past-due invoices in red: that’s business logic, too. Internet Explorer certainly doesn’t look for the strings “unpaid” and “30+ days” and go, hey, that sure would look good with a #990000 background!

So how then is possible to encapsulate all of this business logic in a single layer of code? With terrible architecture and bad code of course!

[...] By implying that a system’s architecture should include a layer dedicated to business logic, many developers employ all sorts of horribly clever techniques to achieve that goal. And it always ends up in a disaster.

Adrial answered 14/10, 2008 at 3:19 Comment(2)
To be honest, if it took several business layers to achieve something I think that could still make for an elegant approach. There could, and ideally would, be consistency between these layers that would allow this to be considered a good layer.Vedavedalia
Wow, I can't believe how many votes this totally incorrect quote has received. These days if you're not using an ORM framework in your data layer, you're probably doing it wrong - it really could be Table032, Column01, Column02 for all that data-layer cares about - its not business logic. The second point is business logic, good. The third point is 100% presentation layer, not business logic. Your past due invoices should have some sort of status flag or IsPastDue property that the presentation-layer properly renders as red or whatever. Again presentation is not business logic.Entelechy
V
6

I imagine this is because business logic, as a general rule, is arbitrary and nasty. Garbage in, garbage out.

Also, most of the really good business layers are most probably proprietary. ;-)

Verbenaceous answered 14/10, 2008 at 3:13 Comment(2)
I have to agree. Does anyone know of any good, non-proprietary examples?Vedavedalia
+1. I know at least two good examples, but cannot give out the sources as I'm under NDA.Ramon
C
5

Good business layers have been designed after a thorough domain analysis. If you can capture the business' semantics and isolate it from any kind of implementation, whether that be in data storage or any specific application (including presentation), then the logic should be well-factored and reusable in different contexts.

Just as a good database schema design should capture business semantics and isolate itself from any application, a business layer should do the same and even if a database schema and a business layer describe the same entities and concepts, the two should be usable in separate contexts--a database schema shouldn't have to change even when the business logic changes unless the schema doesn't reflect the current business. A business layer should work with any storage schema provided that it's abstracted via an intermdiate layer. For example, the ADO.NET Entity framework lets you design a conceptual schema which maps to the business layer and has a separate mapping to the storage schema which can be changed without recompiling the business object layer or conceptual layer.

If a person from the business side of things can look at code written with the business layer and have a rough idea of what's going on then it might be a good indication that the objects were designed right--you've succesfully conveyed a solution in the problem domain without obfuscating it with artifacts from the solution domain.

Cartagena answered 14/10, 2008 at 6:51 Comment(1)
I have started reading up on Domain Driven Design and the approach of creating entities that mean something seems to be pretty key; I think it is called "ubiquitous fluency". The idea seems sound, but I'm unsure of the implementation. Great response, thanks.Vedavedalia
J
4

I've always been stuck between a rock and a hard place. Ideally, your business logic wouldn't be at all concerned with database or UI-related issues.

Keys Cause Problems Still, I find things like primary and foreign keys causing problems. Even tools like Entity Framework don't completely eliminate this creep. It can be extremely inefficient to convert IDs passed as POST data into their respective objects, only to pass this to the business layer, which then passes them to the data layer to just be stripped down again.

Even NoSQL databases come with problems. They tend to return full object models, but they usually return more than you need and can lead to problems because you're assuming that object model won't change. And keys are still found in NoSQL databases.

Reuse vs. Overhead There's also the issue of code reuse. It's pretty common for data layers to return fully populated objects, including every column in that particular table or tables. However, often business logic only cares about a limited subset of this information. It lends itself to specialized data transfer objects that only carry with them the relavent data. Of course, you need to convert between representations, so you create a mapper class. Then, when you save, you need to somehow convert these lesser objects back into the full database representation or do a partial UPDATE (meaning a another SQL command).

So, I see a lot of business layer classes accepting objects mapping directly to database tables (data transfer objects). I also see a lot of business layers accepting raw UI values (presentation objects), as well. It's also not unusual to see business layers calling out to the database mid-computation to retrieve needed data. To try to grab it up-front would probably be inefficient (think about how and if-statement can affect the data that gets retrieved) and lazy loaded values result in a lot of magic or unintended calls out to the database.

Write Your Logic First Recently, I've been trying to write the "core" code first. This is the code that performs the actual business logic. I don't know about you, but many times when going over someone else's code, I ask the question, "But, where does it do [business rule]?" Often, the business logic is so crowded with concerns about grabbing data, transforming it and whatnot that I can't even see it (needle in a hay stack). So, now I implement the logic first and as I figure out what data I need, I add it as a parameter or add it to a parameter object. Getting the rest of the code to fit this new interface usually falls on a mediator class of some kind.

Like I said, though, you have to keep a lot in mind when writing business layers, including performance. The approach above has been useful lately because I don't have rights to version control or the database schema yet. I am working in a dark room with just my understanding of the requirements so far.

Write with Testing in Mind Utiltizing dependency injection can be useful for designing a good architecture up-front. Try to think about how you would test your code without hitting a database or other service. This also lends itself to small, reusable classes that can run in multiple contexts.

Conclusion My conclusion is that there really is no such thing as a perfect business layer. Even in the same application, there can be times when one approach only works 90% of the time. The best we can do is try to write the simplest thing that works. For the longest time I avoided DTOs and wrapped ADO.NET DataRows with objects so updates were immediately recorded in the underlying DataTable. This was a HUGE mistake because I couldn't copy objects and constraints caused exceptions to be thrown at weird times. I only did it to avoid setting parameter values explicitly.

Jacobo answered 4/1, 2013 at 17:31 Comment(0)
R
2

Martin Fowler has blogged extensively about DSLs. I would recommend starting there.

http://martinfowler.com/bliki/dsl.html

Repetitious answered 14/10, 2008 at 3:11 Comment(3)
I'm sorry, but Fowler's advise is lame... so theoretical and esoteric that it has lost its applicability.Iguanodon
I have to agree with dacracot here. Fowler's overrated.Torin
Fowler's DSL book is pretty good. It's applicability to building business layers is limited. It's more oriented toward making languages for customizing business logic that business analysts can write in. The DDD book is much more applicable to this question. Even Fowler's Analysis Patterns or POEAA would be more applicable.Jacobo
I
2

It was helpful to me to learn and play with CSLA.Net (if you are a MS guy). I've never implemented a "pure" CSLA application, but have used many of the ideas presented in the architecture.

Your best bet is keep looking for that elusive magic bullet and use the ideas that best fit the problem you are solving. Keep it simple.

Intermarry answered 14/10, 2008 at 6:29 Comment(0)
N
2

One problem I find is that even when you have a nicely designed business layer it is hard to stop business logic leaking out, and development tools tend to encourage this. For example as soon as you add a validator control to an ASP.NET WebForm you have let business logic leak out into the view. The validation should occur in the business layer and only the results of it displayed in the view. And as soon as you add constraints to a database you then have business logic in your database as well. DBA types tend to disagree strongly with this last point though.

Nausea answered 15/10, 2008 at 3:25 Comment(0)
I
0

Neither have I. We don't create a business layer in our applications. Instead we use MVC-ARS. The business logic is embedded in the (S) state machine and the (A) action.

Iguanodon answered 14/10, 2008 at 3:15 Comment(3)
Can you describe this a little more?Vedavedalia
We implement 2 layers, one is the web server and second is the database server. Each follows the MVC pattern. Within the database however, that pattern is called ARS to keep the vocabulary straight. The Representation is very much the View and the StateMachine is very much the Contoller, but...Iguanodon
... they diverge for Model and Action. Model equals Representation between servers. Action is more easily understood as the SQL that transacts the data. Controller in the web server is much more of a router, pushing actions to other components. Clear as mud?Iguanodon
T
-1

Possibly because in reality we are never able to fully decouple the business logic from the "process", the inputs, outputs, interface and that ultimately people find it hard to deal with the abstract let alone relating it back to reality.

Torin answered 14/10, 2008 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.