一、控制反转:
从简单的代码示例入手:
/// <summary>
/// 邮件服务类
/// </summary>
public class EmailService
{
public string SendMessage()
{
return "发送通知邮件";
}
}
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private EmailService service;
public NotifycationSystem()
{
service = new EmailService(); //邮件通知类必须精确的知道创建和使用了哪种类型的服务,此处高耦合了。
}
public string InterestingEventHappened()
{
return service.SendMessage();
}
}
共两个类,一个邮件服务类,一个邮件通知类,邮件通知类依赖于邮件服务类。邮件通知类必须精确的知道创建和使用了哪种类型的服务,此处高耦合了。
改进一:在两代码块中引入抽象层,提取接口。
/// <summary>
/// 邮件服务接口
/// </summary>
public interface IMessageService
{
string SendMessage();
}
/// <summary>
/// 邮件服务类
/// </summary>
public class EmailService : IMessageService
{
public string SendMessage()
{
return "发送通知邮件";
}
}
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService service;//邮件通知类保存服务实现的接口
public NotifycationSystem()
{
service = new EmailService();
}
public string InterestingEventHappened()
{
return service.SendMessage();
}
}
上面将依赖具体实现改为了依赖接口,减少了部分耦合。但是邮件服务类还是在邮件通知类内实例化的,也就是说邮件通知类还是要完全知道邮件服务类的具体细节。
改进二:将选择抽象实现的责任移到服务消费者类的外部。
/// <summary>
/// 第二层抽象: 服务定位器
/// </summary>
public interface IServiceLocator
{
IMessageService GetMessageService();
}
/// <summary>
/// 第一层抽象:邮件服务接口
/// </summary>
public interface IMessageService
{
string SendMessage();
}
/// <summary>
/// 邮件服务类
/// </summary>
public class EmailService : IMessageService
{
public string SendMessage()
{
return "发送通知邮件";
}
}
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService service;//邮件通知类保存服务实现的接口。
public NotifycationSystem(IServiceLocator locator)
{
service = locator.GetMessageService();//实现依赖关系被转移到类外。
}
public string InterestingEventHappened()
{
return service.SendMessage();
}
}
扩展一:弱类型服务定位器。/// <summary>
/// 第二层抽象: 服务定位器
/// </summary>
public interface IServiceLocator
{
object GetService(Type serviceType);
}
/// <summary>
/// 第一层抽象:邮件服务接口
/// </summary>
public interface IMessageService
{
string SendMessage();
}
/// <summary>
/// 邮件服务类
/// </summary>
public class EmailService : IMessageService
{
public string SendMessage()
{
return "发送通知邮件";
}
}
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService service;//邮件通知类保存服务实现的接口。
public NotifycationSystem(IServiceLocator locator)
{
service = (IMessageService)locator.GetService(typeof(IMessageService));//实现依赖关系被转移到类外。
}
public string InterestingEventHappened()
{
return service.SendMessage();
}
}
弱类型服务定位器使得这种模式更加灵活,因为他允许请求任意类型的服务类型。采用Type类型的参数,并返回一个非类型化的示例,也就是一个object类型对象。
扩展二:泛型方法。
/// <summary>
/// 第二层抽象: 服务定位器
/// </summary>
public interface IServiceLocator
{
T GetService<T>();//泛型接口
object GetService(Type serviceType);
}
/// <summary>
/// 第一层抽象:邮件服务接口
/// </summary>
public interface IMessageService
{
string SendMessage();
}
/// <summary>
/// 邮件服务类
/// </summary>
public class EmailService : IMessageService
{
public string SendMessage()
{
return "发送通知邮件";
}
}
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService service;//邮件通知类保存服务实现的接口。
public NotifycationSystem(IServiceLocator locator)
{
service = locator.GetService<IMessageService>();//实现依赖关系被转移到类外。
}
public string InterestingEventHappened()
{
return service.SendMessage();
}
}
泛型方法,让依赖反转代码看上去更加高效优雅。
二、依赖注入:
1.构造函数注入:
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService _service;
public NotifycationSystem(IMessageService service)//构造函数注入
{
_service = service;
}
public string InterestingEventHappened()
{
return _service.SendMessage();
}
}
2.属性注入:
/// <summary>
/// 邮件通知类
/// </summary>
public class NotifycationSystem
{
private IMessageService MessageService { get; set; }
public string InterestingEventHappened()
{
if (MessageService == null)
{
throw new InvalidOperationException("服务类型为赋值!");
}
return MessageService.SendMessage();
}
}