0

N レイヤー Web アプリケーション (UI/サービス/DAL) を開発しています。

特定のサービスを呼び出す場合、サービス レイヤー内でユーザーへの通知が必要なイベントが発生することがあります。

これらのメッセージをサービス層から UI 層に渡すにはどうすればよいですか?

これらのメッセージはエラーではなく、特定のイベントの通知であることに注意してください。

4

1 に答える 1

1

依存性注入でそれを達成できます。IUserNotificator次のような汎用インターフェースがあるとします。

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

そして、あなたのサービスクラスは次のようなことをしています:

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);
        }
    }
}

このようにして、UI レベルでさまざまな実装を行うことができます。C# の winform アプリがあるとします。

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");
        }
    }
}

柔軟性を高めるために、一度の操作で複数のノーティフィケーターのデコレーターを使用してクラスを拡張できます。

于 2013-11-04T04:51:09.803 に答える