0

I'm building an two service applications, one that interfaces the database using Entity Framework (my Data access layer) and another that handles all my business logic. Both applications are Web API apps.

I want to return one JSON response from the business api to my external clients ultimately but I'm unsure what the best approach is behind the scenes. The data returned will be a nested JSON response describing a member of a company. Should my business layer make multiple asynchronous calls to the data api for collection of data? Or should my business layer make one call and have the data access layer make multiple calls in one method and return one nested json response back to the business layer?

I'm fairly new to this type of architecture so I want to make sure I'm following best practices.

chris
  • 1,152
  • 1
  • 15
  • 37
  • 4
    Test, benchmark. This depends on so many factors invisible to us that it's impossible to say anything useful. – Gert Arnold Apr 04 '15 at 21:44
  • My *preference* is to have the service itself provide aggregation (and a separate/isolated view from the underlying data source). However there isn't One True Way .. you may be interested in reading up about "aggregate roots" (which may provide insight into a common division), of which there are many articles and opinions. In either case, I think it is important to have the service layer *add value* and not expose the data implementation in a brittle manner (which is why I do not reuse EF entities at the service boundary). – user2864740 Apr 04 '15 at 21:44

2 Answers2

-2

If the data is too big, and you think that performance is an issue then maybe it's best to split up your web api so you let the end user choose what data he wants to get because he will certainly have the same issue.

This can be achieved by splitting information type in different functions, or encouraging Filters and Paging functionality (and even set limits) on your API.

Does your API have to make multiple calls, depends also what are the queries you are making to collect the data... If you do multiple calls, you will always have to group your queries by Customer, and somehow repeat some mapping of EF on your model, and when you'll return the data, you'll have to make the same job on your results. This can only be motivated by huge data load (Performance of SQL queries are going down when your locking one or multiple tables)

Laurent Lequenne
  • 902
  • 5
  • 13
-2

It depends.

In any case, you need to determine where your bottleneck is. Load testing is your friend here.

Don't go with a certain type of architecture just because it's popular or because it looks neat in diagrams. Go with the simplest thing that works, and then let the requirements and test results tell you where to go next.

This way you'll make the most sensible decisions based on what is actually needed from a business perspective. It's an obvious but commonly overlooked best practice.

If you and your clients are using OData and your business logic layer only takes care of basic things like validation, then most of the times you'll have a single HTTP call to your API for each requested action/function by the client. The OData query will contain information about which properties/entities to GET, POST or PUT. Consequently, you are never sending more information over the wire than necessary so the request can be passed to your data layer as-is.

This changes when (any or all):

  • You have large models in a more simple, CRUD-like application.
  • You have complex (long-running) logic in your business logic layer
  • Your business logic / data layer needs data from more than one source, for example external services that enrich product information.

Keep in mind that the performance benefit from asynchronous processing only comes if requests are actually executed simultaneously. Your API needs to return exactly the same result if a certain action or function is split up into multiple requests further down the chain, compared to when it is not split up at all.

To be able to say something useful about this I would need more information about the degree of idem-potency and interdependence of the API calls to your BLL and DAL.

Having said all that, performance may not even be your first concern here. If your primary objective is to have a highly extensible, pluggable architecture where different components can be developed independently of each other, you generally have two flavors:

  1. An OWIN-like approach with a pipeline. You can easily plug additional middleware into the pipeline (even from external assemblies) and whether or not you split up the JSON in any part of the communication chain depends on whether or not there is a performance benefit in doing so.

  2. A full-blown SOA where each component is a webservice by itself, also referred to as "microservices". You might go with this approach if your data comes from different (internal and/or external) sources. This gives you more flexibility when one of those sources updates its specification - you would only need to update and re-deploy that particular API.

But, and I cannot stress this enough: start with the simplest thing possible and from there try to find out what you actually need.

Fred Kleuver
  • 7,797
  • 2
  • 27
  • 38