I have an application with three layers (Presentation, Business Logic, and Data Access). In the data access layer, I have an object called Unit
, and I have another object called Unit
in the business layer.
When Insert()
is called on the Unit
object in the business layer, it calls the Insert()
method on the corresponding object in the data access layer, and passes itself as the parameter. The problem I have is that the data access layer does not reference the business layer, and allowing it to do so 开发者_开发百科would cause circular dependencies.
Is my approach flawed? If so, what would be a good solution to my problem?
You're right when you state that the way you're doing it would require a bidirectional dependency between layers, and that's almost always a bad thing. The reason for this dependency is that your business logic layer takes on some of the responsibility of the persistence layer (by implementing Insert()
in the business logic layer).
It seems like you're mixing two incompatible concepts here.
First, you state that you have three layers in your code: presentation, business, and data access. The problem with this statement is that you also claim to be using an active record like pattern (unit.Insert()
). If you truly have a distinct domain (business) layer and persistence (data access) layer, then the domain objects would not know how to Insert()
.
Take a look at the repository pattern. This pattern is better suited for establishing a distinct persistence layer. If you use this pattern, you can define an "Entity" in the persistence layer, and map your Unit
object in the domain layer to the Unit
object in the persistence layer. AutoMapper should save you from the pain of manually mapping the domain model to the entity.
精彩评论