最近在实际项目中应用了Castle容器实现Ioc设计模式,在项目中很方便地进行接口编程,大大提高程序的可扩展性,已经成功在一个数据库压力测式工具、Web服务压力测试工具和权限管理框架上进行成功的应用。
1、首先为了更方便的创建IWindsorContainer接口并且从外部xml配置文件而不是web.config中加载配置信息,创建一个ContainerBuilder类进行简单的封装:
public static class ContainerBuilder
{
private static readonly object ton = new object();
private static IWindsorContainer container = null;
public static IWindsorContainer Create()
{
string fileName = "Components.config";
HttpContext context = HttpContext.Current;
if (context != null)
{
fileName = context.Server.MapPath("~/App_Data/Components.config");
}
if (container == null)
{
lock (ton)
{
if (container == null)
{
container = new WindsorContainer(new XmlInterpreter(fileName));
}
}
}
return container;
}
}
{
private static readonly object ton = new object();
private static IWindsorContainer container = null;
public static IWindsorContainer Create()
{
string fileName = "Components.config";
HttpContext context = HttpContext.Current;
if (context != null)
{
fileName = context.Server.MapPath("~/App_Data/Components.config");
}
if (container == null)
{
lock (ton)
{
if (container == null)
{
container = new WindsorContainer(new XmlInterpreter(fileName));
}
}
}
return container;
}
}
2、同时我了更方便的实现通过修改配置文件便能切换具体接口的实现,创建IContainerDefaultImplement和它的实现ContainerDefaultImplement主要是用于配置接口的默认实现,如果想修改某个接口的