Why are there two constructors in the Default AccountController provided by MVC?

it uses 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)
    {
    }
    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 it? What is his purpose? Is this a feature for the Account Controller or is it a requirement for other controllers? and why should I include it in my project?

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

They seem to use this type of constructors in two other places.

thanks for the help

+3
source share
3 answers

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

, , Injection Dependency (DI), .

, , IControllerFactory, DefaultControllerFactory , .

ASP.NET MVC DI, , Bastard Injection , IControllerFactory.

+7
  • DI (, Unity), , ( ).

  • , ... where T : IController, new(), .

0

( ) .

System.Reflection , , classes, . , .

There are times when you need to create a temporary object of this type to reflect its properties or methods, but do not want or need the overhead of creating a real object, especially if it involves database access or remote maintenance, for example.

0
source

Source: https://habr.com/ru/post/1726882/


All Articles