0

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.

Fex Rex
  • 17
  • 6
  • Do those events happen after a service call has finished, or during? If the former, you can put the message in the return value. What type of messages do you want to display? If it's a confirmation like _"Are you sure you want to delete X?"_, your UI should solve that, not your service. Please explain with an actual example and preferably some code what you want to do. – CodeCaster Oct 31 '13 at 22:28
  • The type of message is somewhate like this: 'You've successfully created your first article, congratulations!' Don't know why but I've been using BOOLEAN as a return type of my service calls...Comment on this also please. – Fex Rex Oct 31 '13 at 22:32
  • That's where a ViewModel can be used, with a `string ResultMessage`. Your UI layer (say, an MVC Controller) translates the `result.Success` boolean into a string message, for example using resource files, and assigns that to the viewmodel. This way you can change the UI language without changing the service. – CodeCaster Oct 31 '13 at 22:34
  • Is it good to use POCO's as result types from my service layer calls? btw is there a problem with model reuse? (used in view as model, used in servicelayer as result type/parameter passing) – Fex Rex Oct 31 '13 at 22:38
  • That's an entirely different question, see for example [POCO classes and ViewModels in MVC3](http://stackoverflow.com/questions/16934696/) for a few opinions. As said there, it depends on the size of the project. If you have flat, one-to-one mapping entities and views, go ahead. You just ran into a problem with model reuse though: you don't want to add view-specific properties to an entity model. You can if you must, using [`[NotMapped]`](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute(v=vs.110).aspx). – CodeCaster Oct 31 '13 at 22:41

1 Answers1

1

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.

Fendy
  • 4,155
  • 1
  • 17
  • 24
  • In this case, is it safe to inject dependencies between the service and the UI layers? – Fex Rex Nov 05 '13 at 05:27
  • It's the purpose of dependency injection, that is to inject the dependency from UI to service layer. In service layer, you are do code related to interface, and that interface are being implemented in UI layer. In UI layer itself, the service is used so it is safe to inject the dependency in UI layer – Fendy Nov 05 '13 at 06:18