ASP.NET MVC中Default AccountController为何设有两个构造函数?

2026-03-30 13:081阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计304个文字,预计阅读时间需要2分钟。

ASP.NET MVC中Default AccountController为何设有两个构造函数?

这是框架生成的默认AccountController.cs代码内容:

csharp这是框架生成的默认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(); }

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

ASP.NET MVC中Default AccountController为何设有两个构造函数?

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

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

谢谢你的帮助

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

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

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

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

标签:Def

本文共计304个文字,预计阅读时间需要2分钟。

ASP.NET MVC中Default AccountController为何设有两个构造函数?

这是框架生成的默认AccountController.cs代码内容:

csharp这是框架生成的默认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(); }

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

ASP.NET MVC中Default AccountController为何设有两个构造函数?

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

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

谢谢你的帮助

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

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

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

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

标签:Def