Skip to main content
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 ...
Kilian Foth's user avatar
115 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 ...
Kilian Foth's user avatar
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 ...
Ewan's user avatar
  • 77.4k
50 votes
Accepted

Purpose of async/await in web servers

As far as I can tell, every request is already being run on a thread pool (as empirically tested by logging the thread ID during each request), so making all calls use async/await within your ...
Ccm's user avatar
  • 2,084
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 ...
gnasher729's user avatar
27 votes
Accepted

What are the differences between .NET Framework, ASP.NET, .NET Core, ASP.NET Core and .NET Standard?

.NET Framework is a VM, a JIT compiler, an object memory system consisting of a memory allocator and a garbage collector, a loader, a linker, and a runtime system (collectively called the Common ...
Jörg W Mittag's user avatar
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-...
mmathis's user avatar
  • 5,498
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 ...
Mr.Mindor's user avatar
  • 309
13 votes

Can a caller abort an execution of code invoked by HTTP request?

HTTP doesn't work like that. The client sends a request, then the server sends a response back. No other communication occurs. Well, the server can send 1xx informational responses before the main ...
amon's user avatar
  • 135k
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 ...
Doc Brown's user avatar
  • 210k
12 votes
Accepted

What is the difference between a Query Object and a Repository?

Your question talks about repositories being IQueryable (i.e. classes implementing the IQueryable interface), whereas your linked resource talks about repositories returning IQueryable objects. That's ...
Flater's user avatar
  • 52.7k
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, ...
Daniel Pryden's user avatar
12 votes

Purpose of async/await in web servers

A threadpool does not have infinite threads. Each time you synchronously wait, you are holding onto a thread and doing nothing with it. If you instead await, the suspension will bubble up to the ...
Caleth's user avatar
  • 11.5k
11 votes

C# Namespace Ordering Coding Convention

When dealing with style, it is always more useful to take either an official style if one exists, or to use the one commonly adopted by the community of developers writing in a specific language. You'...
Arseni Mourzenko's user avatar
11 votes
Accepted

why controller lifetime is transient by default in asp.net core?

There's a number of advantages to transient controllers. I cannot tell you whether any of them were the reason they were done that way - speculating about the intent of designers does not make a good ...
Sebastian Redl's user avatar
11 votes

Is it a bad practice to use Singleton for DI in Asp.net rather than Scoped, Transient whenever possible?

Is it a bad practice to use X whenever possible? Yes. It is bad practice to turn off your brain and lean on absolutes. It's far better to understand why. So lets step back from that framework and try ...
candied_orange's user avatar
10 votes
Accepted

C# Namespace Ordering Coding Convention

How would you determine the importance? The simplest way of ordering the usings is to apply an alphabetical order; However, it is often useful the make the difference between usings pointing to the ....
Olivier Jacot-Descombes's user avatar
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 ...
Thorbjørn Ravn Andersen's user avatar
9 votes
Accepted

Seeding data into a new microservice

From your quote do not rely on making synchronous requests for that data. Instead, replicate or propagate that data (only the attributes you need) into the initial service’s database by using ...
Laiv's user avatar
  • 14.7k
9 votes

Purpose of async/await in web servers

It sounds to me like there are 2 wrong assumptions here. The point of async/await isn't to free up the socket/connection. You're freeing up the thread to do other work during some async operation. ...
Mars's user avatar
  • 273
8 votes

Should we design our code from the beginning to enable unit testing?

If you need to modify to the code, that is the code smell. From personal experience, if my code is difficult to write tests for, it's bad code. It's not bad code because it doesn't run or work as ...
David's user avatar
  • 271
8 votes
Accepted

If we are allowing anonymous users to register inside our web site, how we can prevent hackers from occupying others' email address

The purpose of verifying the email address is to obtain proof that the owner of the email address and the user who registered are the same person. It therefore follows, that the logical thing to do if ...
Joe's user avatar
  • 424
8 votes

C# extension methods design patterns and usage guidelines?

The goal of extension methods is specifically to be able to extend either classes of other assemblies or interfaces. The recommendation to use them sparingly is right, for several reasons: Extension ...
Arseni Mourzenko's user avatar
8 votes

Is it logical to use dependency injection in .net core library project?

In general DI should be supported in libraries. The actual implementation or selection of a DI Container should not be done by a library, that should be left to the root application. That way the ...
Emond's user avatar
  • 1,258
8 votes

Is it a bad practice to use Singleton for DI in Asp.net rather than Scoped, Transient whenever possible?

Firstly, you are confusing singletons provided by a DI container with the "Gang of Four" singleton pattern. The latter is a glorified global variable and thus a complete anti-pattern and ...
David Arno's user avatar
  • 39.4k
8 votes

Is it a bad practice to use Singleton for DI in Asp.net rather than Scoped, Transient whenever possible?

Before you change whether a service is Transient, Scoped, or Singleton, do make sure you are not introducing bugs. When something is designed to be thrown away quickly, there might be assumptions ...
Berin Loritsch's user avatar
7 votes

C# extension methods design patterns and usage guidelines?

Extension methods are just syntactic sugar for ordinary static method calls. Extension methods make possible the ability to "spot-weld" methods onto existing types, without requiring ...
Robert Harvey's user avatar
6 votes
Accepted

In ASP.Net Core , how does one resolve circular references?

I suggest you to read a great book about the C# dependency injection by Mark Seemann, with a lot of real world samples for different tasks you can do with it, even with circular dependencies similar ...
VMAtm's user avatar
  • 178
6 votes

Should it be a claim, a role or a policy?

Essentially you are describing a mapping of Role to Permission. I think this is pretty much covered by the standard [Authorize(Role=xxx)] on your controller actions, where the implied permission is ...
Ewan's user avatar
  • 77.4k
6 votes
Accepted

Which Software Layer should hold Pagination Information?

Pagination is not a domain concern. The domain layer typically does not care to know the number of records fetched from a persistence store. It is a performance concern, specifically on the database ...
Subhash's user avatar
  • 1,601

Only top scored, non community-wiki answers of a minimum length are eligible