I wanted to use Ninject for a while and then searched for a long time. I didn't find any details about how to use Ninject, so I just wrote a few articles about Ninject.
- 1.Dependency injection andIOC
For the concept of dependency injection and IOC, click here to read the previous articles.
- 2.Environment preparation
- Development Environment: WIN7 + VS2010 SP1
- Ninject: This section is based on Ninject2.2.0.0-release-net-4.0. Click here to open Ninject's official website for download. It should be noted that although there are Ninject projects on CodePlex, they are basically not updated. So don't download it from there. Download Ninject from its official website.
3. Simple Example
Ø Requirement Description:
The system has a task message. Each task message has a message number. The message number can be generated based on the base number in the configuration file or a field in the database.
Steps:
1) define a message interface:
Public interface IMessage
{
/// <Summary>
/// Get the message number
/// </Summary>
/// <Returns> </returns>
String GetMsgNumber ();
}
2). Get the message number from the configuration file to implement the message interface. The Code is as follows:
Public class MessageFile: IMessage
{
Public string GetMsgNumber ()
{
Return "get the message number from the file! ";
}
}
3). Obtain the message number from the database to implement the message interface. The Code is as follows:
Public class MessageDB: IMessage
{
Public string GetMsgNumber ()
{
Return "reading message numbers from data ";
}
}
4) The following are the interfaces and interfaces to be registered in our application. The Code is as follows:
Using Ninject. Modules;
Public class MessageModule: NinjectModule
{
Public override void Load ()
{
// Bind the interface and specify the implementation of the interface.
Bind <IMessage> (). To <MessageDB> ();
}
}
We can see through the code that we only need to inherit from NinjectModule and reload its Load () method, bind the interface in the Load () method and specify its specific implementation.
5). The Code is as follows:
Using Ninject;
Static void Main (string [] args)
{
Using (var kernal = new StandardKernel (new MessageModule ()))
{
Var msg = kernal. Get <IMessage> ();
Var msgNo = msg. GetMsgNumber ();
Console. WriteLine (msgNo );
Console. Read ();
}
}
Run the code (Note: before running the code, select version 4.0 in the Framework of the project. Otherwise, "the referenced assembly" Ninject "is not resolved ", because it is not in the current target framework ". "System." In NETFramework, Version = v4.0, Profile = Client. web, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a "dependency relationship" exception). Finally, we can see that the message displayed on the console is:
Read the message number from the data!
In the code, we use the Get method of Kernal to obtain the corresponding type instance. Throughout the code process, we only bind interfaces and implementations to the Module, then code is implemented elsewhere through the interface type. To obtain the message number from the configuration file, you only need to modify the code in the Custom Module. Undoubtedly, there are many advantages in type dependency and coupling of programs.
4.Next, we will introduce how to useNinjectThere are several methods for dependency injection.
Ø constructor Injection
First, define the class to be initialized using the dependency interface, as follows:
Public class MessageCfg
{
Private readonly IMessage msg;
/// <Summary>
/// Constructed through the interface
/// </Summary>
/// <Param name = "msg"> </param>
Public MessageCfg (IMessage msg)
{
This. msg = msg;
}
Public string GetMsgNumber ()
{
Return this. msg. GetMsgNumber ();
}
}
Then, we can write the code as follows:
Using (var kernal = new StandardKernel (new MessageModule ()))
{
Var msg = kernal. Get <IMessage> ();
Var msgcfg = new MessageCfg (msg); // constructor Injection
Console. WriteLine (msgcfg. GetMsgNumber ());
Console. Read ();
}
Here, when initializing MessageCfg, we only need to use the type obtained by kernal. Get <IMessage> (), without worrying about what it is.
Ø property Injection
First, define the class for property injection, as follows:
Public class MessageCfg
{
// Define interface attributes
Public IMessage Msg {get; set ;}
Public MessageCfg (){}
Public string GetMsgNumber ()
{
Return this. Msg. GetMsgNumber ();
}
}
The Code is as follows:
Using (var kernal = new StandardKernel (new MessageModule ()))
{
// Property Injection
Var msgcfg = new MessageCfg () {Msg = kernal. Get <IMessage> ()};
Console. WriteLine (msgcfg. GetMsgNumber ());
Console. Read ();
}
The other injection methods are similar.
Finally, we can see that the basic steps for using Ninject are very simple. You only need to define interface à to implement interface à to inherit NinjectModule in its Load () specify the corresponding binding interface and interface implementation in the method. initialize Kernal using your Module. Use the Get method to obtain the corresponding implementation.