English 中文(简体)
Is a guid as identity field better in domain-driven design?
原标题:

Is it easier to implement domain-driven design when using guids as identity fields instead of auto incrementing integers? With guids you don t have to jump to the database to get the actual value.

最佳回答

Well, GUIDs are easy and look like the best fit. They re tempting to frontend programmers, since they don t have to deal with the database.

On the other hand, when looking at the potential drawbacks they have when used without thinking about database issues too much, I would warn against them as much as possible.

The question really is: do you really need to know the ID of the entity before it s stored in the databsae? REALLY? WHY?

If you do decide to go with GUIDs in the end, and if you re using SQL Server as your database backend (I don t know enough about other RDBMS to make an informed suggestion), I would strongly recommend you make absolutely sure that the GUIDs are not being used as the clustering key on the tables. This will kill your performance - no doubt.

If you do use GUIDs as your primary key, make sure to use something else, some other column that wrecks less havoc on your database, as your clustering key instead - a INT IDENTITY would be my first choice.

Check out these articles by Kimberly Tripp on why a GUID is definitely not a good idea as a clustering key in a SQL Server database - she s the ultimate guru when it comes to indexing and performance issues with indexing and she can makes those points much better than I ever could:

Marc

问题回答

One of the core tenets of DDD is Persistence Ignorance. So yes, GUIDs are the easiest way to provide your objects with unique identity without having to rely on a persistence store.

NB: If you re concerned about database performance using GUIDs, consider using COMBs (specifically for SQL Server index fragmentation)

I recommend Guids as there is no confusion as to what it is you are looking at. Also, and I know this gets tossed around as a joke a lot, but I had to debug an issue that happened in the system where it was looking for uint s instead of guids. This caused a template in sharepoint to be deactivated and gave us no way to reactivate it. Took 2 days to discover what the underlying issue was. So to recap go with Guid s.

The only advantage I see to GUIDs over an incrementing integer is decentralization of identity creation. That is, the incrementing integer requires an atomic increment and read of a shared value, while GUIDs can be created independently with little fear of collision.

As for your suggestion that GUIDs allow one to dereference an entity without consulting the database, I don t see how that s the case, absent some other information not mentioned in your question. Your choice here is one of key type, not whether or not to use a key. If you have a key in hand, and the key maps to a value or entity by way of the database, you need to consult the database to dereference the key.

I use guid s for two reasons:

  • The ID is unique not only within the context where it is created, so ID s for the same type of data can be generated in different locations. This is good in cases where you have several installations distributed geographically that interchange data with each other, or in disconnected scenarios.
  • It counter-acts the urge to treat the ID in logical ways, but rather enforces developers to regard the value as "just and ID".

However, I would not necessarily say that these arguments are valid only for domain-driven design.

No, definitely not. A GUID is an implementation detail that is not supposed to leak into your domain. You need an identity value object, and I don t care how you implement it.





相关问题
DDD - Returning entity in response to a service operation

I am new to domain driven development & have a simple question. If a service needs to generate some entity as a response to an operation then how should it be done? One of the ways is to inject ...

Domain Driven Design efforts in dynamic languages? [closed]

Are you aware of any DDD efforts in a dynamic language ? Practical resources on DDD tend to decrease quite dramatically when straying from enterprise-oriented solutions (a google search exluding C#, ....

Accessing domain objects in the view

If I don t want to expose the internal state of my Domain Objects, but I need to display them, I can think of three approaches. Which of these is the most "correct" (if any?). The "DTO/ViewModel ...

DDD screen cast question?

I wathced a screen cast on DDD by Greg Young the other day which spoke about persisting all state transitions of an object, instead of it s state when saved, then to load it "replay" all these ...

How to fluent-map this (using fluent nhibernate)?

I have two tables in my database "Styles" and "BannedStyles". They have a reference via the ItemNo. Now styles can be banned per store. So if style x is banned at store Y then its very possible that ...

热门标签