简体   繁体   English

如何在控制器的相同post方法内订阅并引发事件?

[英]How do I subscribe to and raise an event within the same post method of my controller?

When an instance of my model MyPost is posted and it has a property with a certain value, eg Type = "Question" , I want to save the Question , subscribe to an event NoMoreAnswers and wait for a NoMoreAnswers event before returning the HTTP Response which will be a collection of 0 or more MyPost of Type = "Answer" . 当发布我的模型MyPost的实例并且它具有一个具有特定值的属性时,例如Type = "Question" ,我想保存Question ,订阅事件NoMoreAnswers并等待NoMoreAnswers事件,然后返回HTTP响应,将是0或多个Type = "Answer" MyPost的集合。 Each MyPost will have a unique Id. 每个MyPost将具有唯一的ID。

When additional instance of MyPost come in with a Type of Answer they also need to be saved. MyPost其他实例带有“ Answer类型”时,也需要保存它们。 Each Answer will also have its own unique id and a RefId pointing back to one of the Questions. 每个Answer还将具有其自己的唯一ID和一个指向问题之一的RefId

Finally when an instance of MyPost of Type = "NoMoreAnswers" is posted, also with its own unique id and with a RefId pointing back to one of the Questions, I want to raise the event NoMoreAnswers . 最后,当发布Type = "NoMoreAnswers"MyPost实例时,还具有其自己的唯一ID和RefId指向其中一个问题,我想引发事件NoMoreAnswers Optionally, the MyPost instance with Type = "NoMoreAnswers" should also be saved. (可选)还应保存Type = "NoMoreAnswers"MyPost实例。 This raised event should cause the waiting Question to collect all the related Answers and return them as a collection of MyPosts . 引发的事件应导致等待中的Question收集所有相关的Answers并将它们作为MyPosts的集合MyPosts This will obviously mean that the eventargs will need to contain the RefId of the NoMoreAnswers post, and every waiting Question will have to examine the eventargs to see if the event is relevant to it, and if not go back to a waiting state. 显然,这将意味着eventargs将需要包含NoMoreAnswers帖子的RefId ,并且每个正在等待的Question将必须检查eventargs以查看该事件是否与之相关,以及是否不返回到等待状态。

Can someone help me get started? 有人可以帮助我入门吗? How does one define, subscribe to and raise events that are relevant in this situation? 如何定义,订阅和引发与此情况相关的事件? Is there a pattern that applies to this kind of problem that I should read up on? 是否有适用于此类问题的模式值得我继续阅读?

If I right understand you, you need to use Session as a context for keeping and operating intermediate data between requests. 如果我理解正确,则需要使用Session作为上下文来保留和操作请求之间的中间数据。 See HttpContext Session. 请参见HttpContext会话。

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

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