ASP.NET-MVC – Why does 2 constructor in Default AccountController provided by MVC?

This is the default AccountController.cs generated by the framework.

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

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

//---
}

This is easy to understand.

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

What is this? What is its purpose? Is it specific to the account controller or a requirement of other controllers? And, why should I include it in my project?

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

They seem to be in other Two places use this type of constructor.

Thank you for your help

This is actually an implementation of the Bastard Injection anti-pattern.

Our idea is to support constructor injection to allow dependency injection (DI) while still providing a default constructor for the default behavior.< /p>

There is actually no need to use the default constructor, but if you omit it, you must provide a custom IControllerFactory, because DefaultControllerFactory assumes that all controllers have a default constructor.

ASP.NET MVC DI was considered when building, but I want to keep it simple, Bastard Injection mode is used for project templates to avoid forcing developers to use a specific IControllerFactory.

This is the framework The generated default 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)
{
FormsAu th = formsAuth ?? new FormsAuthenticationService();
MembershipService = membershipService ?? new AccountMembershipService();

//---
}

This It is easy to understand.

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

What is this? What is its purpose? Is it specific to the account controller or a requirement of other controllers? And, why should I include it in my project?

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

They seem to be in other This type of constructor is used in two places.

Thank you for your help

This is actually the implementation of the Bastard Injection anti-pattern .

Our idea is to support constructor injection to allow dependency injection (DI) while still providing a default constructor for default behavior.

There is actually no need to use the default Constructor, but if you omit it, you must provide a custom IControllerFactory, because DefaultControllerFactory assumes that all controllers have a default constructor.

ASP.NET MVC considered DI when it was built, but I want to keep it Simple, Bastard Injection mode is used for project templates to avoid forcing developers to use a specific IControllerFactory.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2107 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.