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 ...
user avatar
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 ...
user avatar
  • 38.2k
34 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 ...
user avatar
  • 38.3k
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'...
user avatar
  • 2,894
24 votes
Accepted

Should an ASP.NET MVC application directly use Entity Framework as the model?

In my applications I have always separated things out, with different models for the database (Entity Framework) and MVC. I have separated these out into different projects too: Example.Entities - ...
user avatar
  • 1,374
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 ...
user avatar
17 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 ...
user avatar
  • 3,016
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 ...
user avatar
  • 309
15 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-...
user avatar
14 votes

Separating ASP.NET IdentityUser from my other entities

You can create a User class that has nothing to do with ASP.NET Identity in your core library. public class User { public Guid UserId { get; set; } public string UserName { get; set; } ...
user avatar
  • 339
14 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 ...
user avatar
  • 38.3k
12 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> ...
user avatar
  • 65k
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 ...
user avatar
12 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 ...
user avatar
  • 5,269
11 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 ...
user avatar
10 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 ...
user avatar
9 votes
Accepted

How do you guarantee that your database context is disposed of properly when your lazy collection is no longer needed?

In general, you don't need to use Using statements with Entity Framework data contexts. Lazy collections is one of the reasons why. So your code would simply be: <HttpGet> Public Function ...
user avatar
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 ...
user avatar
  • 2,267
9 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 ...
user avatar
  • 545
8 votes
Accepted

Can I update an attached object using a detached but equal object?

I didn't find what I was hoping for but I did find an improvement over the existing select-detach-update-attach sequence. The extension method AddOrUpdate(this DbSet) allows you to do exactly what I ...
user avatar
8 votes
Accepted

Why does CI stipulate that we should be able to rollback to any version of a software?

In short: Since you don't know whether a build contains a critical bug at the time of deployment, you may need to roll back more than one version when you're deploying at a relatively high frequency. ...
user avatar
  • 1,014
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 ...
user avatar
  • 38.3k
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 ...
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 ...
user avatar
7 votes
Accepted

Entity Framework Code First, C# class separation and EAV

(Late to the party, but I couldn't resist) Let's straighten out some misconceptions. Joins are expensive As compared to what? Of course, reading one flat table is "cheaper" than joining tables, ...
user avatar
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 ...
user avatar
6 votes

Should Repositories return IQueryable?

From a pure architecture standpoint, IQueryable is a leaky abstraction. As a generality, it provides the user with too much power. That being said, there are places where it makes sense. If you are ...
user avatar
6 votes

Should an ASP.NET MVC application directly use Entity Framework as the model?

I would say it really depends on your application. Is it just doing pure CRUD, without any business logic? Then I would use EF models directly in my views. Most of the time there is at least some ...
user avatar
  • 126
6 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();...
user avatar
  • 9,028
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 ...
user avatar
  • 8,010

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