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 ...
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 ...
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 ...
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 ...
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-...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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'...
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 ...
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 ...
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....
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
asp.net-core × 200c# × 92
.net × 26
web-api × 24
.net-core × 24
asp.net-mvc × 23
asp.net × 20
dependency-injection × 19
entity-framework × 19
architecture × 17
domain-driven-design × 17
design-patterns × 14
design × 12
mvc × 12
api-design × 11
rest × 10
microservices × 9
authentication × 9
api × 7
azure × 7
repository-pattern × 7
asp.net-mvc-web-api × 7
cqrs × 6
database × 5
sql-server × 5