ASP.NET MVC - 服务层,在每个控制器动作单个或多个服务? [英] ASP.NET MVC - Service layer, single or many services in each controller action?

查看:112
本文介绍了ASP.NET MVC - 服务层,在每个控制器动作单个或多个服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始实现服务层,我的MVC项目瘦下去一些臃肿的控制器(也有资料库/的UnitOfWork模式)。

我的问题是,如果你有一个有很多子对象等的页一个复杂的视图模式,相当多的逻辑去幕后(给你一个想法控制器原开发商写过近4000线code!)是OK的有多个服务去了做自己的事情吗?还是应该我只是有一个大的ReportService的这一切呢?

我的控制器开始看起来像吗?如果我继续我可能最终有相当被称为很多不同的服务建立起来的视图模型。

这是否看起来OK,或者它开始在错误的方向走?

 公开的ViewResult指数(INT?reportId)
    {
        //获取基本报表对象
        VAR ReportService的=新ReportService的();
        VAR报告= reportService.GetByReportId(reportId);
        VAR模型= Mapper.Map<报告ReportViewModel>(报告);        //获得当前活动的用户
        VAR userService =新UserService();
        变种用户= userService.GetCurrentUser();
        model.User = Mapper.Map<使用者,ReportViewModel.UserViewModel>(用户);        //获得第一个未读消息
        VAR messageService =新MessageService();
        VAR消息= messageService.GetFirstUnread(user.Id);
        model.Message = Mapper.Map<消息,ReportViewModel.MessageViewModel>(消息);        //获取类导航
        VAR categoryService =新CategoryService();
        VAR categoryNavigation = categoryService.GetCategoryNavigation(report.Id);
        model.CategoryNavigation = Mapper.Map<&IEnumerable的LT;类别>中的IEnumerable< ReportViewModel.CategoryNavigationViewModel>>(categoryNavigation);        返回查看(模型);
    }


解决方案

这是罚款,你的控制器有多个小型服务。然而,有一件事是错在这里:

您服务应该是可以通过整个控制器,并通过构造函数注入实现松耦合。

因此​​,像这样:

 私人只读IReportService _reportService;
私人只读IUserService _userService;公共SomeConstructor(IReportService ReportService的,IUserService userService等)
{
    _reportService = ReportService的;
    _userService = userService;
    //等
}

I'm starting to implement a service layer to my MVC project to thin down some bloated controllers (it also has repository / unitofwork pattern).

My question is if you have a complicated view model for a page with lots of child objects etc, and quite a lot of logic going on behind the scenes (to give you an idea the controller the original developer wrote had almost 4000 lines of code!!) is it OK to have multiple services going off doing their thing? or should I just have one big ReportService which does everything?

My controller is starting to look like this? and if I carry on I could end up having quite a lot of different services being called to build up the view model.

Does this look OK or is it starting to go in the wrong direction?

public ViewResult Index(int? reportId)
    {
        // get the base report object
        var reportService = new ReportService();
        var report = reportService.GetByReportId(reportId);
        var model = Mapper.Map<Report, ReportViewModel>(report);

        // get the current active user
        var userService = new UserService();
        var user = userService.GetCurrentUser();
        model.User = Mapper.Map<User, ReportViewModel.UserViewModel>(user);

        // get the first unread message
        var messageService = new MessageService();
        var message = messageService.GetFirstUnread(user.Id);
        model.Message = Mapper.Map<Message, ReportViewModel.MessageViewModel>(message);

        // get the category navigation
        var categoryService = new CategoryService();
        var categoryNavigation = categoryService.GetCategoryNavigation(report.Id);
        model.CategoryNavigation = Mapper.Map<IEnumerable<Category>, IEnumerable<ReportViewModel.CategoryNavigationViewModel>>(categoryNavigation);

        return View(model);
    }

解决方案

It's fine to have multiple small services in your controller. However, there is one thing that's wrong here:

You services should be available through the entire controller and injected through the constructor to achieve loose-coupling.

So something like this:

private readonly IReportService _reportService;
private readonly IUserService _userService;

public SomeConstructor(IReportService reportService, IUserService userService, etc.) 
{
    _reportService = reportService;
    _userService = userService;
    // etc
}

这篇关于ASP.NET MVC - 服务层,在每个控制器动作单个或多个服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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