Castle was born from the Apache Avalon
project as an attempt to build a very simple inversion of control
container. The overall design is the result of more than a year in
contact with masters of the Avalon land like Carsten Ziegeler, Leo Sutic, Leo Simons and Berin Loritsch to name a few.
After several discussions about what a useful container must
solve, Castle's first draft was created. Several changes have taken
place before reaching its current form.
However, Castle had a more ambitious agenda. Tools like DynamicProxy
and the Management Extensions were used by several projects and a
proper home to support those users and build a community was necessary.
Along the same lines, these tools had nothing in common with Avalon's
mission, so we had to find a more suitable home for them.
An
empty space lies in the open source community between those who happen
to use CLI (Common Language Infrastructure) running on Windows (.Net
Framework from MS) and|or Linux (Mono). This empty space represents the
lack of a model for how to build a Good Application (tm).
Of course there are several
factors that contribute to creating a Good Application, and from our
standpoint one them is good architecture. Good architecture is
accomplished by having cohesion, single classes with well-defined
responsibilities, orthogonallity and loosely coupled components
(providing reusability among different projects).
From our experience,
inversion of control can help even an inexperienced team to accomplish
good architecture. However, the usage of this design principle is a
matter of taste, and people should not feel obligated to use it.
Castle's goal is to provide a
set of standalone tools and an inversion of control container
implementation in order to create a meeting point for these tools.
新建c#工程
添加对Dll的引用Castle.DynamicProxy.dll
Castle.MicroKernel.dll
Castle.Model.dll
Castle.Windsor.dll
新建 接口
namespace first.Castle
{
public interface Interface1
{
void add();
void del();
}
} 新建实现类
namespace first.Castle
{
public class ClassTest:Interface1
{
public void add()
{
System.Diagnostics.Debug.Assert(false,"add");
}
public void del()
{
System.Diagnostics.Debug.Assert(false,"del");
}
}
}
读取配置
using System;
using System.Diagnostics;
using Castle.MicroKernel;
using Castle.Windsor;
//http://www.blogjava.net/wujun
namespace first.Castle
{
public class MindDumpContainer
{
private static MindDumpContainer _singleMindDupContainer = null;
private static WindsorContainer _windsorContainer = null;
private MindDumpContainer()
{
_windsorContainer =
new WindsorContainer("配置文件路径 windsor.config", AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
if (_windsorContainer == null)
{
throw new Exception("Failed initialize WindsorContainer.");
}
}
//单态
public static MindDumpContainer GetInstance()
{
if (_singleMindDupContainer == null)
{
_singleMindDupContainer = new MindDumpContainer();
}
return _singleMindDupContainer;
}
/**//// <summary>
/// Get contained object by its name
/// </summary>
/// <param name="key">key of the object when registered</param>
/// <returns>the object</returns>
public object GetObject(string key)
{
Debug.Assert(_windsorContainer != null);
try
{
return _windsorContainer[key];
}
catch (ComponentNotFoundException ex)
{
throw new Exception(string.Format("No componet with the passed key: {0}", key), ex);
}
}
public object GetObject(Type type)
{
Debug.Assert(_windsorContainer != null);
try
{
return _windsorContainer[type];
}
catch (ComponentNotFoundException ex)
{
throw new Exception(
"error");
}
}
}
}
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<!-- This configuration file contains castle windsor config file, including facilities and components -->
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
//http://www.blogjava.net/wujun
<components>
//service是配置接口 逗号后面是命名空间
//type 是具体的实现 逗号后面是命名空间
<component id="Classtest" service="first.Castle.Interface1,first.Castle" type="first.Castle.ClassTest,first.Castle" />
</components>
</configuration> 调用 只要
Interface1 iface = (Interface1) MindDumpContainer.GetInstance().GetObject(typeof(Interface1));
iface.add();
就可以了 。。
和spring 太象了 。。
官方网站:
http://www.castleproject.org/index.php/Main_Pagehttp://www.codeproject.com/cs/design/introducingcastle.asp