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);
}