I am studying TDD. I know about dependency injection, in which you place class dependencies in constructor parameters and pass them, passing default implementations from the default constructor, for example:
public AccountController() : this( RepositoryFactory.Users()) { } public AccountController( IUserRepository oUserRepository) { m_oUserRepository = oUserRepository; }
RepositoryFactory is a simple static class that returns selected implementations for the current assembly.
But the ASP.NET web application project does not do this by default; instead, the DI takes the form of public properties that are assigned in the object initializer in the test class, for example; 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), };
So, now my AccountController class has two approaches to dependency injection. Which one should I use? Constructor inlining or public properties?
Turning on the mouse constructor ...
Is using ASP.NET MVC publicly available because you need to provide a specific way to enter the constructor, and the basic "create new" web application should be shared as a starting point?
c # dependency-injection tdd asp.net-mvc
Typo johnson
source share