Comparison of Castle IOC container and spring.net configuration

Source: Internet
Author: User
Tags comparison constructor

I personally do not understand spring.net, this article is just a simple example to compare the difference between the configuration. In the Castle IOC container, the concept of automatic assembly (auto-wiring) is proposed, whereby the container automatically manages the dependencies between components, and we do not have to write our own XML configuration files to configure dependencies between components. Spring.net is also supported for automatic assembly, but it is not recommended, it runs through the idea that everything is an XML configuration, which is the biggest difference between the two.

With regard to automated assembly, proponents of spring.net think that letting containers be managed automatically gives us no control over component dependencies, and if it's an XML configuration, it lets us know what we're doing, what dependencies we specify, and how we can control and manage them; and from Castle Supporters of the IOC argue that if the container is not managed automatically, the manual configuration becomes very complex and the configuration files become cumbersome, and management can become difficult if there are a lot of components in the system.

Let's look at a simple example of a component mymaincomponent that relies on MyComponent1, MyComponent2, and it also needs to receive an integer parameter in the constructor.

//出处:http://terrylee.cnblogs.com
public class MyMainComponent
{
  MyComponent1 _com1;
  MyComponent2 _com2;
  int _i;
  public MyMainComponent(MyComponent1 com1,MyComponent2 com2,int i)
  {
    this._com1 = com1;
    this._com2 = com2;
    this._i = i;
  }
}
public class MyComponent1
{
  public MyComponent1()
  {
    //
  }
}
public class MyComponent2
{
  public MyComponent2()
  {
    //
  }
}

If you use Spring.net, which uses XML to connect components, the configuration file needs to specify each object and its dependencies in the configuration file, while distinguishing between the constructor and other methods in the configuration file:

<!--出处:http://terrylee.cnblogs.com-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <object id="myManComponent" class="CastleDemo.MyMainComponent, CastleDemo">
    <constructor-arg>
      <ref object="mycomponent1" />
    </constructor-arg>
    <constructor-arg>
      <ref object="mycomponent2" />
    </constructor-arg>
    <constructor-arg>
      <value>1</value>
    </constructor-arg>
  </object>
  <object id="mycomponent1" class="CastleDemo.MyComponent1, CastleDemo" />
  <object id="mycomponent2" class="CastleDemo.MyComponent2, CastleDemo" />
</configuration>

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.