I'm learning TDD. I know about dependency injection whereby you place the class's dependencies in the constructor's parameters and have them passed in, passing in default implementations from the default constructor eg;
public AccountController() : this( RepositoryFactory.Users())
{
}
public AccountController( IUserRepository oUserRepository)
{
m_oUserRepository = oUserRepository;
}
RepositoryFactory is a simple static class that returns the chosen implementations for the current build
But the default ASP.NET MVC web app project doesn't do this, instead the DI takes the form of public properties that are assigned in the object initializer in the test class eg; from AccountController.cs :
protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null)
{ FormsService = new FormsAuthenticationService(); }
if (MembershipService == null)
{ MembershipService = new AccountMembershipService(); }
base.Initialize(requestContext);
}
And in the test class AccountControllerTest.cs :
private static AccountController GetAccountController()
{
AccountController controller = new AccountController()
{
FormsService = new MockFormsAuthenticationService(),
MembershipService = new MockMembershipService(),
Url = new UrlHelper(requestContext),
};
//snip
}
So now my AccountController class has two approaches to dependency injection. Which one should I use? Constructor injection or public properties?
Am thinking constructor injection...
Is the ASP.NET MVC use of public properties like that because you need to provide a specific way of injecting into the constructor, and the basic "create new" web app needs to be generic as a starting point?