简体   繁体   English

模型内的金字塔事件

[英]Pyramid events inside models

I'm using Pyramid 1.4. 我正在使用Pyramid 1.4。 I would like to generate some custom Pyramid events from inside my model classes. 我想从我的模型类中生成一些自定义Pyramid事件。 Events are generated like so: 事件生成如下:

request.registry.notify(MyCustomEventType("Here it comes"))

As you can see, I need access to the application registry. 如您所见,我需要访问应用程序注册表。 I'm aware of get_current_registry() function. 我知道get_current_registry()函数。 But I'm also concerned about this comment from the Pyramid website: 但我也很关注Pyramid网站上的评论:

"This function should be used extremely sparingly, usually only in unit testing code" “这个功能应该非常谨慎地使用,通常只在单元测试代码中使用”

Questions : 问题

  • Is generating Pyramid events from the data layer (SQLAlchemy models) generally a bad idea? 从数据层生成Pyramid事件(SQLAlchemy模型)一般是个坏主意吗?
  • If not , how to access application registry in a more civilised way? 如果没有 ,如何以更文明的方式访问应用程序注册表? (Extending Base model perhaps?) (也许扩展Base模型?)
  • If yes , is there some alternative I could use. 如果 ,是否有一些我可以使用的替代品。 I'm aware of SQLAlchemy events, but I couldn't find an ability to generate custom events. 我知道SQLAlchemy事件,但我找不到生成自定义事件的能力。

Rationale : 理由

Basically, I divided my app into features and I try to keep them decoupled. 基本上,我将我的应用程序划分为功能,并尝试将它们分离。 For that, I sometimes need IoC: I was planning on using events as a mean for that. 为此,我有时需要IoC:我计划将事件作为一种手段。 For instance, whenever a user answers a questionare, an event is emitted. 例如,每当用户回答问题时,都会发出事件。 Then, such an event can be subscribed to in another parts of the application. 然后,可以在应用程序的其他部分订阅这样的事件。 I like to keep the application logic in the models and not in the views. 我喜欢将应用程序逻辑保留在模型中而不是视图中。 Hence, the described issue. 因此,所描述的问题。

What is the use case for firing the pyramid events from your models? 从模型中触发金字塔事件的用例是什么? Generally that would be a bad idea. 一般来说,这是一个坏主意。

As for connecting up the app/models. 至于连接app / models。 Most of that is done in the initialize call in your main() that passes the settings to the model to give it connection settings and the like. 其中大部分是在main()中的initialize调用中完成的,该调用将设置传递给模型以为其提供连接设置等。 I'd make sure any logic done to tie things together be limited to that single function call on application start up. 我确保任何将事物联系在一起的逻辑仅限于应用程序启动时的单个函数调用。

In the end though I'd imagine what you want is better done in another way. 最后,虽然我想象你想要的是以另一种方式做得更好。

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

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