zoukankan      html  css  js  c++  java
  • Castle.Net 基本应用

    什么是Castle

    Castle是针对.NET平台的一个开源项目,从数据访问框架ORM到IOC容器,再到WEB层的MVC框架、AOP,基本包括了整个开发过程中的所有东西,为我们快速的构建企业级的应用程序提供了很好的服务。

    官方网站:http://www.castleproject.org/

    提到Castle就不得不说说IOC容器.

    IOC容器

    IOC(Inversion of Control,反转控制)是一种面向对象设计原则, 是面向对象领域新兴的编程思想 。其原理是组件本身并不与其他组件建立直接的依赖关系,依赖关系将在组件之外的某个地方建立。这样做主要是为了解耦类之间的关系,使得类更加容易测试、重 用,系统更加容易组装和配置。这些先进技术极大促进了软件开发的发展。

    还有一种解释:控制反转意味着在系统开发过程中,设计的类将交由容器去控制,而不是在类的内部去控制,类与类之间的关系将交由容器处理,一个类在需 要调用另一个类时,只要调用另一个类在容器中注册的名字就可以得到这个类的实例,与传统的编程方式有了很大的不同,”不用你找,我来提供给你”,这就是控 制反转的含义.

    其实说明白了, 反转控制就是依赖注入,两者概念一样.最终目标是都是为了降低系统的耦合关系.

    Castle.Net简单应用

    说是简单应用,就不对一些特殊操作做出解释啦,具体的系列文章可以看看TerryLee  的Castle.net系列文章.不过就是版本旧了点.

    我下载的是Castle.Windsor.2.5.3.可以到官网上去下载.

    此例来做一个发送消息的简单例子.来学习Castle.

    首先建立两个接口,一个发送消息接口ISend,一个信息内容格式接口IMessage,这里可以形象的把接口当成服务来看.

    namespace CastleNetDem2.Container
    {
        public interface ISend
        {
            void Send(string Sendform, string SendTo, string Message);
        }
    }
    namespace CastleNetDem2.Container
    {
        public interface IMessage
        {
            string FormMessage(string message);
        }
    }

    然后建立实现了这俩个服务的组件,即实现类.

    namespace CastleNetDem2.Components
    {
       public class MessagePro : IMessage
        {
           
            public string FormMessage(string message)
            {
                return "[" + message + "]";
            }
     
        }
    }
     
     
    namespace CastleNetDem2.Components
    {
        public class SendPro : ISend
        {
            public SendPro() { }
     
            private IMessage _msgObj;
            public SendPro(IMessage msg) 
            {
                this._msgObj = msg;
            }
     
            public void Send(string Sendform, string SendTo, string Message)
            {
                Console.WriteLine("{0}发送给{1}消息:{2}", Sendform, SendTo, _msgObj.FormMessage(Message));
            }
        }
    }

    下面最重要的就是编写配置文件,

    <configSections>
        <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
      </configSections>
      <castle>
        <components>
          <component id="Container.SendPro" service="CastleNetDem2.Container.ISend, CastleNetDem2"
                     type="CastleNetDem2.Components.SendPro, CastleNetDem2">
            <parameters>
              <msg>${Message}</msg>
            </parameters>
          </component>
     
          <component id="Message" service="CastleNetDem2.Container.IMessage, CastleNetDem2"
                     type="CastleNetDem2.Components.MessagePro, CastleNetDem2"/>
        </components>
      </castle>

    这里需要注意SendPro的构造注入需要传入一个Message对象.用${}来引入.

    测试:

    static void Main(string[] args)
            {
                //容器
                IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
     
                ISend send = container.GetService<ISend>();
     
                send.Send("MisYan", "MisJia", "我到家了.");
     
                Console.ReadKey();
            }

    运行结果:

    1

    补充

    component节点的parameters类型说明:

    类型参照

    更具体的说明请访问:http://www.cnblogs.com/Terrylee/archive/2006/04/24/383196.html

    Castle.Windsor.2.5.3 需要引入的 DLL文件Castle.Core.dll,Castle.Windsor.dll

    总结

    其实注入就是不用去手动的实例化你的对象,而由容器帮你完成这一系列的操作.用IBatis.Net结合Castle使用,想必会非常强大..

  • 相关阅读:
    HDU4474 Yet Another Multiple Problem BFS搜索
    HDU4473 Exam 数学分析
    2013ACM多校联合(4)
    POJ1273 网络流...
    HDU4472 Count 递推
    POJ1149 PIGS 网络流
    UVA10881 Piotr's Ants 想法题
    javascript js string.Format()收集
    修改 设置 vs.net 网站 调试 设为 起始页
    【转】HTML5杂谈 概念与现行游戏 割绳子 宝石迷阵
  • 原文地址:https://www.cnblogs.com/xdot/p/5154850.html
Copyright © 2011-2022 走看看