Spring. Net study Note 3-Implementing a simple IOC framework (Exercise)

Source: Internet
Author: User

After talking about so many theories, we can manually implement a simple IOC {
Tagshow (Event)
} "> Framework, which can deepen the theoretical knowledge of IOC.

  I. Ideas

When we use the spring. NET Framework, we first need to instantiate the spring. Net container, and then call the IOC container iobjectfactory {
Tagshow (Event)
} "> The GetObject method in the interface gets the objects in the container. This tells us that to create an IOC container, We need to write a method to get the content of the XML file and declare a dictionary <string, Object> to store the objects in the IOC container, you also need to write a method that can get an object from dictionary <string, Object>.

  Ii. Analysis

To get the content of an XML file, in the 3.5 syntax, I naturally came up with the ability to write data in the following format. {
Tagshow (Event)
} "> For the configuration object, we thought of using the reflection technology to obtain the object instance.
  

  Iii. Code Implementation

1. xml Factory

Myxmlfactory

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. xml. LINQ;
  6. Using system. reflection;
  7. Namespace myselfioc
  8. {
  9. Public class myxmlfactory
  10. {
  11. Private idictionary <string, Object> objectdefine = new dictionary <string, Object> ();
  12. Public myxmlfactory (string filename)
  13. {
  14. Instanceobjects (filename); // instance IOC container
  15. }
  16. /// <Summary>
  17. /// Instance IOC container
  18. /// </Summary>
  19. /// <Param name = "FILENAME"> </param>
  20. Private void instanceobjects (string filename)
  21. {
  22. Xelement root = xelement. Load (filename );
  23. VaR objects = from OBJ in root. Elements ("object") Select OBJ;
  24. Objectdefine = objects. todictionary (
  25. K => K. Attribute ("ID"). value,
  26. V =>
  27. {
  28. String typename = V. Attribute ("type"). value;
  29. Type type = type. GetType (typename );
  30. Return activator. createinstance (type );
  31. }
  32. );
  33. }
  34. /// <Summary>
  35. /// Obtain the object
  36. /// </Summary>
  37. /// <Param name = "ID"> </param>
  38. /// <Returns> </returns>
  39. Public object GetObject (string ID)
  40. {
  41. Object result = NULL;
  42. If (objectdefine. containskey (ID ))
  43. {
  44. Result = objectdefine [ID];
  45. }
  46. Return result;
  47. }
  48. }
  49. }

Copy code

2. Call

Program

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace myselfioc
  6. {
  7. Class Program
  8. {
  9. Static void main (string [] ARGs)
  10. {
  11. Appregistry ();
  12. Console. Readline ();
  13. }
  14. Static void appregistry ()
  15. {
  16. Myxmlfactory CTX = new myxmlfactory (@ "D: \ objects. xml ");
  17. Console. writeline (CTX. GetObject ("persondao"). tostring ());
  18. }
  19. }
  20. }

Copy code

Well, a simple IOC framework is basically implemented.

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.