Castle IOC学习

http://www.castleproject.org/

Castle 小巧 配置简单

配置文件

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3     <configSections>
 4         <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"/>
 5         
 6     </configSections>
 7     <castle>
 8         <components>
 9             <component id="txtLog"
10                                     service="ConsoleApplication2.ILog,ConsoleApplication2"
11                                     type="ConsoleApplication2.TextFileLog,ConsoleApplication2">
12                 <parameters>
13                     <target>C:\Log.txt</target>
14                 </parameters>
15             </component>
16             <component id="format" 
17                                     service="ConsoleApplication2.ILogFormat,ConsoleApplication2"
18                                     type="ConsoleApplication2.TextFormat,ConsoleApplication2">
19                 <paramters>
20                     <target>ConsoleApplication2</target>
21                 </paramters>
22             </component>
23         </components>
24         
25     </castle>
26 </configuration>

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using Castle.Core;
 6 using Castle.Windsor;
 7 using Castle.Windsor.Configuration.Interpreters;
 8 using Castle.MicroKernel;
 9 
10 
11 namespace ConsoleApplication2
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             IKernel kernel;
18             Castle.Core.Resource.ConfigResource source = new Castle.Core.Resource.ConfigResource();
19             XmlInterpreter interpreter = new XmlInterpreter(source);
20             WindsorContainer windsor = new WindsorContainer(interpreter);
21             kernel = windsor.Kernel;
22             
23 
24             ILog log=(ILog)kernel["txtLog"];
25             log.Write(DateTime.Now.ToShortDateString());
26             
27         }
28     }
29 }



 

Castle由以下子项目组成:MonoRail是一个MVC框架,ActiveRecord是一个使用NHibernate的企业数据映射,MicroKernel/Windsor Container 是一个IOC容器。

posted on 2012-04-09 15:03  小邪魔  阅读(300)  评论(0)    收藏  举报

导航