Castle研究-第一个Castle程序
来源:互联网 发布:碳晶地暖垫 知乎 编辑:程序博客网 时间:2024/04/30 09:57
最近项目中用到Castle ,用的第一感觉就是和spring 及象。。
新建c#工程
添加对Dll的引用
新建 接口
新建实现类
调用 只要
Interface1 iface = (Interface1) MindDumpContainer.GetInstance().GetObject(typeof(Interface1));
iface.add();
就可以了 。。
和spring 太象了 。。
官方网站:
http://www.castleproject.org/index.php/Main_Page
新建c#工程
添加对Dll的引用
Castle.DynamicProxy.dll
Castle.MicroKernel.dll
Castle.Model.dll
Castle.Windsor.dll
新建 接口
namespace first.Castle
{
public interface Interface1
{
void add();
void del();
}
}
{
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");
}
}
}
{
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");
}
}
}
}
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>
<!-- 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_Page
- Castle研究-第一个Castle程序
- Castle
- Castle
- Hearst Castle
- Castle 2.0
- Castle 配置
- PKU_ACM_1164_The Castle
- Castle Igloo
- Castle资料
- Castle Windsor
- Castle介紹
- 接触castle
- 2.1 castle
- The Castle
- The Castle
- Castle介绍
- [usaco] castle
- the castle
- 网络设备驱动和数据链路层的分析
- eDonkey 2000 1.4.5 Pro Final Full
- 我为什么选palm,不选PPC(不是对用户的攻击哦)Zen of Palm
- SQL SERVER tips
- 找工作
- Castle研究-第一个Castle程序
- 关于权限管理的系统的构思
- 14点开始做,现在时间16:23。出错了,头好晕~~~
- 微软笔试题3
- 图片缓存BUG导致Ajax无效的解决
- FCKeditor與asp.net 7/24
- 看《北大牛人唐翔》有感以自勉
- Enum的toString方法
- Microsoft® Office Access 2003 Inside Out