简体   繁体   English

如何在层之间进行n层应用程序的通信?

[英]How should communication be done n-Tier applications between layers?

Recently I have been debating on the best way to handle communication up the chain in n-tier architecture. 最近,我一直在讨论在n层架构中处理链路通信的最佳方法。

Currently the methods I am doing is throwing and handling exceptions between layers for errors, and using events/delegates for other communication (to update progress bars and such). 目前我正在做的方法是抛出和处理层之间的异常以获取错误,并使用事件/委托进行其他通信(更新进度条等)。 Are these the best way or is there another method that I have overlooked that would be considered better practice? 这些是最好的方法还是我忽略了另一种被认为是更好的做法的方法?

I would say you are on the right track as far the exception handling is done - that is the Chain of Responsibility pattern implementation. 我会说你在异常处理方面已经走上正轨 - 这就是责任链模式的实现。 It is always good to throw the exception up the chain. 将异常抛到链上总是好的。 As for the other one (events/delegates) I didnt quite understand your statement so couldnt comment on that. 至于另一个(事件/代表),我不太明白你的陈述,所以无法对此发表评论。

Exceptions are indeed a good way to handle errors from lower tiers. 例外确实是处理较低层错误的好方法。

In my mind, delegates are most useful when one object that owns another object needs to customize that object. 在我看来,当拥有另一个对象的一个​​对象需要自定义该对象时,委托最有用。 That could make sense if your tiers logically "own" the objects on lower tiers—otherwise, I'd probably shy away from the delegate pattern and use events. 如果你的层在逻辑上“拥有”较低层上的对象,那么这可能是有意义的 - 否则,我可能会回避委托模式并使用事件。

I have tried to use the Adapter pattern. 我试过使用适配器模式。 The main objects lie in the logic layer and are wrapped, using composite objects so that the presentation and data access layers may work. 主要对象位于逻辑层中,并使用复合对象进行包装,以便表示和数据访问层可以工作。 Most of the presentation and data access layer classes use interfaces. 大多数表示和数据访问层类都使用接口。 The wrapper (composite) objects lie in the presentation and data access layers and implement the interfaces of those layers. 包装器(复合)对象位于表示层和数据访问层中,并实现这些层的接口。

In addition to composite objects, there are controller objects which may pull data from the logic objects and create new objects (ie a list of strings). 除了复合对象之外,还有控制器对象可以从逻辑对象中提取数据并创建新对象(即字符串列表)。

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

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