SCSF. Chapter 1. B. Solution Architecture: Loose Coupling with CAB
We can solve these architectural problems by developing an application from loosely 松散的 coupled 耦合的 parts. Instead of compiling everything together into one giant 巨大的 .exe file, we can build the parts more or less separately and stitch 缝 them together ("Compose 组合 them") at runtime using services provided by the Composite UI Application Block (CAB).
我们可以通过开发松耦合部件来解决这些架构问题。不是把所有东西都编译成一个巨大的.exe文件,而是独立构建这些部件,并且通过使用CAB提供的服务在运行时组合它们("Compose them")。
This approach allows our application to be based on the concept of modules or plug-ins. Because of loose coupling, there are fewer interactions between the parts, making our application not only easier to develop initially 最初, but also easier to extend and maintain 维护 than a classic monolithic 整体的 application. It also allows development teams to work independently—for example, the user interface specialists concentrating 专心 on presentation and the business logic teams on business logic, as shown in Figure 1-3.
这种方法允许我们的 application 建立在 modules 或者 plug-ins 的概念基础上。由于松耦合,减少了部件之间的相互影响,不仅首先使得我们开发更加简单,而且比整体 application 更加简单地扩展和维护。同时允许开发小组独立工作,例如,UI专家专注于表示层,而业务小组专注于业务层。
Figure 1-3. An application developed from loosely coupled parts.

We've seen this sort of architecture before in (any number of) 许多 modular designs. The key improvement that CAB provides is a prefabricated 预制构件的 infrastructure 基础设施 that supports this loose coupling. For example, instead of having to know at compile time which class is responsible for firing an event (announcing, say, the user has selected a patient from a list or a search), CAB allows us to easily write code that says, "Whenever the event with the name Patient Selected is fired, no matter who does it, call this response function to tell me about it because I care." Or "Whichever object is in charge of implementing the interface IWhatever, give me a reference to it because I need it to get my work done." Or "Take this control and show it in the display windows whose name is [this] so the user can see it, please." CAB makes extensive 广泛的 internal use of .NET Reflection to provide these capabilities 功能. The services that CAB provides for accomplishing 完成 these tasks are listed in Table 1-1, along with the lesson in which each is described.
在许多模块设计中我们在早已见过这套架构,但CAB的主要改进是预制构件的infrastructure支持松耦合。例如,不是需要在编译时知道那个类是负责触发事件(),CAB允许我们更简单的编码:每当选中病人名字的事件触发了,不管谁做了这个动作,可以通过访问响应函数通知我。又或者,无论哪个对象负责实现IWhatever接口,给我一个这个接口的引用地址。又或者,只是给一个控件名称,则可以在显示窗口中看到它的实现。CAB通过广泛的内部.NET反射实现了这些功能。CAB为了完成这些任务而提供的这些服务在Table1-1上列出了。
Table 1-1. Location in this Book of CAB Functionality for Solving Loose Problems of Loosely Coupled Applictions.
| Problem | CAB Infrastructure | Chapter |
|---|---|---|
| Central control of modules loaded at runtime by a specific user | Module enumerator and loader services | 2 |
| Connection to programmatic services | Service infrastructure | 2 |
| Connection of logical subapplications to each other within a composite application | WorkItem mechanism and dependency injection | 3 |
| Display of visual controls created by subapplications | Workspaces (container frames) and SmartParts (controls container with those frames) | 4 |
| Different subapplications sharing main menu, toolbar, and status bar | User interface extension sites | 5 |
| Notifying and being notified of events | Publish and subscribe event broker service | 6 |
Table 1-2 identifies the main terms we will be using in our discussion of CAB, listed in the order in which you're likely to think about them. A sample CAB application is shown in Figure 1-4, illustrating some of the concepts.
Table 1-2定义了我们在讨论CAB时用到的主要项目,并且按照你可能的思考的顺序来排序。Figure 1-4展示了一个样板CAB application,图解了部分概念。
Table 1-2. Definitions of Basic Terms Used in a CAB Application
Figure 1-4. Application diagram of shell, smartparts, and workspaces.

There really is nothing new under the sun.
事实上并没有什么新鲜的东西。
Although they use different underlying 底层 technologies, the architecture of a CAB application reminds me very much of in-place 适当 activation 活化作用 in Object Linking and Embedding 嵌入. While not a perfect or complete analogy类比 (OLE-embedded objects rarely if ever talk to each other, while CAB items often do), if you think of things that way, your user-level understanding of OLE will help you quickly grasp 抓住/领会 the roles of the components of CAB. Consider the archetypal 原型的 case of an Excel spreadsheet 电子数据表 embedded in a Microsoft Word document as shown in Figure 1-5. The CAB analogy would be as shown in Table 1-3.
虽然他们使用不同的底层技术,CAB application的架构提醒我在对象的链接与嵌入中添加适当的活化作用。(OLE是Object Linking and Embedding的缩写,即“对象链接与嵌入”,这是一种把一个文件的一部分嵌入到另一个文件之中的技术,例如把Excel图表加入到PowerPoint演示文稿或Word文档。)虽然是一个不完美或完整的类比,如果你那样思考问题,你的用户级OLE理解会帮助你快速领会组件在CAB中的角色。参考 Figure 1-5 中的Excel表格嵌入在Word文档中的原型案例。CAB类比在Table 1-3.
Figure 1-5

The original CAB library code is available on the MSDN Web site, currently at http://msdn.microsoft.com/library/en-us/dnpag2/html/cab.asp. It comes in a set of three source code projects, named CompositeUI, CompositeUI.WinForms, and ObjectBuilder. They all live in the solution CompositeUI.sln, the default location of which is C:\Program Files\Microsoft Composite UI App Block\CSharp. See Figure 1-6.
原始的CAB库代码在MSDN网上可以看到,包含三部分代码,CompositeUI, CompositeUI.WinForms, and ObjectBuilder.都包括在CompositeUI.sln解决方案中。
Figure 1-6. CAB DLL projects.

The solution builds three dynamic linked libraries (DLLs), named
CompositeUI.dll
CompositeUI.WinForms.dll
ObjectBuilder.dll
The easiest place to find them all is in the CompositeUI.WinForms\bin\Debug or \Release folder, as shown in Figure 1-7.
Figure 1-7. CAB DLLs built in a project folder.

You will have to decide early on in your project exactly how you want to distribute 分类 these DLLs. If you make every CAB application require its own private copy of them, then you can bundle 捆 the whole thing up and download it via ClickOnce. This is fast and easy and hard to get wrong. The only disadvantage is that you will then have extra copies of them. They aren't very large, about 300KB total, so perhaps this isn't too bad.
你需要在你的项目中尽早决定这些DLL的分配。如果你使得每个CAB application都只依赖自己,那么你可以把他们都绑在一起并通过ClickOnce下载。这是快速简单并且不易出错的方式。唯一的劣势是你需要它们的额外拷贝,不过也不大,300KB左右,所以也不是太糟。
Alternatively 非此即彼/二者择一地, you could sign them with your strong name and place them in the Global Assembly Cache GAC 全局程序集缓存. However, this means that ClickOnce won't be able to put them there because it can't modify the GAC.
作为一种选择,你也可以把他们安装在全局程序集缓存中,不管怎样,意味着ClickOnce不能完成程序更新,因为它无法修改GAC。

浙公网安备 33010602011771号