Castle IOC Container Quick Start

Source: Internet
Author: User
Tags abstract string format

Main content

1. Why should the IOC

2. What is the Castle IOC container

3. Quick Start Sample

4. A few important concepts

One, why should the IOC

IOC (inversion of control or dependency injection) Martin Fowler has been very good in his article, and it is not easy to teach, but simply to explain a few words. We use abstract interfaces to isolate the dependencies between the user and the concrete implementation, but no matter how abstract the end result is to create an instance of the concrete implementation class that creates an instance object of the concrete implementation class that is dependent on the specific implementation, in order to eliminate this creation dependency, Dependencies need to be moved out to the outside of the program (such as configuration files). After using dependency injection, these classes are completely written based on abstract interfaces, so they can be adapted to the requirements change to the fullest extent possible. There are three types of dependency injection, which are constructed sub injection (constructor injection), set value method injection (Setter injection) and interface injection (Interface injection).

Two What is the Castle IOC container

Windsor is an IOC container for castle. It is built on top of microkernel, powerful enough to detect classes and understand what parameters are needed to use them, detect work dependencies between types and types, and provide services or mechanisms to provide alerts when errors occur.

Three Quick Start Sample

Now if we have such a requirement, develop a log component, output the log information to a text file, and then format the output information in schematic code.

1. Create a new C # project and add a reference to the following DLL

Castle.DynamicProxy.dll

Castle.MicroKernel.dll

Castle.Model.dll

Castle.Windsor.dll

2. Writing Services

Since it is a log component, we first add two interfaces, respectively, Ilog and Ilogformatter, so that we are generally called services, that is, the interface that implements some kind of service (described later).

/**//// <summary>
/// 编写:Terrylee
/// 出处:http://terrylee.cnblogs.com
/// </summary>
public interface ILog
{
  void Write(string MsgStr);
}
/**//// <summary>
/// 编写:Terrylee
/// 出处:http://terrylee.cnblogs.com
/// </summary>
public interface ILogFormatter
{
  string Format(string MsgStr);
}

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.