douxie5930 2014-03-14 14:15
浏览 42
已采纳

循环依赖,好还是坏? [关闭]

I'm making some sort of personal framework, for educational and practical purposes, and encountered a design problem i'd like to share.

I have a Component namespace, that contains various classes for, um, components (i. e. bits of reusable ViewModels). One of them is Component\Form, a generator class for building form templates from models. After some time i realized a need for some Validation service classes, that will perform non-model specific validations, like checking captcha.

The problem is, my default form validation accumulates error messages and displays them in one place. So, to accomplish this, my Validator classes need to have access to the Form, and Form class adds Validator classes via setter injection.

So, this creates circular dependency. From one side, there's an (although optional) dependency of Form on Validator classes, and from other side, i have to inject a Form class into every Validator i add. I wonder, if this approach is fine from the point of OOP design, and if not, what problems could possibly arise from that, and how could i possibly resolve it.

I'm sorry i don't provide any relevant code, i think it would take too much space to be readable, but if necessary, i'll gladly post anything to clarify specific moments of the problem.

  • 写回答

2条回答 默认 最新

  • dongzhi1851 2014-03-14 14:22
    关注

    I think the validator should not have a dependency on the form, because it should not display an error. The form can ask the validator to check some raw input against some rule. If the validation fails, the validator might return an error code or message, based on which the form can display an error. Displaying that error is not the job of the validator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名