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
44 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
  • 59.5k
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
  • 49.4k
22 votes
Accepted

Is it an anti-pattern to create ORM entities based on existing database schema?

Reverse engineering was created for precisely your kind of situation. It was meant to be a time-saving tool when using an ORM with an existing database. The tool can do in a few minutes what a human (...
Greg Burghardt's user avatar
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
9 votes
Accepted

How to write dynamic (non ORM) repositories that can return only the necessary data without creating many methods or data-objecs?

It would be wasteful to always fetch the entire objects. First I would question this. If your objects are well-designed and not too bloated, the performance and memory overhead of fetching them ...
Doc Brown's user avatar
  • 220k
7 votes
Accepted

Routing all SQL queries through a single micro-service

What would be the drawbacks of such an architecture? Single point of failureness. Coupling that microservice to all others, hindering deployments and versioning. That DB access service is going to be ...
Telastyn's user avatar
  • 110k
6 votes

Unifying programming and database query

This is my opinion. While I do see where are you coming from, I just can't see it happening from design perspective. Data persistence is extremely complex subject. And so are programming languages. ...
Euphoric's user avatar
  • 38.2k
6 votes
Accepted

Object that can set its own subclass/amend its methods with subclass?

Clients have different kinds of contracts, their data format remains the same, however, the underlying dynamics are different. According to this statement, you implement the single table inheritance ...
Christophe's user avatar
  • 82.1k
6 votes

Is it an anti-pattern to create ORM entities based on existing database schema?

Both patterns are supported. Code first versus Model first. If you have an opiniated database administrator(s) or strong SQL skills one may choose Model first. If one has less SQL expertise, ...
Jon Raynor's user avatar
  • 11.8k
5 votes

Advice on AggregateRoot boundaries

There are two different aspects in your question: where should the boundary be in your domain model? and how to implement your model? The boundaries in the model The definition of an aggregate is: A ...
Christophe's user avatar
  • 82.1k
5 votes

Unifying programming and database query

Yes (not me). It was called MUMPS. According to this this former SE.SE question, or this article, MUMPs was not very well designed. But is was indeed used in the health industry (and I guess there ...
Doc Brown's user avatar
  • 220k
5 votes

Unifying programming and database query

It seems like you're making some major assumptions. For example, you're assuming that everyone is writing to relational databases. That's simply not the case, there are lots of examples of databases ...
Paul's user avatar
  • 3,347
5 votes

Unifying programming and database query

There are indeed multiple systems which unify database and programming language into a single environments. Smalltalk is probably the closest to what you describe. Object in memory are persisted in ...
JacquesB's user avatar
  • 62.3k
5 votes
Accepted

Should I check if something exists in the db and fail fast or wait for db exception

Rather a confused question, but YES you should check first and not just handle a DB exception. First of all, in your example you are at the data layer, using EF directly on the database to run SQL. ...
Ewan's user avatar
  • 84.4k
5 votes
Accepted

When using an ORM when should I sacrifice performance for convenience?

The way you make this decision is by optimizing when you need to. There are many scenarios (e.g. a single-record data-entry form) where pulling all of the fields is perfectly acceptable, since you're ...
Robert Harvey's user avatar
5 votes
Accepted

Encapsulation and input validation duplication

Could you tell me please, is the validation in Basket.ChangeItemQuantity redundant? Yes it's redundant. But that's the least of your problems. Semantically it's weird for an Item to know how many of ...
candied_orange's user avatar
5 votes
Accepted

Any solid reason to have getters/setters in entities? ORM, Doctrine

I used to eschew getters and setters, I would "Pah" those who put forward the reasons why they are better for the kind of reasons you suggest. Now I use them all the time without a second thought. ...
Ewan's user avatar
  • 84.4k
4 votes

The Open/Closed Principle, how does it work for adding entities?

It's not your changes that told the open/closed principle to go to hell. It was the design. OCP says it's a better design that allows change to come, not from rewriting code, but from adding new code....
candied_orange's user avatar
4 votes

Best practice for object relation mapping to execute delete/update

My question here this, for the purpose of convenience and ease of maintenance, is it worth to follow ORM pattern and ignore the performance trade off? It depends, mainly on the following things: does ...
Doc Brown's user avatar
  • 220k
4 votes

How do I approach structuring when to read/write objects to db?

You can take a look at Unit of work: [A unit of work] maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency ...
Armando Garza's user avatar
4 votes

Should Entity Framework 6 not be used with repository pattern?

Entity framework is not a full abstraction, only a partial one. It only abstracts away the querying of the data. For example, what if you have an Order entity, but as your application grows, it ...
TheCatWhisperer's user avatar
4 votes
Accepted

Maintaining parallel libraries: Binary files access + Metadata Database ORM design

Take a step back. If there is something in common between the two API's then they are discussing the same thing. You can extract that sameness into a library which is expanded on by the ...
Kain0_0's user avatar
  • 16.6k
4 votes
Accepted

Select from many-to-many in one query

Yes. You can issue a query that returns customer_name | customer_id | item_id | item_name --------------|-------------|---------|---------- bob | 0 | 0 | chair bob |...
Caleth's user avatar
  • 12.4k
4 votes
Accepted

Using orm events or database triggers

It is an architectural choice. Either you do it in the ORM. But the you should make sure that the relevant database objects are only accessed outside of your code via a service that also uses the ORM. ...
Christophe's user avatar
  • 82.1k
4 votes

Is it an anti-pattern to create ORM entities based on existing database schema?

Neither is an anti pattern, they are just two different mindsets regarding database integration: The application centric: The database is just a service for persisting application state. The role of ...
JacquesB's user avatar
  • 62.3k
4 votes

Is it an anti-pattern to create ORM entities based on existing database schema?

Who "owns" the database? Is it your app? Or some other business? Do you have a continuity plan to support the old app while you develop new one? Unless the database is for your app only, and ...
Thomas's user avatar
  • 49
3 votes

Database modeling. Many to Many relationships. Link tables

Let us ignore the fact your solution 2 is not semantically equivalent to the first one (since it will allow only to model the status interested for a user who has a subscription for that particular ...
Doc Brown's user avatar
  • 220k
3 votes

The Open/Closed Principle, how does it work for adding entities?

I think you've misunderstood the purpose of the open/closed principle (which I'm going to abbreviate OCP for the rest of this). The OCP doesn't mean that if you ever have to add an entity that no ...
Becuzz's user avatar
  • 4,865
3 votes

How can I model unknown and an unknown number of attributes on an object?

Django can store JSON directly in the database with JSONField. It is supported in some databases only, but https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#jsonfield and http://...
btilly's user avatar
  • 18.4k

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