Question

Im developing a N Layer web application (UI/Service/DAL).

When in calling a certain service, inside the service layer sometimes theres an event that requires user notification.

How should I pass these messages from the service layer to the UI layer?

It is very important to notice that these messages are not errors but only notifications of certain events.

Was it helpful?

Solution

You can achieve it with Dependency Injection. Say that you have a generic interface of IUserNotificator like this:

interface IUserNotificator{
    //message type can be Warning, Success, Error or Confirmation
    void Notify(string message, MessageType messageType);
}

And your service class doing something like this:

class Service{
    // construtor injection of IUserNotificator

    void DoSomething(){
        // doing something
        if(error){
            IUserNotificator.Notify("There is error", MessageType.Error);
        }
        else{
            IUserNotificator.Notify("Operation success", MessageType.Success);
        }
    }
}

This way, you can have different implementations at UI level. Say that you have a C# winform app:

class MessageBoxUserNotificator : IUserNotificator{
    void Notify(string message, MessageType messageType){
        if(messageType == MessageType.Error){ 
            MessageBox.Show(message, "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else{
            MessageBox.Show(message, "Notification");
        }
    }
}

For more flexibility the class can be expanded using decorator for multiple notificator at one operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top