b、EmailService

namespace MonitorService
{
    public interface IEmailService
    {
        string To { get; set; }

        string Subject { get; set; }

        string Body { get; set; }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        void SendEmail(string to, string subject, string body);
    }

    public class MockEmailService : IEmailService
    {
        public string To { get; set; }

        public string Subject { get; set; }

        public string Body { get; set; }

        public void SendEmail(string to, string subject, string body)
        {
            To = to;
            Subject = subject;
            Body = body;
        }
    }
}

  在配置文件监控类的Analyze方法内部实现和桩对象和模拟对象的通信:

  c、ConfigFileMonitor

using System;
using MonitorService.Contract;

namespace MonitorService.Impl
{
    /// <summary>
    /// 配置文件监控管理类
    /// </summary>
    public class ConfigFileMonitor : IFileMonitor
    {
        public ILogService LogService { get; set; }

        public IEmailService EmailService { get; set; }

        private static readonly string configFileType = ".config";//配置文件后缀

        public void Analyze(string fileName)
        {
            try
            {
                if (fileName.Length <= configFileType.Length)
                {
                    LogService.AppendLog(string.Format("input filename({0}) is too short", fileName));
                }
            }
            catch (Exception ex)
            {
                EmailService.SendEmail("mailto:jeffwong@cnblogs.com jeffwong@cnblogs.com", "filename check", ex.Message);
            }
        }
    }
}