我的应用程序有MVC控制器和WebApi控制器。到目前为止,我已经能够将控制器之间共享的任何逻辑分别推送到BaseController
和BaseApiController
。在WebApi和MVC控制器之间共享逻辑?
现在我有一种情况,API控制器和MVC控制器都在做同样的事情 - 注册一个新的用户帐户。这个过程有点复杂,因为我有一个多租户应用,看起来是这样的:
// MVCController : BaseController
public ActionResult Register(RegisterModel model) {
// see if the model is valid
// check and make sure the credentials check out with policy requirements
// add the user to the user table if they aren't already there
// create a tenancy
// assign cookie
}
大多数的实际工作推离到服务层,但通话和条件占用约20我宁愿不复制并粘贴到类似的WebApi控制器中的代码行。
所以我想知道如果我能以某种方式让我的BaseController
和BaseApiController
继承自一个通用控制器,我可以把这个认证代码。这里推荐的方法是什么?
是的,我将账户注册码拉入另一个班级/界面,以便重新使用它。如果你为它定义一个接口,你会让你的代码更加可测试,并且与控制原理的反转一致。 –