I am writing a Java DDD application in which the database model is already designed and implemented. The problem i开发者_开发问答s that my domain objects differs from the database model and the the ORM mapping is too complex. Here is the question: What can I do with that? DTO? How can I associate DTO with repositories and Domain Objects, if they see the repositories interfaces?
Thanks!
Hibernate has very good support for legacy databases. It goes well beyond class=table mapping. The complexity that you are referring to would not go away if you use additional DTO layer, it will just be spread over one more layer. It may be simpler to just contain it in mapping files. It might sense to bend model a bit to database schema, but only if you will see significant benefits in terms of reducing overall complexity. And then refactor domain model later, along with database.
Many ORMs lack in implementing good enough mapping capabilities. Moreover, they introduce conceptual shortcuts that can prevent the solution to align itself on the business. Your case is a good example.
In your case, I would probably challenge the use of an ORM. I would implement the business model without trying to reuse the existing database model, with POCOs, Domain Services... For persistence, given the fact you have 2 differenciating models, I would manage the code with a Dependency Injection of the Domain Model into the Data Access Layer, to prevent the storage model to pollute your domain, and then, in the DAL, implement my own mapping with procedural code and a micro-ORM (or a ORM if the storage model is complicated). It represents more work, but you will get a far better Domain.
精彩评论