现在我的想法是编写包含所有必要字段和属性的DTO,但没有逻辑(只有状态).
然后我将从这些DTO派生我的业务对象,使用我的业务逻辑扩展它们,处理DTO基类属性.这些对象也将是所使用的ORM中持久存在的对象(NHibernate).
使用这种方法,在服务器端,我可以处理业务对象并将它们直接传递给客户端(它们是派生的,因此可以向下转换).我不会被迫以这种方式暴露我的业务逻辑并节省大量代码.
你认为这种做法是明智的吗?
问候,
塞巴斯蒂安
您可能需要考虑以下事项:“…,
because keeping the DTO unaware of the
domain objects enables you to reuse
the DTO in different contexts.
Likewise, you do not want the domain
objects to know about the DTO because
that may mean that changing the DTO
would require changing code in the
domain logic, which would lead to a
maintenance nightmare.The best solution is to use the Assembler pattern, which creates DTOs from business objects and vice versa. Assembler is a specialized instance of the Mapper pattern also mentioned in Patterns of Enterprise Application Architecture….”
从Pattern and Practice: Data Transfer Object
另外,我自己没有使用它,但你也可以查看AutoMapper.