Introduce Castle IOC into project development for "dependency injection"

Source: Internet
Author: User

Generally, the IOC implementation step is --> Create a container --> Add a component --> get a component --> use a component.ArticleThis article describes the four phases.

1. Create a container
Here, I will take a ready-made project for analysis. First, we need to build an IOC container. In the project, a container-type container is set up to be responsible for IOC container construction and component addition.CodeAs follows:

1 using system;
2 using system. Collections. Generic;
3 using system. text;
4
5 using Castle. Windsor;
6 using Castle. microkernel;
7 using Castle. Windsor. configuration. interpreters;
8 using TMS. Framework. exceptions;
9
10 namespace TMS. Framework
11 {
12 /// <summary>
13 // The IOC container which provides interface to find service.
14 /// </Summary>
15 public sealed class container
16 {
17 /// <summary>
18 // windsorcontainer object
19 /// </Summary>
20 private windsorcontainer Windsor;
21 // public windsorcontainer Windsor
22 //{
23 // get {return Windsor ;}
24 //}
25
26 private ikernel kernel;
27 public ikernel Kernel
28 {
29 get {return kernel ;}
30}
31
32 /// <summary>
33 // This
34 /// </Summary>
35 Private Static readonly container instance = new container ();
36 public static container instance
37 {
38 get {return container. instance ;}
39}
40
41 // <summary>
42 // construction method.
43 // initialization IOC.
44 /// </Summary>
45 public container ()
46 {
47 try
48 {
49 // IOC containers establishment, and through the most dynamic configuration file by adding components.
50 Castle. Core. Resource. configresource source = New Castle. Core. Resource. configresource ();
51 xmlinterpreter interpreter = new xmlinterpreter (source );
52 Windsor = new windsorcontainer (Interpreter );
53 kernel = Windsor. kernel;
54}
55 catch (bizsystemexception BSE)
56 {
57 tmsexcetionbase. processbizexception (BSE );
58}
59}
60
61 // <summary>
62 // returns a component instance by the type of service.
63 // </Summary>
64 // <typeparam name = "T"> </typeparam>
65 // <returns> </returns>
66 public t resolve <t> ()
67 {
68 return (t) kernel [typeof (t)];
69}
70
71 /// <summary>
72 // returns a component instance by the service name.
73 /// </Summary>
74 // <Param name = "service"> </param>
75 /// <returns> </returns>
76 private object resolve (type Service)
77 {
78 return kernel [SERVICE];
79}
80
81 /// <summary>
82 // returns a component instance by the service name.
83 /// </Summary>
84 /// <Param name = "key"> </param>
85 /// <returns> </returns>
86 private object resolve (string key)
87 {
88 return kernel [Key];
89}
90
91 /// <summary>
92 // release resource that be container used.
93 /// </Summary>
94 public void dispose ()
95 {
96 kernel. Dispose ();
97}
98}
99}
100
Note: processbizexception () in the preceding code is a static method for customizing the tmsexcetionbase class for exception handling.

2. Add Components
During the process of adding components, when creating a container in the previous section, we have specified that the component should be searched in the configuration file. Take a look at the following code:

1castle. Core. Resource. configresource source = New Castle. Core. Resource. configresource ();
2 xmlinterpreter interpreter = new xmlinterpreter (source );
3 Windsor = new windsorcontainer (Interpreter );
In the <configsections> Configuration section of the configuration file, first Add the following configuration. The role of this configuration is not explained here. For details, refer to my previous article:

<Configsections>
<! -- Specify the castle section Handler -->
<Section name = "Castle" type = "Castle. Windsor. configuration. appdomain. castlesectionhandler, Castle. Windsor"/>

I defined the castle configuration node above. How can I configure the castle configuration Node? Let's see the following configuration code:

1 <Castle>
2 <include uri = http://www.cnblogs.com/beniao/admin/file://configuration//TMS.Role.config/>
3 <include uri = http://www.cnblogs.com/beniao/admin/file://configuration//TMS.UserInfo.config/>
4 </Castle>
What does this configuration mean? When a container is created in the container class, it is specified in the configuration file to find the component to be added to the container. The component is configured with the castle configuration node in the configuration file, in the detailed configuration of Castle, we can see that the include statement is used to load the specified component configurator. <include uri = file: // configuration // TMS. userinfo. config/>, which indicates the TMS in the configuration file under the root directory. userinfo. config is a detailed component configurator. Let's take a look at the specific configuration of the component configurator:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <configuration>
3 <Components>
4 <component id = "userinfo" service = "TMS. Service. iuserinfoservice, TMS. Service"
5 type = "TMS. component. userinfocomponent, TMS. component"/>
6 </components>
7 </configuration>
8

What does the above component configuration mean? I will give you an analysis here.
Service = "TMS. Service. iuserinfoservice, TMS. Service" ---- represents the Service (both interfaces, In the TMS. Service Program Set)
Type = "TMS. component. userinfocomponent, TMS. component" -- represents components (classes that implement services, under TMS. Component)
Now you should know exactly what the above configuration means, that is, TMS. the userinfocomponent component class in the component assembly implements TMS. the userinfoservice (Interface) in the servie assembly ).

3. Getting components and using components
after half a day, the final stage is finally reached. The previous steps are all about preparing for this step. We are really happy to be at the end. Let's take a look at how to get a component and use it.
the input interface allows the container to automatically find the component that implements the interface.
iuserinfoservice user = container. instance. resolve ();
The following is the call code:
1 protected void page_load (Object sender, eventargs E)
2 {
3 this. gridview1.datasource = user. inquireall ();
4 This. gridview1.databind ();
5}
the running result is as follows:

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.