c# Castle Windsor简单例子
2021-04-11 10:26
标签:ORC init tor container index conf 创建 color c中 Windsor是Castle的IOC框架。需要用到两个dll(Castle.Core.dll和Castle.Windsor.dll)。 1、接口以及接口实现类: 2、创建自定义类WindsorInstaller,继承IWindsorInstaller,实现IWindsorInstaller的Install方法,如下: 3、创建自定义类WindsorInit,单例模式获取ioc容器: 4、mvc中controller中使用: 页面会输出结果:30 这里简单实现Castle Windsor(IOC)。 c# Castle Windsor简单例子 标签:ORC init tor container index conf 创建 color c中 原文地址:https://www.cnblogs.com/qk2014/p/9028291.htmlpublic interface ITest
{
int Add(int a, int b);
}
public class Test:ITest
{
public int Add(int a, int b)
{
return a + b;
}
}
public class WindsorInstaller: IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
//单个注册法,还有其他方式
//ITest为接口,Test为接口的实现类
container.Register(Component.For
public class WindsorInit
{
private static WindsorContainer _container;
public static WindsorContainer GetContainer()
{
if (_container == null)
{
_container = new WindsorContainer();
_container.Install(FromAssembly.This());
}
return _container;
}
public void CloseContex()
{
_container.Dispose();
}
}
public class HomeController : Controller
{
private ITest _test;
public ActionResult Index()
{
WindsorContainer container = WindsorInit.GetContainer();
_test = container.Resolve
上一篇:三种方式构建C#单例模式
文章标题:c# Castle Windsor简单例子
文章链接:http://soscw.com/index.php/essay/74220.html