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
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 ...
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
  • 72.4k
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
  • 43.6k
26 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,438
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

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
  • 203k
12 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
  • 134k
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
  • 47.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

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

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

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
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.5k
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
  • 414
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,248
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.1k
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
7 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
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

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
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
  • 72.4k
6 votes
Accepted

What should we do in face of a failing sub in pub-sub?

Having 5 subscribers for one event seems only appropriate if all 5 actions are independent, in that the failure of any one doesn't affect the others. If the failure of one, A, affects another, B, then ...
Erik Eidt's user avatar
  • 33.6k
6 votes

Microservice arcitecture - seperation of services

Microservices are a tool for organising development teams and the independent maintenance of features. Splitting a database into one microservice per table will ruin performance for no measurable gain....
pjc50's user avatar
  • 12.4k
6 votes

In .net 6 dependency injection and Program.cs, what is the best practice for handling a large number of dependencies?

A software with this many dependencies is probably structured in modules or at least the classes can be grouped in some way. For example, you may have some data access layer, some frontend, and maybe ...
pschill's user avatar
  • 1,807
5 votes
Accepted

How to choose between a library and shared project?

In my view there is rarely a good case for a shared project. The main reason is versioning and change control. If you recompile source code, then strictly speaking you have a new version. Its not so ...
Ewan's user avatar
  • 72.4k
5 votes

Ambient dependency injection through static service locator

Are there any other objections? Are there any legitimate, real-world reasons why DI with a static/global service locator would be bad? Ugh, yes. Statics/globals are horrible. They assume that the ...
Telastyn's user avatar
  • 109k

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