/Files/wenjie/WindowsFormsApplication3.rar
Program.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
namespace WindowsFormsApplication3
{
static class Program
{
public static void Main()
{
//IWindsorContainer container = new WindsorContainer();
//container.AddComponent("myform", typeof(Form1));
//container.AddComponent("httpservicewatcher", typeof(HttpServiceWatcher));
//container.AddComponent("email.notifier", typeof(IFailureNotifier), typeof(EmailFailureNotifier));
//container.AddComponent("alarm.notifier", typeof(IFailureNotifier), typeof(AlarmFailureNotifier));
//container.AddComponent("form.cmponent", typeof(Form1));
IWindsorContainer container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
Form1 form
= (Form1)container["form.component"];
Application.Run(form);
container.Release(form);
}
}
}
HttpServiceWatcher.cs
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication3
{
public class HttpServiceWatcher
{
private string msg;
public string Msg
{
get
{
return msg;
}
set
{
msg = value;
}
}
private IFailureNotifier notifier;
private string url = "default url";
public string Url
{
get
{
return url;
}
set
{
url = value;
}
}
public HttpServiceWatcher(IFailureNotifier notifier)
{
this.notifier = notifier;
}
public void StartWatching()
{
Msg =notifier.Notify();
}
public void StopWatching()
{
}
}
}
IFailureNotifier
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication3
{
public interface IFailureNotifier
{
string Notify();
}
}
AlarmFailureNotifier
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication3
{
public class AlarmFailureNotifier : IFailureNotifier
{
public string Notify()
{
return "警报";
}
}
}
EmailFailureNotifier
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication3
{
public class EmailFailureNotifier : IFailureNotifier
{
public string Notify()
{
return "邮件";
}
}
}
Form1.cs
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private readonly HttpServiceWatcher serviceWatcher;
public Form1()
{
InitializeComponent();
}
public Form1(HttpServiceWatcher serviceWatcher)
: this()
{
this.serviceWatcher = serviceWatcher;
}
private void button1_Click(object sender, EventArgs e)
{
serviceWatcher.StartWatching();
MessageBox.Show(serviceWatcher.Msg);
}
}
}
Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections >
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"/>
</configSections>
<castle>
<components>
<component id="httpservicewatcher" type="WindowsFormsApplication3.HttpServiceWatcher,WindowsFormsApplication3" >
<parameters>
<notifier>${email.notifier}</notifier>
<url>different url </url>
</parameters>
</component>
<component id="email.notifier" service="WindowsFormsApplication3.IFailureNotifier,WindowsFormsApplication3" type="WindowsFormsApplication3.EmailFailureNotifier,WindowsFormsApplication3"></component>
<component id="alarm.notifier" service="WindowsFormsApplication3.IFailureNotifier,WindowsFormsApplication3" type="WindowsFormsApplication3.AlarmFailureNotifier,WindowsFormsApplication3"></component>
<component id="form.component" type="WindowsFormsApplication3.Form1,WindowsFormsApplication3" ></component>
</components>
</castle>
</configuration>
${}就是服务所查找调用的
传递一系列的notifiers给HttpServiceWatcher
假设你不满意HttpServiceWatcher只是接收一个notifier的实例。好的,那么我们就让它接收一个notifiers数组
public class HttpServiceWatcher
{
private IFailureNotifier[] notifiers;
private string url = "default url";
public HttpServiceWatcher(IFailureNotifier[] notifiers)
{
this.notifiers = notifiers;
}
...
<component
id="httpservicewatcher"
type="GettingStartedPart1.HttpServiceWatcher, GettingStartedPart1">
<parameters>
<notifiers>
<array>
<item>${email.notifier}</item>
<item>${alarm.notifier}</item>
</array>
</notifiers>
<Url>different url</Url>
</parameters>
</component>