Question

I looked at questions with similar titles with no success. Can I create service reference from Business Layer to pull data from another business entity? Or should it be done from Service Layer?

Was it helpful?

Solution

This isn't easy to answer because it could spiral into a huge philosophical discussion. However, I do not think your business logic layer should go back to your service layer to get other business entities.

A typical approach for this scenario is to have a facade layer above the business logic. That layer is responsible for coordinating a response when the retrieval of multiple business entities is necessary. So:

Service -> Business Facade -> Business Logic -> Data

EDIT: For small, simple apps, this is overkill. Remove the facade layer, and simply have the service call a logic method, or have the service call multiple logic methods.

The service layer is really just a pass-through, and you're better off with as little logic there as possible. That makes is possible to either replace the service layer, or to have trusted apps/services call the facade layer directly (on the local machine) when it's unnecessary to make a service call.

This approach also allows you to place a "line of trust" at the facade level, and implement security there. If security checks are to be done, and possibly other things at this "line of trust," then we only want one service call to the business facade, so we don't repeat this logic for each entity that is necessary to retrieve.

The facade layer is simply a layer of methods that call methods on the logic layer. The facade methods can be as simple as calling one method on the logic layer, and they can be as complex as calling multiple methods on the logic layer and orchestrating a coherent response of proper domain entities, or even DTOs.

I could go on. Indeed there are entire books devoted to this discussion. Hopefully that helps with at least a broad overview.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top