Skip to main content
Changed variable names and detailed the solution a bit more
Source Link
Maxim
  • 2.5k
  • 10
  • 17

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallback =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallback[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

The typecasting is done inside the Register method, making it invisible to any class using it.

You can also change signature of the GreetUser method so that it takes a User instead of ana objectstring as a parameter. By doing so you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallback =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallback[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

The typecasting is done inside the Register method, making it invisible to any class using it.

You can also change signature of the GreetUser method so that it takes a User instead of an object as a parameter. By doing so you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallback =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallback[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

The typecasting is done inside the Register method, making it invisible to any class using it.

You can also change signature of the GreetUser method so that it takes a User instead of a string as a parameter. By doing so you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);
Changed variable names and detailed the solution a bit more
Source Link

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallbacksregisteredCallback =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallbacks[message]registeredCallback[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

Also youThe typecasting is done inside the Register method, making it invisible to any class using it.

You can also change signature of the GreetUser method so the method will takethat it takes a User instead of an object as a parameter. In this caseBy doing so you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallbacks =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallbacks[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

Also you can change signature of GreetUser method so the method will take a User. In this case you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallback =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallback[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

The typecasting is done inside the Register method, making it invisible to any class using it.

You can also change signature of the GreetUser method so that it takes a User instead of an object as a parameter. By doing so you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);
deleted 102 characters in body
Source Link
Maxim
  • 2.5k
  • 10
  • 17

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallbacks =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallbacks[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        foreach (var callback in registeredCallbacks[message])
        {
            callback(parameter);
        }
    }
}

Also you can change signature of GreetUser method so the method will take a User. In this case you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallbacks =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallbacks[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        foreach (var callback in registeredCallbacks[message])
        {
            callback(parameter);
        }
    }
}

Also you can change signature of GreetUser method so the method will take a User. In this case you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);

What about making the Register method generic?

public class Mediator
{
    private Dictionary<Messages, Action<object>> registeredCallbacks =
        new Dictionary<Messages, Action<object>>();

    public void Register<TParam>(Messages message, Action<TParam> callback)
    {
        registeredCallbacks[message] = p => callback((TParam)p);
    }

    public void Notify(Messages message, object parameter)
    {
        callback(parameter);
    }
}

Also you can change signature of GreetUser method so the method will take a User. In this case you can call Register like this:

mediator.Register<User>(Messages.UserCreated, GreetUser);
edited body
Source Link
Maxim
  • 2.5k
  • 10
  • 17
Loading
Source Link
Maxim
  • 2.5k
  • 10
  • 17
Loading