Castle.net Basic Applications

Source: Internet
Author: User

What is Castle

Castle is an open source project for the. NET platform, from the data Access framework ORM to the IOC container, to the MVC framework of the web layer, to AOP, which basically includes everything in the entire development process, providing a great service for us to quickly build enterprise-class applications.

Official website: http://www.castleproject.org/

Referring to Castle, we had to talk about the IOC container.

IOC container

The IOC (inversion of control, inversion control) is an object-oriented design principle and a new programming idea in object-oriented field. The principle is that the component itself does not establish a direct dependency with other components, and the dependencies will be established somewhere outside the component. This is mainly to understand the relationship between the coupling class, making the class easier to test, reuse, and the system easier to assemble and configure. These advanced technologies have greatly facilitated the development of software development.

Another explanation: control reversal means that in the system development process, the design of the class will be left to the container to control, rather than within the class control, the relationship between the class and the class will be handled by the container, a class when the need to call another class, as long as the call to another class in the container registered name can be obtained instances of this class And the traditional way of programming is very different, "do not you find, I come to provide you", this is the meaning of control reversal.

In fact, it is clear that inversion control is a dependency injection, and the concept is the same. The ultimate goal is to reduce the coupling relationship of the system.

Castle.net Simple Application

Say is a simple application, do not explain some special operations, specific series of articles can look at Terrylee's Castle.net series of articles. But the version is a bit old.

I downloaded the castle.windsor.2.5.3. can go to the official website to download.

This example makes a simple example of sending a message. To learn castle.

First set up two interfaces, a send message interface isend, an information content format interface imessage, here can image of the interface as a service to see.

Namespace Castlenetdem2.container
{
    Interface Isend
    {
        void Send (string Message);
    }
}
Namespace Castlenetdem2.container
{
    Interface IMessage
    {
        String Formmessage (string message);
    }
}

It then builds the components that implement the two services, the implementation class.

Namespace Castlenetdem2.components
{
   Class Messagepro:imessage
    {
       
        String Formmessage (string message)
        {
            "]";
        }
    }
}
Namespace Castlenetdem2.components
{
    Class Sendpro:isend
    {
        Public Sendpro () {}
        Private IMessage _msgobj;
        
        {
            This._msgobj = msg;
        }
        void Send (string Message)
        {
            Console.WriteLine ("{0} sent to {1} message: {2}", Sendform, SendTo, _msgobj.formmessage (message));
        }
    }
}

The next most important thing is to write the configuration file,

<configsections> 
    <nametype= "Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, castle.windsor"/ >   
  </configsections> 
  <Castle> 
    <components> 
      <IDService= "CastleNetDem2.Container.ISend, CastleNetDem2"  
                 Type= "CastleNetDem2.Components.SendPro, CastleNetDem2"> 
        <parameters> 
          <msg>${message}</msg>    
        </Parameters> 
      </Component> 
      <IDService= "CastleNetDem2.Container.IMessage, CastleNetDem2"  
                 Type= "CastleNetDem2.Components.MessagePro, CastleNetDem2"/> 
    </Components> 
  </Castle> 

It is important to note that the construction injection of Sendpro needs to pass in a Message object. Introduced with ${}.

Test:

void Main (string[] args)
        {
            Container
            New WindsorContainer (new Xmlinterpreter ());
            Isend send = container. Getservice<isend> ();
            Send. Send ("I'm home.");
            Console.readkey ();
        }

Operation Result:

Add

Description of the parameters type of the component node:

For more specific instructions, please visit: http://www.cnblogs.com/Terrylee/archive/2006/04/24/383196.html

castle.windsor.2.5.3 the DLL file that needs to be introduced Castle.core.dll,castle.windsor.dll

Summarize

In fact, the injection is not to manually instantiate your object, and the container to help you do this series of operations. Using ibatis.net combined with castle, it will surely be very powerful.

Castle.net Basic Applications

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.