Trying to understand the event aggregator pattern
Asked Answered
G

2

20

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.

Garda answered 9/8, 2010 at 1:37 Comment(0)
B
7

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

Berkow answered 9/8, 2010 at 1:49 Comment(2)
the article is nice, but i can't imagine how the ISubject and Subject will look like, he didn't write them in his example.Garda
In case anyone stumbles in here, Amr posted on finding ISubject and someone pointed out that they are in Reactive Extensions: #3439919Containerize
R
8

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.

Registration answered 12/2, 2012 at 18:2 Comment(0)
B
7

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

Berkow answered 9/8, 2010 at 1:49 Comment(2)
the article is nice, but i can't imagine how the ISubject and Subject will look like, he didn't write them in his example.Garda
In case anyone stumbles in here, Amr posted on finding ISubject and someone pointed out that they are in Reactive Extensions: #3439919Containerize

© 2022 - 2024 — McMap. All rights reserved.