分享

使用StructureMap扩展ASP.NET MVC三层架构4

 ThinkTank_引擎 2014-02-03

终于到了MVC的UI层了,也终于要到StructureMap了,不容易啊。本篇文章介绍ASP.NET MVC三层架构中的Controller和View,以及系统中异常处理。

mvc-structuremap-controller

Controller和View

其实View没有什么介绍的,就是显示。本章的重点在Controller层,这一层需要注入Service层,同时会有异常处理,具体的异常处理的实现会在下篇文章介绍,本片只是说明一下怎么使用本系统中的异常处理类。

Controller

MVC中的Controller翻译过来是控制器,没错我们只用他来跳转,这里不存在任何的业务逻辑,仅仅向Service中传递View中的数据,然后Service处理业务并返回我们需要结果,之后进行跳转。为什么异常处理放在这里呢,因为我们需要把有好的错误信息返回到页面中,构建一个友好的页面交互。

BaseController的实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using TYStudioDemo.Interfaces;
  7. namespace TYStudioDemo.WebUI.Controllers
  8. {
  9.     public abstract class BaseController : Controller
  10.     {
  11.          protected ITYExceptionService _tyExceptionService;
  12.          public BaseController() { }
  13.          public BaseController(ITYExceptionService tyExceptionService)
  14.          {
  15.              _tyExceptionService = tyExceptionService;
  16.          }
  17.     }
  18. }

每个单独的Controller都要继承自BaseController,BaseController中放入Controller中一般常用的Service,本系统只放入了ExceptionService,如果你在开发的时候发现很多Controller都需要注入相同的Service,没错把他们放到BaseController是一个很好地选择。

SupplierController

同样今天只介绍SupplierController的实现,其他举一反三~

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using TYStudioDemo.Interfaces;
  7. using TYStudioDemo.DTO;
  8. namespace TYStudioDemo.WebUI.Controllers
  9. {
  10.     public class SupplierController : BaseController
  11.     {
  12.         ISupplierService _supplierService;
  13.         public SupplierController(ITYExceptionService tyExceptionService,
  14.                                     ISupplierService supplierService)
  15.         {
  16.             _tyExceptionService = tyExceptionService;
  17.             _supplierService = supplierService;
  18.         }
  19.         public ActionResult Index()
  20.         {
  21.             return View(_supplierService.GetAll());
  22.         }
  23.         public ActionResult Details(int id)
  24.         {
  25.             return View(_supplierService.GetByID(id));
  26.         }
  27.         public ActionResult Create()
  28.         {
  29.             return View();
  30.         }
  31.         [HttpPost]
  32.         public ActionResult Create(SupplierViewModel vModel)
  33.         {
  34.             ActionResult result;
  35.             try
  36.             {
  37.                 _supplierService.Create(vModel);
  38.                 result = RedirectToAction("Index");
  39.             }
  40.             catch(Exception ex)
  41.             {
  42.                 //定义Dictionary用来存储非法的对象
  43.                 //根据实际需求,添加相应的数据,也可以不填任何数据
  44.                 Dictionary<string, object> parms = new Dictionary<string, object>();
  45.                 //返回错误信息,并将异常记入TYStudio_Logging数据库。
  46.                 ViewData["ErrorMessage"] = _tyExceptionService.HandleException(ex, parms, ex.Message);
  47.                 result = View(vModel);
  48.             }
  49.             return result;
  50.         }
  51.         public ActionResult Edit(int id)
  52.         {
  53.             return View(_supplierService.GetByID(id));
  54.         }
  55.         [HttpPost]
  56.         public ActionResult Edit(SupplierViewModel vModel)
  57.         {
  58.             try
  59.             {
  60.                 _supplierService.Update(vModel);
  61.                 return RedirectToAction("Index");
  62.             }
  63.             catch
  64.             {
  65.                 return View();
  66.             }
  67.         }
  68.         public ActionResult Delete(int id)
  69.         {
  70.             return View(_supplierService.GetByID(id));
  71.         }
  72.         [HttpPost]
  73.         public ActionResult Delete(int id, FormCollection collection)
  74.         {
  75.             try
  76.             {
  77.                 _supplierService.Delete(id);
  78.                 return RedirectToAction("Index");
  79.             }
  80.             catch(Exception ex)
  81.             {
  82.                 //定义Dictionary用来存储非法的对象
  83.                 //根据实际需求,添加相应的数据,也可以不填任何数据
  84.                 Dictionary<string, object> parms = new Dictionary<string, object>();
  85.                 //返回错误信息,并将异常记入TYStudio_Logging数据库。
  86.                 ViewData["ErrorMessage"] = _tyExceptionService.HandleException(ex, parms, ex.Message);
  87.                 return View(_supplierService.GetByID(id));
  88.             }
  89.         }
  90.     }
  91. }

Create(SupplierViewModel vModel) Action

SupplierController中主要介绍的是这个方法,首先我们定义一个规则,一个Action中只有一个return方便以后的维护。然后看一下我们的异常处理Try Catch。这里我们通过_tyExceptionService.HandleException这个方法进行异常的日志记录,这个方法的详细实现将在Exception异常处理文章中向大家说明。用ViewData["ErrorMessage"]传递HandleException方法返回的异常信息到View,当出现异常的时候页面就会看到相应的信息了。

UI的显示层View

这一层如果用到强类型View,我们不适用Entity而是DTO工程中的ViewModel,主要是把UI层和Model层分开,降低耦合度。

ViewData["ErrorMessage"]这个错误信息,你可以在页面上找一个合适的位置显示出来就ok了,通常我们把它放到master母版页中。

Controller中的权限管理

这个问题对于mvc4你可以参考天屹的MVC4 SimpleMembership权限管理系统的实现,加到里面就可以了。

总结:

BaseController的添加使得我们可以把公共的Service都管理起来一起注入。减少了单独Controller中的代码维护。Exception在Controller中捕获,同样更加利于系统的维护。

下一篇我们介绍StructMap的配置,与如何把Service注入到Controller,Repository注入到Service中的具体实现。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多