网站建设加排名要多少,wordpress 置顶字段,方案策划网站,学做淘宝客网站有哪些你可以遵循一些最佳实践来写出更干净的 Controller#xff0c;一般我们称这种方法写出来的 Controller 为瘦Controller#xff0c;瘦 Controller 的好处在于拥有更少的代码#xff0c;更加单一的职责#xff0c;也便于阅读和维护#xff0c;而且随着时间的推移也容易做 Co… 你可以遵循一些最佳实践来写出更干净的 Controller一般我们称这种方法写出来的 Controller 为瘦Controller瘦 Controller 的好处在于拥有更少的代码更加单一的职责也便于阅读和维护而且随着时间的推移也容易做 Controller 的多版本。这篇文章我们一起讨论那些让 Controler 变胖变臃肿的一些坏味道并且一起探索让 Controller 变瘦的手段虽然我的一些在 Controller 上的最佳实践可能不是专业的但我每一步都提供相关源代码来进行优化接下来的章节中我们会讨论什么是 胖Controller什么是 坏味道什么是 瘦Controller它能带给我们什么福利并且如何让 Controller 变瘦变简单利测试易维护。从 Controller 中移除数据层代码 当在写 Controller 的时候你应该遵守 单一职责也就意味着你的 Controller 只需做一件事情换句话说只有一个因素或者唯一一个因素能让你修改 Controller 中的代码如果有点懵的话考虑下面的代码片段它将 数据访问代码 糅进了 Controller 。
public class AuthorController : Controller
{private AuthorContext dataContext new AuthorContext();public ActionResult Index(int authorId){var authors dataContext.Authors.OrderByDescending(xx.JoiningDate).Where(xx.AuthorId authorId).ToList();return View(authors);}//Other action methods
}请注意上面的代码在 Action 中使用了 dataContext 从数据库读取数据这就违反了单一职责原则并直接导致了 Controller 的臃肿。假如后续你需要修改 数据访问层 代码可能基于更好的性能或者你能想到的原因这时候只能被迫在 Controller 中修改举个例子吧假如你想把上面的 EF 改成 Dapper 去访问底层的 Database更好的做法应该是单独拎出来一个 repository 类来操控 数据访问 相关的代码下面是更新后的 AuthorController。
public class AuthorController : Controller
{private AuthorRepository authorRepository new AuthorRepository();public ActionResult Index(int authorId){var authors authorRepository.GetAuthor(authorId);return View(authors);}//Other action methods
}现在 AuthorController 看起来是不是精简多了上面的代码是不是就是最佳实践呢不完全是为什么这么说呢上面这种写法导致 Controller 变成了 数据访问组件取出数据后必然少不了一些业务逻辑处理这就让 Controller 违反了 单一职责对吧更通用的做法应该是将 数据访问逻辑 封装在一个 service 层下面是优化之后的 AuthorController 类。
public class AuthorController : Controller
{private AuthorService authorService new AuthorService();public ActionResult Index(int authorId){var authors authorService.GetAuthor(authorId);return View(authors);}//Other action methods
}再看一下 AuthorService 类可以看到它利用了 AuthorRepository 去做 CURD 操作。
public class AuthorService
{private AuthorRepository authorRepository new AuthorRepository();public Author GetAuthor (int authorId){return authorRepository.GetAuthor(authorId);}//Other methods
}避免写大量代码做对象之间映射 在 DDD 开发中经常会存在 DTO 和 Domain 对象在数据 Input 和 Output 的过程中会存在这两个对象之间的 mapping按照普通的写法大概就是这样的。
public IActionResult GetAuthor(int authorId)
{var author authorService.GetAuthor(authorId);var authorDTO new AuthorDTO();authorDTO.AuthorId author.AuthorId;authorDTO.FirstName author.FirstName;authorDTO.LastName author.LastName;authorDTO.JoiningDate author.JoiningDate;//Other code......
}可以看到这种一一映射的写法让 Controller 即时膨胀同时也让 Controller 增加了额外的功能那如何把这种 模板式 代码规避掉呢可以使用专业的 对象映射框架 AutoMapper 去解决下面的代码展示了如何做 AutoMapper 的配置。
public class AutoMapping
{public static void Initialize(){Mapper.Initialize(cfg {cfg.CreateMapAuthor, AuthorDTO();//Other code });}
}接下来可以在 Global.asax 中调用 Initialize() 初始化如下代码所示
protected void Application_Start()
{AutoMapping.Initialize();
}最后可以将 mapping 逻辑放在 service 层中请注意下面的代码是如何使用 AutoMapper 实现两个不兼容对象之间的映射。
public class AuthorService
{private AuthorRepository authorRepository new AuthorRepository();public AuthorDTO GetAuthor (int authorId){var author authorRepository.GetAuthor(authorId);return Mapper.MapAuthorDTO(author);}//Other methods
}避免在 Controller 中写业务逻辑 尽量避免在 Controller 中写 业务逻辑 或者 验证逻辑 Controller 中应该仅仅是接收一个请求然后被下一个 action 执行别无其它回到刚才的问题这两种逻辑该怎么处理呢业务逻辑这些逻辑可以封装 XXXService 类中比如之前创建的 AuthorService。验证逻辑这些逻辑可以用 AOP 的操作手法比如将其塞入到 Request Pipeline 中处理。使用依赖注入而不是硬组合 推荐在 Controller 中使用依赖注入的方式来实现对象之间的管理依赖注入是 控制反转 的一个子集它通过外部注入对象之间的依赖从而解决内部对象之间的依赖很拗口是吧一旦你用了依赖注入方式就不需要关心对象是怎么实例化的怎么初始化的下面的代码展示了如何在 AuthorController 下的构造函数中实现 IAuthorService 对象的注入。
public class AuthorController : Controller
{private IAuthorService authorService new AuthorService();public AuthorController(IAuthorService authorService){this.authorService authorService;}// Action methods
}使用 action filer 消除 Controller 中的重复代码 可以利用 action filter 在 Request pipeline 这个管道的某些点上安插一些你的自定义代码举个例子可以使用 ActionFilter 在 Action 的执行前后安插一些自定义代码而不是将这些业务逻辑放到 Controller 中让 Controller 不必要的膨胀下面的代码展示了如何去实现。
[ValidateModelState]
[HttpPost]
public ActionResult Create(AuthorRequest request)
{AuthorService authorService new AuthorService();authorService.Save(request);return RedirectToAction(Home);
}总的来说如果一个 Controller 被赋予了几个职责那么只要是其中任何一个职责的原因你都必须对 Controller 进行修改总的来说一定要坚守 单一原则。译文链接https://www.infoworld.com/article/3404472/how-to-write-efficient-controllers-in-aspnet-core.html