尝试理解事件聚合器模式

发布于 2024-09-13 14:06:49 字数 1860 浏览 0 评论 0原文

我正在尝试以简单的方式实现事件聚合器模式,以逐步学习它。但我没有找到任何书籍或不错的视频教程谈论它的实现。
我刚刚找到了一些好文章,例如http://weblogs.asp.net/rashid/archive/2009/03/05/use-event-aggregator-to-make-your-application-more-extensible.aspx< /a> 和 http://martinfowler.com/eaaDev/EventAggregator.html 第一篇文章是太大了,无法让我理解该模式,并且第二个尚未完成:)。
顺便说一句,我创建了我的类:

public class Member
{
    public int ID { get; set; }

    public string UserName { get; set; }
}

public class MemberService
{
    public void CommentSubmited()
    {
        // increase member score and do some other logic.
    }
}

public class Comment
{
    public int ID { get; set; }

    public string CommentBody { get; set; }

    public Member ByMember { get; set; }
}

public class CommentService
{
    public void SubmitNewComment(Member member, string commentBody, EventAggregator eventAggregator)
    {
        Comment comment = new Comment();
        comment.ByMember = member;
        comment.CommentBody = commentBody;

        db.SaveComment(comment); // save comment to the db

        //eventAggregator.GetEvent<CommentSubmited>.Fire();
    }
}

public class EventAggregator
{
    public void RegisterEvent()
    {

    }

    public void RemoveEvent()
    {

    }
}

我想要的是创建一种通用方法,以便每当有新评论时都会创建要触发的 CommentSubmited() 方法。
我希望它是通用的,因为以后会有更多服务,例如 RateService、QuestionService 等,并且每个服务在 MemberService 类中都会有一个 XXXSubmited() 方法。

希望你明白我想学什么,问我是否想让我把事情说得更清楚。

请注意,我检查了通用委托主题并且认为这可能对我解决这个问题有帮助,但无法按照我的意愿实现。

I am trying to implement the event aggregator pattern in a simple way to learn it step by step. But i didn't find any book or nice video tutorial talking about it's implementation.
I just found some good articles such as this http://weblogs.asp.net/rashid/archive/2009/03/05/use-event-aggregator-to-make-your-application-more-extensible.aspx and http://martinfowler.com/eaaDev/EventAggregator.html the first article is too big to let me understand the pattern and the second one is not completed :).
By the way i created my classes:

public class Member
{
    public int ID { get; set; }

    public string UserName { get; set; }
}

public class MemberService
{
    public void CommentSubmited()
    {
        // increase member score and do some other logic.
    }
}

public class Comment
{
    public int ID { get; set; }

    public string CommentBody { get; set; }

    public Member ByMember { get; set; }
}

public class CommentService
{
    public void SubmitNewComment(Member member, string commentBody, EventAggregator eventAggregator)
    {
        Comment comment = new Comment();
        comment.ByMember = member;
        comment.CommentBody = commentBody;

        db.SaveComment(comment); // save comment to the db

        //eventAggregator.GetEvent<CommentSubmited>.Fire();
    }
}

public class EventAggregator
{
    public void RegisterEvent()
    {

    }

    public void RemoveEvent()
    {

    }
}

And what i want is to create a generic way so that when ever a new comment created the CommentSubmited() method to Fire.
I want it generic because there will be more services later such as RateService, QuestionService, .... and each one will have a XXXSubmited() method in the MemberService class.

Hope you understood what i want to learn, ask me if you want me to make things more clear.

Note i checked the Generic Delegates topic and thought it may help me in this issue, but couldn't make it as i wanted.

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

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

发布评论

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

评论(2

桃气十足 2024-09-20 14:06:49

Karl Shifflett(Microsoft 模式和实践团队)制作了一个视频,他在其中介绍了事件聚合器模式并解释了他如何在他的 Stuff WPF/MVVM 应用程序中使用了它。他的 博客条目有更多关于他的项目的信息,源代码也可供下载。我发现他的示例应用程序和视频对我的学习非常有帮助。

Karl Shifflett (Microsoft patterns and practices team) made a video where he walks through the Event Aggregator pattern and explains how he used it in his Stuff WPF/MVVM application. His blog entry has more about his project and the source code is available for download as well. I found his example application and videos to be really helpful as I was learning.

你曾走过我的故事 2024-09-20 14:06:49

查看这篇关于使用 Rx 的简单事件聚合器的文章: Event具有反应式扩展的聚合器

Check out this post on a simple event aggregator using Rx: Event Aggregator with Reactive Extensions

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