1. What is IOC?
The full name of Ioc: Inversion of Control (IoC) is called Control Inversion or Dependency Injection ). The so-called inversion of control refers to the transformation of dependent objects (control), from the dependency objects managed by the current class to the IoC framework to manage these objects, independent from class control to achieve low coupling
2. Implement the IOC mechanism through the configuration file
1. I will not talk much about creating a project. Let's go directly to the topic. First, we need to build an interface ISpiocDao (the class can also be just a simple demonstration in this article.
Interface ISpiocDao
{
String Name {get; set ;}
Void Write ();
} The property Name of a real Name and a data output function Write () are provided in the interface ();
2. Add the class SpiocDao as the implementation class of the interface ISpiocDao.
Public class SpiocDao: System. Configuration. ConfigurationSection, ISpiocDao
{
[System. Configuration. ConfigurationProperty ("Name", IsRequired = true)]
Public string Name {
Get {
Return this ["Name"]. ToString ();
}
Set {
This ["Name"] = value;
}
}
Public void Write ()
{
Console. WriteLine ("I am is come from ISpioDao ");
}
}
First, for SpiocDao, we need to add the Configuration of the attribute Name in the Configuration file. Therefore, this class must inherit the System. Configuration. ConfigurationSection class and also inherit the interface ISpiocDao defined above.
Note: The attribute access attribute is this. name. Why is the attribute orientation in this article in the form of this ["Name, if you carefully read this article, you will find that our Name attribute needs to be configured in the configuration file, so our Name attribute needs to be marked as ConfigurationProperty (indicating that this attribute is configured Dependency Property) ), for details about how to configure dependency properties in the configuration file, refer to System. configuration. configurationSection (this class provides access to the dependency attributes in the configuration file)
3. Start configuring our configuration file
Add a configuration file. For the. net configuration file loading mechanism, I will not detail it here. For details, refer to the msdn
Configure the class to be instantiated <section name = "SpioDao" type = "SpIOC. spiocDao, SpIOC "/> name configuration name this name we will use when loading the class, remember that the name should not be the same as the name before the type comma into the full name of the class, the attributes of this class are configured after the current Assembly name.
The detailed configuration information is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section name = "SpioDao" type = "SpIOC. SpiocDao, SpIOC"/>
</ConfigSections>
<SpioDao Name = "this is come from app. config">
</SpioDao>
</Configuration>
Public static string congfigSecName = "SpioDao ";
Static void Main (string [] args)
{
Var classConfig = System. Configuration. ConfigurationManager. GetSection (congfigSecName );
ISpiocDao ispioc = classConfig as ISpiocDao;
If (ispioc = null)
{
Console. WriteLine ("the config is error ");
}
Else
{
Ispioc. Write ();
Console. WriteLine (ispioc. Name );
}
Console. Read ();
}
So far, a simple code has implemented the configuration file loading class.
Appendix: source code: http://files.cnblogs.com/sunnyblogs/SpIOC.zip