Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8608029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:30:52+00:00 2026-06-12T03:30:52+00:00

I have written a simple mediator for my WPF application (which is a derivative

  • 0

I have written a simple mediator for my WPF application (which is a derivative of the code found here).

public class Messenger
{
    private IDictionary<Type, IList<MessageObserver>> observers;

    public Messenger()
    {
        observers = new Dictionary<Type, IList<MessageObserver>>();
    }

    public IDisposeObserver AddObserver<TMessageType>(Action<TMessageType> handler) 
        where TMessageType : Message { }

    protected abstract void RemoveObserver<TMessageType>(Action<TMessageType> handler) 
        where TMessageType : Message { }

    public abstract void PostMessage<TMessageType>(TMessageType message) 
        where TMessageType : Message { }
}

Message is just an empty class that provides a base class for all messages that are posted via the Messenger. The idea is you would derive from this class to create a specific message class with relevant data (a bit like EventArgs).

Where my implementation differs is in the way I want to remove observers. I want to be able to specify when they should be removed in a declarative way, along the lines of:

MessengerInstance.AddObserver<LoginMessage>(HandleLogin)
    .RemoveObserverWhen(message => message.LoginResult == LoginResult.Successful);

The RemoveObserverWhen method takes a Predicate<T> where T should be the message type specified in the AddObserver method.

The idea here is you can specify the logic that removes the observer in the same place as registering the observer itself. The Messenger class will then check the predicate after the message handler has been run and remove both the message handler and the removal handler if the predicate evaluates to true.

Each message handler can have any number of removal handlers so I have packaged this up in a class called MessageObserver.

public class MessageObserver : IDisposeObserver
{
    private IList<object> disposalHandlers;

    public MessageObserver(object observer)
    {
        Observer = observer;
        disposalHandlers = new List<object>();
    }

    public object Observer { get; private set; }

    public IList<object> DisposalHandlers
    {
        get { return disposalHandlers; }
    }

    public IDisposeObserver RemoveObserverWhen<T>(Predicate<T> predicate) 
        where T : Message
    {
        disposalHandlers.Add(predicate);
        return this;
    }
}

The MessageObserver implements IDisposeObserver which provides the RemoveObserverWhen method.

public interface IDisposeObserver
{
    IDisposeObserver RemoveObserverWhen<T>(Predicate<T> predicate) 
        where T : Message;
}

The idea here is that an IDisposeObserver instance can be returned so that methods can be chained.

All this works and I have the following code in my viewModel:

MessengerInstance.AddObserver<LoginMessage>(HandleLogin)
    .RemoveObserverWhen<LoginMessage>(message => message.LoginResult == LoginResult.Successful)
    .RemoveObserverWhen<LoginMessage>(SecondDisposalHandler);

The problem I have is that this only works if I specify the generic parameter (LoginMessage in this case) when calling the RemoveObserver method. I would like to be able to call the methods in the way described at the beginning of the post.

I think I need to somehow return a generic IDisposeObserver but if I make this change then the MessageObserver has to be made generic and then I cannot specify the constraints as the Messenger class is non generic.

So my question is, Can my code be updated so that I do not have to specify the message type when calling the RemoveObserverWhen method or do I have to live with my current solution?

Note: I am aware that there are other implementations of this available but I am doing this to help my understanding of object oriented design principles and generics in c# so please do not point me in the direction of other implementations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:30:53+00:00Added an answer on June 12, 2026 at 3:30 am

    I think I need to somehow return a generic IDisposeObserver but if I make this change then the MessageObserver has to be made generic and then I cannot specify the constraints as the Messenger class is non generic.

    You probably want to have a non-generic MessageObserver base class which contains all the type-neutral parts, and then a generic one which implements a generic IDisposeObserver<T>. You can then change AddObserver to return the generic IDisposeObserver<T>, but keep the dictionary within Messenger as IDictionary<Type, IList<MessageObserver>>. So just to be clear, you’d have:

    public interface IDisposeObserver<T>
    {
        IDisposeObserver<T> RemoveObserverWhen(Predicate<T> predicate) 
            where T : Message;
    }
    
    public abstract class MessageObserver { ... }
    
    public class MessageObserver<T> : MessageObserver, IDisposeObserver<T> { ... }
    

    The problem then comes when you have to execute the IDiposeObserver<T>.RemoveObserverWhen method – you may well need to just cast each item within the dictionary’s list to IObserver<T>. You’d know the T at that point, and you’d know that you’d populated the list appropriately.

    Of course, if that’s the only thing you’re going to do with the observers, you could just keep an IDictionary<Type, IList<object>> instead, and not bother having the two different MessageObserver classes. It really depends on whether you get any value out of the non-generic one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple WPF application in which I wanted to test the
I have written a simple web Application which is running(on Tomcat Server) fine on
I have written a simple test class for Play! 2.0: public class TestLogin {
I have written a simple OCI wrapper class in PHP which uses persistent connections
I have written a simple application which has login page. After login search insertion
I have written simple code to get content from xml file to php. $xml
I have written a simple automation script for deploying and restarting my twisted application
I have written a simple CRUD form which has one select list. However the
I have written a simple jQuery.ajax function which loads a user control from the
I have written a simple Java class to generate the hash values of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.