0
可能重複:
Dependency injection through constructors or property setters?與構造器注入相比,屬性注入有什麼優勢嗎?
我珍玩如果物業注射在構造器注入有什麼優點:
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;
}
}
是的,屬性注入允許替換已創建的對象中的邏輯,但這可能是同時注入物業的一個缺點。我在尋找優勢:) – Ruslan