加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 为什么MVC提供的Default AccountController中有2

发布时间:2020-12-16 09:31:19 所属栏目:asp.Net 来源:网络整理
导读:这是框架生成的默认AccountController.cs. public class AccountController : Controller{ public IFormsAuthentication FormsAuth { get; private set; } public IMembershipService MembershipService { get; private set; } public AccountController() :
这是框架生成的默认AccountController.cs.

public class AccountController : Controller
{
    public IFormsAuthentication FormsAuth { get; private set; }
    public IMembershipService MembershipService { get; private set; }

    public AccountController()
        : this(null,null)
    {
    }
    public AccountController(IFormsAuthentication formsAuth,IMembershipService membershipService)
    {
        FormsAuth = formsAuth ?? new FormsAuthenticationService();
        MembershipService = membershipService ?? new AccountMembershipService();

        //---
    }

这很容易理解.

public AccountController(IFormsAuthentication formsAuth,IMembershipService membershipService)
    {
        FormsAuth = formsAuth ?? new FormsAuthenticationService();
        MembershipService = membershipService ?? new AccountMembershipService();
    }

这是什么?它的目的是什么?它是特定于帐户控制器还是其他控制器的要求?而且,我为什么要将它纳入我的项目?

public AccountController()
        : this(null,null)
    {
    }

他们似乎在其他两个地方使用这种类型的构造函数.

谢谢你的帮助

解决方法

这实际上是Bastard Injection反模式的实现.

我们的想法是支持构造函数注入以允许依赖注入(DI),同时仍为默认行为提供默认构造函数.

实际上没有必要使用默认构造函数,但如果省略它,则必须提供自定义IControllerFactory,因为DefaultControllerFactory假定所有控制器都具有默认构造函数.

ASP.NET MVC在构建时考虑了DI,但我想为了保持简单,Bastard Injection模式用于项目模板,以避免强制开发人员使用特定的IControllerFactory.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读