简体   繁体   English

将Zope组件注册到GlobalSiteManager有什么意义?

[英]What's the point in registering a Zope component to the GlobalSiteManager?

I'm reading the awesome tutorial about the zope component architecture from: 我正在从以下位置阅读有关zope组件体系结构的出色教程:

http://www.muthukadan.net/docs/zca.html#adapters http://www.muthukadan.net/docs/zca.html#adapters

I can't seem to understand the Adapters chapter. 我似乎无法理解“ 适配器”一章。

>>> from zope.interface import implements
>>> from zope.component import adapts

>>> class FrontDeskNG(object):
...
...     implements(IDesk)
...     adapts(IGuest)
...
...     def __init__(self, guest):
...         self.guest = guest
...
...     def register(self):
...         guest = self.guest
...         next_id = get_next_id()
...         bookings_db[next_id] = {
...         'name': guest.name,
...         'place': guest.place,
...         'phone': guest.phone
...         }

Correct me if I'm wrong. 如果我错了纠正我。 The above class FrontDeskNG is an adapter, right? 上面的类FrontDeskNG是适配器,对吗? As written in the article: 如文章所述:

FrontDeskNG is an adapter of IDesk which adapts IGuest FrontDeskNG是IDesk的适配器,可适应IGuest

Ok, so now I have the adapter, why would I have to register it to the GlobalSiteManager before I can use it? 好的,现在有了适配器,为什么在使用适配器之前必须将其注册GlobalSiteManager

I'm working on a mobile game backend and I want to make the code more modular by checking out Zope Component Architecture. 我正在开发手机游戏后端,我想通过签出Zope组件体系结构使代码更具模块化。 I need the ability to swap out my storage with whatever database technology and have the code still work. 我需要使用任何数据库技术交换存储空间并保持代码正常工作的能力。 This seemed like something ZCA proudly advertises. 这似乎是ZCA自豪地宣传的东西。 I have never read the famous GoF Design Patterns book, so please bear with me. 我从未读过著名的GoF设计模式书,因此请耐心等待。

One pragmatic advantage is that it allows the following very cool snippet; 务实的优势是它允许以下非常酷的代码段;

use_me_like_a_desk = IDesk(instance_of_something_providing_iguest)

You've not had to specify anything there apart from the fact you want something that provides the IDesk interface. 除了您想要提供IDesk接口的东西之外,您无需在此指定其他任何东西。 As long as you've registered an adapter for converting IGuest to IDesk everything works. 只要您已经注册了将IGuest转换为IDesk的适配器,一切都可以使用。

In other words 换一种说法

This is probably the nicest example but there are a number of queries you can make using the registration framework. 这可能是最好的示例,但是可以使用注册框架进行许多查询。 For example you can essential just ask; 例如,您可以只问一下就可以;

I have this thing, please ensure that it has this interface. 我有这个东西,请确保它具有这个界面。

or even in the case of utilities 甚至就公用事业而言

Please give me the thing that provides this interface for my application 请给我提供为我的应用程序提供此接口的东西

Advantages 好处

Often all the registration of the various adapters and utilities is done per application using zcml. 通常,每个适配器和实用程序的所有注册都使用zcml针对每个应用程序完成。 This makes it easy as pie to swap out one adapter for another, or use a different implementation of a utility without having to change a ton of code. 这样一来,馅饼就可以轻松地将一个适配器换成另一个适配器,或者使用实用程序的其他实现方式而不必更改大量代码。

More generally the registration of components reinforces decoupling. 更一般地,组件的配准加强了去耦。 You can build multiple modules that work together without having to couple them together. 您可以构建可以一起工作的多个模块,而不必将它们耦合在一起。 The only things that two modules need to work together is a common set of interfaces. 两个模块需要一起工作的唯一事情是一组通用的接口。

This makes it incredibly easy to write modules that work seamlessly together without making them dependent on each other, which in turn makes it easy to add, remove, evolve and test new components without massive refactoring. 这使得编写无缝地协同工作而又彼此不依赖的模块变得异常容易,这反过来又使得无需进行大量重构即可轻松添加,删除,发展和测试新组件。

More information 更多信息

As I'm sure you've found out the various bits of zope documentation is spread out and difficult to find. 我敢肯定,您已经找到了zope文档的各个部分,并且很难找到。 It takes a long time I think for some of the concepts and the advantages they bring to click, the best thing you can do is read as much as you can and then go live in a cave for six months in Alaska and meditate on it. 我想花很长时间才能找到一些概念和它们带来的好处,您能做的最好的事情就是尽可能多地阅读,然后在阿拉斯加的一个山洞里住六个月并进行冥想。

The Comprehensive Guide to Zope Component Architecture is a good start but I also advise reading at least the zope.component docs and the zope.interface docs . Zope组件体系结构综合指南》是一个好的开始,但我也建议您至少阅读zope.component文档zope.interface文档 In particular this example might help you understand the benefit 特别是这个例子可以帮助您理解好处

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

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