我应该如何在 .NET 中安排我的项目/类以避免循环依赖
我的程序正在尝试在 C# 和 C# 中绘制 语法 WPF。我有:
1 个 DataStructure 项目,它描述了如何可视化的树结构。树中的每个节点都与语法中的一个符号相关。顶部的节点定义该非终结符号的规则。
1 Drawer 项目,描述 WPF 中的用户控件。
我需要在数据结构中引用抽屉,因为当我遍历树时,我在每个节点上调用 DataStructure.draw(); 。我还需要引用抽屉项目中的数据结构,以便我可以响应用户单击我的 GUI,它将更新数据结构。
这会产生循环依赖,我尝试使用控制器类,但我不知道:/
My program is attempting to draw grammars in C# & WPF. I have:
1 DataStructure project which describes a tree structure of how it should be visualised. Each node in the tree relates to a symbol in the grammar. Nodes at the top define the rule for that nonterminal symbol.
1 Drawer project which describes the user controls in WPF.
I need to reference drawer in my datastructure as when i traverse the tree, I call DataStructure.draw(); on each node. I also need to reference the datastructure in my drawer project so I can respond to a user clicking on my GUI, it will update the data structure.
This creates a circular depedency, I have tried to use a controller class but I have no idea :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看此处描述的访客模式:
http://en.wikipedia.org/wiki/Visitor_pattern
这将允许您的数据结构项目接受任何类型的访问者,包括抽屉,但实际访问者的实现(在您的情况下是绘图逻辑)可以单独存在,而不会像您不那样依赖想。
You should look at the Visitor pattern, described here:
http://en.wikipedia.org/wiki/Visitor_pattern
This will allow your datastructure project to accept a visitor of any type, including a drawer, but the implementatin of the actual visitor (in your case the drawing logic) to live separately with no dependency in the way you don't want.
快速修复:遍历并调用
HelperObject.Draw(DataStructure);
您的 DataStructure 不应该知道如何绘制自身,假装它必须在 WinForms 或其他平台中重复使用。 HelperObject 可以是 GUI 的一部分。
Quick fix: traverse and call
HelperObject.Draw(DataStructure);
Your DataStructure shouldn't know how to Draw itself, pretend it has to be re-used in a WinForms or other platform. HelperObject could be part of the GUI.
从对象中提取通用功能,直到第三层抽象。您可能还想查看 MVC您的语法将是模型,您的抽屉将是视图,并且您仍然需要控制器。
Extract common functionality out of your objects until a third layer of abstraction. You may also want to look at MVC where your grammer would be the Model, your drawer would be the View, and you still need the controller.