Spring. Net study Note 6-dependency injection (Application)

Source: Internet
Author: User

Speaking of advanced {
Tagshow (Event)
} "> Language programming, we will think {
Tagshow (Event)
} "> Design Pattern. When it comes to design patterns, we will talk about how to solve coupling. And spring. one of the IOC containers of net is decoupling. Its most typical application is dependency injection (DI), which is one of the best decoupling methods. Next I will talk about the application scenarios of dependency injection.

I have simulated three different scenarios and can learn the importance of using dependency injection together.

  The following are the conditions for the Application Scenario:Human Use {
Tagshow (Event)
} "> Tool labor.

  1. /** // <Summary>
  2. /// Abstract human
  3. /// </Summary>
  4. Public abstract class person
  5. {
  6. /** // <Summary>
  7. /// Work with tools
  8. /// </Summary>
  9. Public abstract void work ();
  10. }
  11. Public interface itool
  12. {
  13. /** // <Summary>
  14. /// Use the tool
  15. /// </Summary>
  16. Void usetool ();
  17. }

Copy code

Scenario 1: primitive society:The original man used spears for hunting.

  1. Public class spear: itool
  2. {
  3. Public void usetool ()
  4. {
  5. Console. writeline ("using spears ");
  6. }
  7. }

Copy code

Primitiveperson

  1. Public class primitiveperson: person
  2. {
  3. /** // <Summary>
  4. /// The primitive society uses Spears for hunting
  5. /// </Summary>
  6. Public override void work ()
  7. {
  8. // Know that hunting uses a spear and creates a spear.
  9. Itool tool = new spear ();
  10. Tool. usetool ();
  11. Console. writeline ("hunting with spears ");
  12. }
  13. }

Copy code

From the above code, we can easily see that, although the classic Rys replacement principle is used, the primitiveperson class is coupled with the spear class.

Attachment: 2009-10-26-1.gif

  Scenario 2: Economic and Social:Farming With tools

  1. Public class Hoe: itool
  2. {
  3. Public void usetool ()
  4. {
  5. Console. writeline ("Use a hoe ");
  6. }
  7. }

Copy code

Toolfactory

  1. Public static class toolfactory
  2. {
  3. /** // <Summary>
  4. /// Factory manufacturing tools
  5. /// </Summary>
  6. /// <Returns> </returns>
  7. Public static itool createtool ()
  8. {
  9. Return new hoe (); // create a hoe
  10. }
  11. }

Copy code

Economy Person

  1. Public class economical myperson: person
  2. {
  3. /** // <Summary>
  4. // Economic and Social farming
  5. /// </Summary>
  6. Public override void work ()
  7. {
  8. // You don't need to know what tools are. You only need to know that the factory can buy the tools instead of making the tools by yourself.
  9. Itool tool = toolfactory. createtool ();
  10. Tool. usetool ();
  11. Console. writeline ("farming using economic and social tools ");
  12. }
  13. }
Copy code
From the code above, I can see that, using the classic factory model, economy person is only coupled with the factory, and does not care about how the factory makes tools.

  Scenario 3: current society:Work with tools

  1. Public class computer: itool
  2. {
  3. Public void usetool ()
  4. {
  5. Console. writeline ("use computer ");
  6. }
  7. }

Copy code

Modernperson

  1. Public class modernperson: person
  2. {
  3. /** // <Summary>
  4. /// Obtain the tool from the external
  5. /// </Summary>
  6. Public itool tool {Get; set ;}
  7. /** // <Summary>
  8. /// The current user does not need to know where the computer came from and directly uses it for work.
  9. /// </Summary>
  10. Public override void work ()
  11. {
  12. // I don't know what tools are used and what tools are used, but they are just mechanized office work.
  13. Tool. usetool ();
  14. Console. writeline ("working with tools ");
  15. }
  16. }

Copy code

App. config

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Configuration>
  3. <Configsections>
  4. <Sectiongroup name = "Spring">
  5. <Section name = "context" type = "Spring. Context. Support. contexthandler, spring. Core"/>
  6. <Section name = "objects" type = "Spring. Context. Support. defaultsectionhandler, spring. Core"/>
  7. </Sectiongroup>
  8. </Configsections>
  9. <Spring>
  10. <Context>
  11. <Resource uri = "config: // spring/objects"/>
  12. </Context>
  13. <Objects xmlns = "http://www.springframework.net">
  14. <Description> a simple example of control reversal </description>
  15. <Object ID = "computer" type = "springnetioc. Computer, springnetioc"/>
  16. <Object ID = "modernperson" type = "springnetioc. modernperson, springnetioc">
  17. <Property name = "tool" ref = "computer"/>
  18. </Object>
  19. </Objects>
  20. </Spring>
  21. </Configuration>

Copy code

Program

  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. Iapplicationcontext CTX = contextregistry. getcontext ();
  6. Person = (person) CTX. GetObject ("modernperson ");
  7. Person. Work ();
  8. Console. Readline ();
  9. }
  10. }

Copy code

From the code above, we can see that the modernperson class does not need to know the specific tools to use when handing over objects to spring. Net containers for management. It is just a mechanical task. The configuration file determines what tools are used, and all objects are managed by the spring. Net container. This allows dynamic disassembly and assembly and Component reuse. I personally understand that dependency injection is an enhanced version of the reflection factory.

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.