This project aims to discuss best practices for PKs of type int. We also extend this discussion to the auditable properties (properties CreateAt
e UpdateAt
).
Primary keys of type int generates a huge architectural issue:
- Whose responsibility is it to generate?
- It is good practice property
Id
as protected. What better way to set the value outside the domain layer? - If the responsibility of managing the PKs is for repository, how to ensure that two users will not generate the same PK?
Many of these issues also apply to objects auditable. For example the property UpdateAt
- Whose responsibility is it to generate?
- It is good practice property
UpdateAt
as protected. What better way to set the value outside the domain layer?
The solution is very simple consisting of 5 projects. They are:
- Domain
Contains interfaces and classes domain - Orm.NHibernate
Implements NHibernate repository - Repository
Abstraction repository - Test
Unit test - Orm.NHibernate.Test
Integration test for NHibernate
In the solution you will find some reviews that can be discussed. All can be seen in the TaskList
window:
For integration tests, we use the LocalDB. You can use GenerateIdDesignerProblem.Orm.NHibernate.Test.mdf
as the database, or if you prefer you can recreate it on your SGDB. Remember to set your App.config
in yours GenerateIdDesignerProblem.Orm.NHibernate.Test
project.
This question was raised in various places such as in StackOverflow, in Asp.net forum, in a discussion group .Net Architects(pt-br), was recorded a video showing the problem(pt-br) and in post on my blog(pt-br) about it.
Anyway, we are in git, feel free to use the Issues or/and send pull requests.