简体   繁体   English

mapper层可以使用业务层吗?

[英]Can mapper layer use business layer?

In my n-tier architecture, I have a mapper layer to fill my responsecontext from the dataset/datatable comes from DBlayer. 在我的n层架构中,我有一个mapper层来填充来自DBlayer的数据集/数据表的responsecontext。 So DBlayer calls Mapperlayer to get the things done. 所以DBlayer调用Mapperlayer来完成工作。 But before filling responsecontext i need to do some validation. 但在填写responsecontext之前,我需要做一些验证。 For this, shall i call businesslayer from mapperlayer? 为此,我应该从mapperlayer调用businesslayer吗? Or i should just pass what i get from DBlayer to businesslayer and ask the businesslayer to call mapperlayer. 或者我应该将我从DBlayer获得的内容传递给businesslayer并要求businesslayer调用mapperlayer。

Here, My business layer will use Business Entities to do thier own operation. 在这里,我的业务层将使用业务实体来执行自己的操作。 Kindly help me on to sort of this design decision. 请帮助我做出这个设计决定。

Either one will "work", but I prefer not putting such things in a business object. 任何一个都会“工作”,但我不希望将这些东西放在业务对象中。

Having the business layer call the mapper layer creates a circular dependency between packages. 让业务层调用映射器层会在包之间创建循环依赖关系。 The mapper layer already has to know about the business layer - that is the heart of its responsibility. 映射器层已经必须知道业务层 - 这是其职责的核心。

But why should the business layer know about the mapper? 但为什么业务层应该知道映射器呢? Mapping has nothing to do with business logic. 映射与业务逻辑无关。 The harm, of course, is that you can never test them independently if there's a two-way dependency. 当然,这种伤害是,如果存在双向依赖性,您永远不能独立测试它们。

I'd keep it as a one-way dependency. 我会把它作为一种单向依赖。 You can test the business objects independent from the mapping objects that way. 您可以通过这种方式独立于映射对象测试业务对象。

I'd also keep the mapper inside the persistence tier, not situated between persistence and model. 我还保持持久层的映射,而不是位于持久性和模型之间。 Have your persistence layer return business objects, not ResultSets. 让持久层返回业务对象,而不是ResultSets。 Let it be package private if possible. 如果可能的话,让它成为私人包。 My reasoning is the same - minimum knowledge means minimal coupling. 我的推理是一样的 - 最小的知识意味着最小的耦合。

There are different levels of validation. 有不同级别的验证。 I'd say that data should have been validated long before it gets into your database. 我说数据应该在进入数据库之前很久就已经过验证。 There should be no need to validate, except for specific business rules, when you query the database. 在查询数据库时,除了特定的业务规则之外,不需要验证。 Those rules should be part of the business transaction in the service layer, not the persistence layer. 这些规则应该是服务层中业务事务的一部分,而不是持久层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM