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 ...
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 ...
39
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 ...
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'...
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 ...
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 ...
18
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 ...
17
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-...
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
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 ...
12
votes
Accepted
Are Repositories needed any longer in ASP.net 5 & EF7?
If you're adding methods to a repository like
GetById
GetByName
GetWithIncludesABC
GetWithIncludes123
Then you're better off moving to a Service Layer, and letting the Service Layer use EF ...
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 ...
11
votes
If Repository Pattern is overkill for modern ORMs (EF, nHibernate), what is a better abstraction?
I think you are conflating repositories and generic repositories.
A basic repository just interfaces your data store and provides methods to return the data
IRepository {
List<Data> ...
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 ...
11
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
7
votes
Accepted
Mapping Domain models to ViewModel
Keep mapping implementation out of your controllers. Instead, call the thing responsible for mapping in your controller.
public ActionResult FetchSomething()
{
var thingMapper = new ThingMapper();...
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 ...
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 ...
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 ...
7
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, ...
6
votes
If Repository Pattern is overkill for modern ORMs (EF, nHibernate), what is a better abstraction?
Most of the arguments you mention wrongly attribute to the Repository pattern features that it doesn't have.
Conceptually, a Repository as originally defined in DDD is just a collection of objects ...
6
votes
Should Repositories return IQueryable?
I've faced such choice as well.
So, let's summarize positive and negative sides:
Positives:
Flexibility. It's definitely flexible and convenient to allow client side to build custom queries with ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
entity-framework × 375c# × 165
asp.net-mvc × 48
.net × 40
architecture × 34
orm × 31
design-patterns × 30
domain-driven-design × 29
asp.net × 29
sql-server × 28
database × 26
design × 19
database-design × 19
asp.net-core × 19
web-api × 14
repository-pattern × 14
repository × 13
linq × 13
codefirst × 12
mvc × 11
sql × 11
object-oriented × 10
unit-of-work × 10
mvvm × 9
unit-testing × 8