This is almost getting me crazy. EF bring us a very convenience experience in development.
However, if we need to use the Entity model in the upper layer, we always put a reference from the upper layer to DAL or some layer at the lower which is holding the Entity class. I have visualize the situation.
+-------------------+
| |
| Presentation |
(This layer is directly referring to DAL, which break the 3-tier architecture rules)
+-------------------+
|
|
|
|
+---------v---------+
| |
| BLL |
(Function that returning Entity Model)
+-------------------+
|
|
|
|
+---------v---------+
| |
| DAL |
(Repo & Entity Model)
+-------------------+
Implement something like DTO at BLL is quite heavy and cannot solve this issue completely. So I may not try to implement DTO at such layer.
In the meantime, I found that some project may take out those Entity Model like the following diagram. Is it a good way to solve the problem?
+-------------------+
| |
+------------------+ Presentation |
| | |
| +---------+---------+
| |
| |
| |
| |
+-----v------+ +---------v---------+
| | | |
| Entities <-----------+ BLL |
| | (Function that returning Entity Model)
+-----^------+ +---------+---------+
| |
| |
| |
| |
| +---------v---------+
| | |
+------------------+ DAL |
| (Repositories) |
+-------------------+
Although this design can solve some of the problem, developers can attempt to update the entity to DB from Presentation Layer by calling entity.SaveChanges(). Which is so dangerous. How can I prevent other developer from calling SaveChanges() at the upper layer?