0

Possible Duplicate:
Dependency injection through constructors or property setters?

I'm curios if Property Injection has any advantages over Constructor Injection:

  public class LoginController : Controller
  {
    [Inject]
    public ICookiesManager CookiesManager {get; set;}

    [Inject]
    public ISecurityManager SecurityManager {get; set;}

    public LoginController()
    {

    }
  }

vs

  public class LoginController : Controller
  {
    private ICookiesManager _cookiesManager;
    private ISecurityManager _securityManager;

    public LoginController(ICookiesManager cookiesManager, ISecurityManager securityManager)
    {
      _securityManager = securityManager;
      _cookiesManager = cookiesManager;
    }
  }
Community
  • 1
  • 1
Ruslan
  • 2,678
  • 3
  • 22
  • 25

1 Answers1

1

There's very little effective difference in the two patterns other than with property injection, you can replace the implementation with a simple property set. With constructor injection, it's more explicit (usually) that you don't want anyone to replace the implementation at runtime (as doing so would require creating a new LoginController.)

x0n
  • 51,312
  • 7
  • 89
  • 111
  • Yes, property injection allows replacing of logic in already created objects, but this can be a disadvantage of Property Injection at the same time. I'm looking for advantages :) – Ruslan May 06 '12 at 14:12