203
votes
Accepted
Should we design our code from the beginning to enable unit testing?
Reluctance to modify code for the sake of testing shows that a developer hasn't understood the role of tests, and by implication, their own role in the organization.
The software business revolves ...
116
votes
Should I check if something exists in the db and fail fast or wait for db exception
Checking for uniqueness and then setting is an antipattern; it can always happen that the ID is inserted concurrently between checking time and writing time. Databases are equipped to deal with this ...
79
votes
Accepted
How to design a REST API that can "prompt" the client about long-running operations?
For long-running operations, it often helps to model the active job as a REST resource with its own structure and/or sub-resources.
For example, starting a job may return a result such as
202 Accepted
...
75
votes
Should we design our code from the beginning to enable unit testing?
It's not as simple as you might think. Let's break it down.
Writing unit tests is definitely a good thing.
BUT!
Any change to your code can introduce a bug. So changing the code without a good ...
59
votes
Is it ok to have validation layer before access control layer
It depends on whether knowing the validity of some input for a task that you aren't permitted to do is a security leak. If it is, you really should to do it the other way round.
The only safe ...
39
votes
Should I check if something exists in the db and fail fast or wait for db exception
I think what you call “fail fast” and what I call it is not the same.
Telling the database to make a change and handling the failure, that is fast. Your way is complicated, slow and not particularly ...
36
votes
Accepted
Use empty string, null or remove empty property in API request/response
TLDR; Remove null properties
The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm). The JSON that you receive is ...
26
votes
Why PATCH method is not idempotent?
PATCH requests describe a set of operations to be applied to a resource, if you apply the same set of operations twice to the same resource, the result may not be the same. This is because defining ...
25
votes
Is it ok to have validation layer before access control layer
Well, there are multiple types of validation:
Cheap basic sanity-checking, which verifies that the request is not obviously malformed.
This is typically at least partially duplicated client-side, to ...
23
votes
Accepted
Isn't CQRS overengineering?
Is CQRS a relatively complicated and costly pattern ? Yes.
Is it over-engineering ? Absolutely not.
In the original article where Martin Fowler talks about CQRS you can see a lot of warnings about not ...
22
votes
Accepted
Is it bad practice to use hyphens in JSON keys?
You can use anything as JSON keys, as long as it is valid UTF-8, doesn't contain zero code points, and it would be useful if you could represent the key as a string in the programming language of your ...
21
votes
Accepted
Why do Microsoft's libraries depend on Newtonsoft.Json?
The most direct quote I've found is part of Scott Guthrie's announcement of the MVC 4 roadmap, back in 2012, (apparently offline but available via the Wayback Machine) which contains the following ...
20
votes
Accepted
Is Open Data Protocol (odata) Being Widely Embraced by the Development Community?
Given that:
The two "big name" implementors, Netflix and Ebay, abandoned it a couple of years ago and excitement for OData pretty much died with their departure,
OData is a RESTful way of exposing a ...
20
votes
Use empty string, null or remove empty property in API request/response
Going with an empty string is a definitive no. Empty string still is a value, it is just empty. No value should be indicated using a construct which represents nothing, null.
From API developer's ...
19
votes
Accepted
Is it a bad idea to pass JSON objects on the query string for an API "search" operation?
Its not a brilliant idea. The URI is not really a good place for data of unpredictable length, and although there is no 'official' maximum length, many webservers apply their own limit (IIS is 2083 ...
18
votes
Why PATCH method is not idempotent?
I think clear answer when PATCH in not idempotent is this paragraph from RFC 5789:
There are also cases where patch formats do not need to operate from a
known base-point (e.g., appending text ...
18
votes
Accepted
Use composition and inheritance for DTOs
As a best practice, try and make your DTOs as concise as possible. Only return what you need to return. Only use what you need to use. If that means a few extra DTOs, so be it.
In your example, a ...
18
votes
Should we design our code from the beginning to enable unit testing?
Designing code to be inherently testable is not a code smell; on the contrary, it is the sign of a good design. There are several well-known and widely-used design patterns based on this (e.g., Model-...
17
votes
Best practice for REST API call with many parameters
I have a REST API with GETs operations which receive a (long) list of >
parameters. Which is the best practice to manage this scenario?
AFAIK, there is no firmly established best practice (sorry). ...
16
votes
Why do people do REST API's instead of DBAL's?
Warning: big post, some opinions, vague 'do what works best for you' conclusion
Generally, this is done as a means of implementing 'hexagonal architecture' around your database. You can have web ...
16
votes
Should I check if something exists in the db and fail fast or wait for db exception
This started as a comment but grew too large.
No, as the other answers have stated, this pattern should not be used.*
When dealing with systems that use asynchronous components, there will always ...
14
votes
Is it ok to have validation layer before access control layer
There must be some validation before access control. Let's say SO's API has an endpoint "edit answer", then whether the user can edit a particular answer can depend on the answer (below a certain ...
13
votes
Is it bad practice to use hyphens in JSON keys?
There are plenty of JSON serialization systems that are more than capable of handling mapping between field names that aren't suitable for use in the language they integrate with. In most cases, they ...
13
votes
Accepted
How to design a REST API to handle non-CRUD operations?
You need to watch this talk by Jim Webber.
When I need to update the state of the subscription, I cannot simply send a POST request to the server, because I don't have direct access to the resources, ...
13
votes
Should we design our code from the beginning to enable unit testing?
It is IMHO very simple to understand that for creating unit tests, the code to be tested must have at least certain properties. For example, if the code does not consist of individual units which can ...
13
votes
How to map "mv" operation to HTTP verbs?
Is there a RESTful way to implement this whilst maintaining atomicity of the operation?
Short Answer
Just use POST
Medium Answer
Seriously; it is okay to use POST.
POST serves many useful purposes ...
12
votes
Accepted
Repository pattern, call another API that updates a SOR's from service or repository class?
The calls to other APIs should go in the repository layer.
The service layer shouldn't care how it gets its data, no matter where that data comes from.
The job of a repository isn't communicating ...
12
votes
Should we design our code from the beginning to enable unit testing?
I take issue with the (unsubstantiated) assertion you make:
to unit test the Web API service we will need to mock this factory
That's not necessarily true. There are lots of ways to write tests, ...
10
votes
Accepted
What are the consequences of using verbs instead of nouns in REST API URI?
Standard URL mapping for REST has the resource mapped to the URL and what you do to it in the HTTP method.
It works well when interacting programmatically with your REST endpoint. It's also very ...
10
votes
Should we design our code from the beginning to enable unit testing?
You are in luck as this is a new project. I've found that Test Driven Design works very well for writing good code (which is why we do it in the first place).
By figuring out up front how to invoke ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
web-api × 372rest × 124
api-design × 115
api × 84
c# × 59
web-applications × 31
architecture × 28
web-development × 27
asp.net × 27
asp.net-mvc-web-api × 26
web-services × 24
asp.net-core × 24
design × 20
.net × 15
entity-framework × 14
http × 14
design-patterns × 13
json × 13
asp.net-mvc × 12
security × 11
microservices × 11
database × 9
authentication × 9
php × 7
mobile × 7