C# - 管道式事件模型

发布于 2024-08-02 04:15:20 字数 452 浏览 14 评论 0原文

在 ASP.NET Web 应用程序中,事件按特定顺序触发:

为了简单起见 Load =>验证 => 回发 => 渲染

假设我想开发这样的管道式事件

示例:

事件 1 [ "观众正在聚集" ,各位{ 事件 2 和事件 3 请等待我发出信号 }]

事件 1 完成任务后

事件 2 [ { 事件 2, 事件 3 "观众聚集!我的任务结束了 } ]

事件 2 正在接管控制权以执行其任务

事件 2 [ " 观众正在登录 "事件 3 请等待,直到我发出信号]

在事件 2 完成任务之后

.....

事件 3 [“Jon skeet 的演示结束:)”]

任何人都可以通过非常基本的示例来解释,如何才能我设计这个?

In ASP.NET Web Apps , events are fired in particluar order :

for simplicity Load => validation =>postback =>rendering

Suppose I want to develop such pipeline -styled event

Example :

Event 1 [ "Audiance are gathering" ,Guys{ Event 2 and Event 3 Please wait until i signal }]


after Event 1 finished it task

Event 2 [ { Event 2, Event 3 "Audiance gathered! My task is over } ]

Event 2 is taking over the control to perform its task

Event 2 [ " Audiance are Logging in " Event 3 please wait until i signal ]

after Event 2 finished it task

.....

Event 3 [ "Presentation By Jon skeet is Over :) "]

With very basic example can anybody explain ,how can i design this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城°AllureLove 2024-08-09 04:15:20

此示例展示了如何采用简单的抽象类和处理程序列表上的简单扩展方法,并实现您正在讨论的管道模型。 这当然是一个简单的示例,因为我只是将一个字符串作为事件数据传递给它。 但显然您可以根据您的情况进行定制。

扩展方法 RaiseEvent 枚举处理程序列表,并调用处理程序上的 Handle 方法来通知该事件。

public abstract class Handler
{
  public abstract void Handle(string event);
}

public static class HandlerExtensions
{
  public static void RaiseEvent(this IEnumerable<Handler> handlers, string event)
  {
     foreach(var handler in handlers) { handler.Handle(event); }     
  }
}

...

List<Handler> handlers = new List<Handler>();
handlers.Add(new Handler1());
handlers.Add(new Handler2());

handlers.RaiseEvent("event 1");
handlers.RaiseEvent("event 2");
handlers.RaiseEvent("event 3");

This example shows how you can take a simple abstract class, and a simple extension method on a list of handlers, and implement the pipeline model you're talking about. This is of course a trivialized example as I'm simply passing it a string as the event data. but you can obviously customize to suit your situation.

the extension method RaiseEvent enumerates over the list of handlers and calls the Handle method on the handler to notify it of the event.

public abstract class Handler
{
  public abstract void Handle(string event);
}

public static class HandlerExtensions
{
  public static void RaiseEvent(this IEnumerable<Handler> handlers, string event)
  {
     foreach(var handler in handlers) { handler.Handle(event); }     
  }
}

...

List<Handler> handlers = new List<Handler>();
handlers.Add(new Handler1());
handlers.Add(new Handler2());

handlers.RaiseEvent("event 1");
handlers.RaiseEvent("event 2");
handlers.RaiseEvent("event 3");
谁许谁一生繁华 2024-08-09 04:15:20

Windows Workflow Foundation 就是为此而设计的。 这里有 2 个截屏视频,您可以看看如何实现这样的事情。

http://blog.wekeroad.com/mvc-storefront/mvcstore-part -19a/

http://blog.wekeroad.com/ mvc-storefront/mvcstore-part-21/

Windows Workflow Foundation is designed to do just this. There's 2 screencasts here you can take a look at on how to implement something like this.

http://blog.wekeroad.com/mvc-storefront/mvcstore-part-19a/

http://blog.wekeroad.com/mvc-storefront/mvcstore-part-21/

无所的.畏惧 2024-08-09 04:15:20

一个非常简单的解决方案,也接近 ASP.NET 中实际发生的情况:

class EventChain
{    
    public event EventHandler Phase1Completed;
    public event EventHandler Phase2Completed;
    public event EventHandler Phase3Completed;

    protected void OnPhase1Complete()
    {
        if (Phase1Completed != null)
        {
            Phase1Completed(this, EventArgs.Empty);
        }
    }

    public void Process()
    {
        // Do Phase 1
        ...
        OnPhase1Complete();

        // Do Phase 2
        ...
        OnPhase2Complete();    
    }
}

A very simple solution, that is also close to what actually happens in ASP.NET:

class EventChain
{    
    public event EventHandler Phase1Completed;
    public event EventHandler Phase2Completed;
    public event EventHandler Phase3Completed;

    protected void OnPhase1Complete()
    {
        if (Phase1Completed != null)
        {
            Phase1Completed(this, EventArgs.Empty);
        }
    }

    public void Process()
    {
        // Do Phase 1
        ...
        OnPhase1Complete();

        // Do Phase 2
        ...
        OnPhase2Complete();    
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文