为什么在asp.net-mvc的Default AccountController里存在两个构造函数?

更新于
2026-09-26 07:36:49
8阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

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

为什么在asp.net-mvc的Default AccountController里存在两个构造函数?

这是框架生成的默认AccountController.cs的简化开头内容:csharppublic class AccountController : Controller{ public IFormsAuthentication FormsAuth { get; private set; } public IMembershipService MembershipService { get; private set; } public AccountController() { // 初始化服务 }}

这是框架生成的默认AccountController.cs.

为什么在asp.net-mvc的Default AccountController里存在两个构造函数?

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.

标签:Def

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

为什么在asp.net-mvc的Default AccountController里存在两个构造函数?

这是框架生成的默认AccountController.cs的简化开头内容:csharppublic class AccountController : Controller{ public IFormsAuthentication FormsAuth { get; private set; } public IMembershipService MembershipService { get; private set; } public AccountController() { // 初始化服务 }}

这是框架生成的默认AccountController.cs.

为什么在asp.net-mvc的Default AccountController里存在两个构造函数?

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.

标签:Def