MVC 4通知消息到用户 [英] MVC 4 Notification Messages to the User

查看:75
本文介绍了MVC 4通知消息到用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的控制器 - 服务 - 库层图案的我为我的应用。存储库包含基本的CRUD操作和服务层的所有业务逻辑,而控制器保持非常瘦(他们只调用服务层的功能)。

I'm using a controller - service - repository layer pattern I'm my Application. The repository holds basic CRUD operations and the service layer all the business logic while the controllers are kept very lean (they only call the service layer functions).

我要发送消息(成功,警告等)的用户,但我不能确定如何从服务层的水平呢?我抬起头用的TempData和基类中的一些解决方案,但这些似乎只在控制器级工作。

I want to send messages (success, warning, etc.) to the user but I'm unsure how to do it from a service layer level? I've looked up some solutions using TempData and a base class but those only seem to work on a controller level.

有没有很好的解决了从服务层用户推送通知到一个看法?

Is there a good solution to push user notifications to a view from a service layer?

推荐答案

看起来你已经在你的应用程序架构的关注良好的分离。这将是非常明智的保持下去 - 不要让你的服务层了解UI事情。

Looks like you have a good separation of concerns in your app architecture. It would be very wise to keep it this way - do not make your service layer to know anything about UI.

我会怎么做,我会创造一些类服务层返回的执行结果。

What I would do I would create some class for service layer to return as execution result.

public class ExecutionResult<T>
{
    public T Result { get; set; }
    public string Message { get; set; }
}

这样,您就可以通过消息 ViewBag.UserNotification ,并在您的视图shouw吧:

This way you could pass Message to ViewBag.UserNotification and shouw it in your view:

编辑:示例用法:

public class Math
{
    public ExecutionResult<double> Divide(double number, double divideBy)
    {
        if (divideBy == 0)
        {
            return new ExecutionResult<double>
                {
                    Result = double.NaN,
                    Message = "Division by zero is not possible"
                };
        }

        return new ExecutionResult<double>
            {
                Result = number/divideBy
            };
    }
}

这只是一个样本。在现实世界中,你会采取比较类型的值

This is just a sample. In real world you would take different approach in comparing values of type double

这篇关于MVC 4通知消息到用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆