0

Project exposes WebAPI to the external clients and they will embed multiple parameter values in query string and this query string data will be stored in DB.

Question My silly question is how I should pass query string values from WebAPI action to business layers.

I can think of two ways: 1. Passing complete Request object to business layer. 2. Convert query string parameters to a list or array and pass that list / array to business layer.

Do you consider passing Request is sort of overkilling or burdon on system. I m just thinking it might be a heavy object in term of size.

If I convert query string parameters am I doing something against development good practices or Microsoft recommendation ?

I highly appreciate your guidance.

4
  • 1
    I recommend not passing the Request object. This forces a dependency on System.Web.Http in your business layer which you should try to avoid where possible. Commented Oct 27, 2014 at 1:25
  • @trnelson highly appreciate your view. In term of good practices is it ok to transfer data from Request to object and send to business layer ? I m thinking in general it is said service should just expose busienss layer and should have every thing (logic) in business layer ? Commented Oct 27, 2014 at 1:29
  • @trnelson does WebAPI automatically convert query string parameters to object or JSON / XML ? Or I will need to do manually ? Please guide. Commented Oct 27, 2014 at 1:30
  • 3
    It's absolutely okay to use custom objects (generally DTO - Data Transfer Objects) to send data to your BL from your API layer. Sometimes referred to as a Orchestration pattern or Services Layer, your business layer in this case shouldn't know about the web except in certain circumstances. Web API will convert query string parameters into typed variables (int, string, etc.) and it's up to you whether to pass them into BL methods or to assemble DTO objects and pass those in. Generally, the latter makes the most sense, but like all things in software development, it depends. Commented Oct 27, 2014 at 1:34

2 Answers 2

1

You might consider this overkill, but I followed some guidance written up by Jimmy Bogard while describing his MediatR library:

https://lostechies.com/jimmybogard/2014/09/09/tackling-cross-cutting-concerns-with-a-mediator-pipeline/

The gist is to map your incoming requests to DTO objects (that are usually simple data classes) and use a pipeline to get them down to the business layer. This requires you to map your Request object, but provides very clean separation between the concerns of the API (which WebAPI handles), and those of your business domain (which your own code handles of course).

I have been using this approach in a manufacturing-based API with really positive results.

Sign up to request clarification or add additional context in comments.

Comments

0

Business layer should be independent of the layers calling it. If you pass the whole Request object into the business layer, you are tightly coupling the business layer to the WebAPI, which is considered a bad practice. Think of another scenario: What if you want to call the same business layer from a console application, or a windows service? Surely, you don't want to construct a whole Request object there just to make a call to the business layer.

As you have a reference from the WebAPI to the business layer, you should define a parameter object on the business layer. Thus, your business layer can use it as a parameter which the WebAPI can provide in the method call after it has filled it based on the request it is receiving. This promotes loose coupling and enables your business layer to change without it effecting the WebAPI layer, and vice versa.

Consider also handling exceptions in the WebAPI layer, as that is a place where you can return correct HTTP status codes, i.e. 404 NOT FOUND when a requested object cannot be found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.