Skip to main content
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
42 votes
Accepted

Should Entity Framework 6 not be used with repository pattern?

To get this out of the way, I am a big proponent of Entity Framework, but it does come with some drawbacks that you need to be aware of. I also apologize for the long answer, but this is a very hot ...
Flater's user avatar
  • 52.7k
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
31 votes

Is it good practice to use entity objects as data transfer objects?

It is up to you. Most people will tell you that it's not a good practice but you can get away with it in some cases. EF never played nicely with DDD for multiple reasons, but two stand out: you can'...
devnull's user avatar
  • 3,055
22 votes
Accepted

Is unit of work pattern really needed with repository pattern

The Entity Framework DbContext is a unit of work, and its DbSet<T> properties are repositories. That's not to say that you should never roll your own layer on top of them, but you're correct in ...
Flater's user avatar
  • 52.7k
21 votes
Accepted

How to avoid incrementing ID's being exposed in an API?

The advice at the post you linked about using GUIDs for clustered primary keys is seven years old and almost certainly bad advice today. The article it refers to references SQL Server 7. We use ...
Robert Harvey's user avatar
20 votes
Accepted

Pitfalls of Domain Driven Design with Entity Framework

DDD and EF have little to nothing to do with each other. DDD is a modeling concept. It means to think about the Domain, the Business Requirements, and model those. Especially in the context of object-...
Robert Bräutigam's user avatar
19 votes
Accepted

Repository Pattern and Joined Queries

Main responsibility of Repository pattern to abstract actual database from domain codebase. When you have one repository per entity you will leak database implementation details back to the domain ...
Fabio's user avatar
  • 3,136
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

How to avoid incrementing ID's being exposed in an API?

Looking at posts such as this on StackOverflow, the consensus seems to be that you should hide incrementing ID's from external API's for security. Whenever you're talking about security, your first ...
jpmc26's user avatar
  • 5,419
12 votes

Is it good practice to use entity objects as data transfer objects?

No, it is not. Ideally, DTOs will match your persistence repositories (aka, your database tables). But your business classes are not necessarily a match. You might need additional classes , or ...
Juan Carlos Eduardo Romaina Ac's user avatar
12 votes

Is unit of work pattern really needed with repository pattern

The Unit-Of-Work pattern makes sense when you have a complex use case with several objects involved, often objects which map to different master-detail tables. As part of the use case, you want to ...
Doc Brown's user avatar
  • 210k
11 votes
Accepted

Entity Framework and avoiding the Anemic Domain Model

The issue is, you are using EF objects as domain objects in the first place. EF objects are data models NOT business models. You need to declare business objects which give you the freedom to do as ...
TheCatWhisperer's user avatar
10 votes

Pitfalls of Domain Driven Design with Entity Framework

Treat EF for what it is i.e. data access library which is only slightly more strongly-typed than raw ADO.NET. I wouldn't recommend to model your domain using EF entity classes just like I wouldn't ...
KolA's user avatar
  • 615
9 votes

Is it good practice to use entity objects as data transfer objects?

No, it's a bad practice. Some reasons: New entity fields will be in the Dto, by default. Use the entity means that every information will be available to be consumed by default. This can lead you to ...
Dherik's user avatar
  • 2,456
8 votes
Accepted

Is it OK to create and drop databases during unit tests

The first thing to note is that, if you are creating and dropping DBs as part of your tests, then they aren't unit tests; they're better described as integration tests. Pedantic point aside, then the ...
David Arno's user avatar
  • 39.4k
8 votes

Generic repository pattern +EF and unit of work

It should satisfy SOLID principles A laudable goal, but does it help you satisfy any of your software's functional and non-functional requirements? The purpose of the SOLID principles is to help you ...
Robert Harvey's user avatar
8 votes
Accepted

Managing Database Access for MicroServices

In general, the process to migrate a monolith to a microservice architecture involves carefully extracting parts from the monolith so they can live as stand-alone applications. For example, let's ...
Vincent Savard's user avatar
8 votes

Avoiding Repository pattern - implementing Onion Architecture with DbContext only

The core argument for using repositories is to prevent leaking EF dependent code into your domain. That argument is not wrong, it just comes with a steep cost, i.e. a high-complexity uow/repo layer, ...
Flater's user avatar
  • 52.7k
7 votes
Accepted

Entity Framework Code First, C# class separation and EAV

First, let's rectify a couple of misconceptions. Joins are expensive As compared to what? Of course, reading one flat table is "cheaper" than joining tables, but any mature RDBMS is highly ...
Gert Arnold's user avatar
7 votes

Is it good practice to use entity objects as data transfer objects?

It is actually a very bad idea. Martin Fowler has an article about Local DTOs. Long story short, DTO Pattern was used for transfering data outside the process, for example over the wire and not ...
Constantin Galbenu's user avatar
7 votes

Pitfalls of Domain Driven Design with Entity Framework

Entity Framework brings UoW & Repository (DbSet) out of the box No. Entity Framework abstractions were built with ORM, not DDD, in mind. The DbSet abstraction in any version of Entity Framework ...
guillaume31's user avatar
  • 8,633
6 votes
Accepted

Determining the best way(s) of adding unit tests to a large project that makes good use of stored procedures

A stored procedure and the C# code that uses it are different units, so a single unit test can not test them both. You'll need separate unit tests: Unit tests that mocks the stored procedure and ...
Idan Arye's user avatar
  • 12.1k
6 votes
Accepted

How to incorporate external entity models in conceptual model?

IMHO you should model those objects to exactly the degree you need them in your application, no less, no more. For example, don't model all available attributes, just model the attributes which are ...
Doc Brown's user avatar
  • 210k
6 votes

Is it good practice to have a unit test for a simple data method?

The decision on whether or not to unit test a method lies in the answer to the question "Are you confident that the method works the way you expect it to?" If the answer is yes, then no further unit ...
Robert Harvey's user avatar
6 votes
Accepted

Is it good practice to have a unit test for a simple data method?

Ultimately tests are a tool. Like all tools, there's a cost, and a possible benefit. In this case, the test has a high cost (the test is dictating implementation details and will break if the ...
Dogs's user avatar
  • 1,176
6 votes

Deleting each row and re-adding when saving to database

I always use the delete all and re-add approach. Usually its quicker to just delete and re add than apply the logic that's required to update. This would probably require selects, locking while you ...
Ewan's user avatar
  • 77.4k
6 votes
Accepted

Posts and Comments - how should I define them in domain driven design?

So I think that Comment model can also be the aggregate root and should contain a related post. You are conflating aggregate roots with DTOs, and these are two very different things. An aggregate ...
Flater's user avatar
  • 52.7k
5 votes

Which is more secure for the DB, Web API or MVC with repository pattern & EF?

Web API and MVC a for all practical purposes the same. A Web API interface is a subclass of an MVC Controller. The only difference is instead of returning a rendered template from a Web API controller,...
Michael Brown's user avatar
5 votes
Accepted

Using Entity Framework as substitute for SSIS, SSAS and SSRS

It would work but it is using the wrong tools for the job. The whole process here is DB-centric, so use the DB-centric tools. SSIS will be able to reads your datasources and update your data warehouse....
gbjbaanb's user avatar
  • 48.6k

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