Castle IOC container component life cycle management

Source: Internet
Author: User
Tags thread

Main content

1. The way of life treatment

2. Customizing the way Life is handled

3. Life cycle Processing

A The way of life treatment

We usually create an instance of a component using the New keyword, so that every time we create it, we use the singleton pattern if we want to have only one instance of the component. In the castle IOC, it supports our control over the instance of a component, which means that we can transparently manage how many instances a component has. The Castle IOC container provides the following kinds of life-processing methods:

L Singleton: Only one instance of a component is created, and all requested clients use the same instance, which is also the default processing method of the Castle IOC container.

L Transient: This approach is the same as the effect we usually use new, for each request is a new instance.

L Perthread: For each thread, the singleton is used, which means that each thread gets the same instance.

L Pooled: How the object pool is handled, and the instances that are no longer needed are saved to an object pool.

Custom: Customizing the way Life is handled.

There are two ways we can specify the life-handling of a component, or singleton if not specified:

1. Using configuration Files

<!--出处:http://terrylee.cnblogs.com-->
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <components>
    <component id="comp1" lifestyle="transient">
      <parameters>
        <para>component1 para</para>
      </parameters>
    </component>>
  </components>
</configuration>

2. Use attribute attributes

//出处:http://terrylee.cnblogs.com
[Transient]
public class MyComponent
{
  public MyComponent()
  {
    //
  }
  public MyComponent(string _Str)
  {
    //
  }
}

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.