你的位置:首页 > ASP.net教程

[ASP.net教程]控制反转 (inversion of control)


The inversion of control (IoC) pattern is abstract; it says that one should move dependency creation
out of the consumer class, but it doesn’t talk about exactly how to achieve that.

 

 

public class EmailService{public void SendMessage() { ... }}

 


public interface IMessagingService{void SendMessage();}public class EmailService : IMessagingService{public void SendMessage() { ... }}public class NotificationSystem{private IMessagingService svc;public NotificationSystem(){svc = new EmailService();}public void InterestingEventHappened(){svc.SendMessage();}}