Architecture to improve program reusability ~ Fifth, reuse is inseparable from reflection and IOC (big vernacular IOC)

Source: Internet
Author: User
Question to be studied: What is IOC? What does it solve? Why is IOC used in our project? Does reflection have a great impact on performance? No reflection. Who has the greatest opinion?

Come on, start to talk about the problem.

1. What is IOC? Needless to say, What containers, frameworks, and big vernacular are. In fact, IOC is to configure some interfaces and their various implementation class names in a file, either XML or. in the config fileProgram"RunTime" dynamically obtains the (resolved) object based on the configuration information, and then the object can be used in the program. The IOC is used to create the object.

2. What does it solve? An interface has three implementations, such as SQL implementation, Oracle implementation, and access implementation. which method is used in the current environment to implement it? In this case, it can be implemented by IOC.

 

 1   //  Unified Specification  2   Public   Interface Irepository <tentity> Where Tentity: Class  3  {  4     Void  Insert (tentity entity );  5   }  6   7   //  SQL implementation  8   Public   Class Sqlrepository <tentity>: irepository <tentity> 9   {  10    Public   Void  Insert (tentity entity)  11   {  12   ...  13   }  14   }  15   16   //  Access Implementation  17   Public  Class Accessrepository <tentity>: irepository <tentity> 18   {  19     Public   Void  Insert (tentity entity)  20   {  21   ...  22   }  23 }

 

3. Why is IOC used in our project? The objective of using IOC in a project is to make the program loosely coupled and abstract.Code

 
1 ClassUserservice2 {3VaRUserrepository =NewUserrepository ();4}

We can see that the userrepository object depends onUserrepositoryWhen we need to change the implementation method of userrepository, it is very difficult. We can only change it in the code, which leads to low program scalability. This is why IOC is introduced in the project, that is: "When the program module has multiple implementation methods, you can reference the IOC method to create an object instance ".

4. Does reflection have a great impact on performance? There are countless people doing countless experiments on this issue. The gap between reflection and non-reflection programs is hard to determine, because it is related to data complexity. Generally, the data volume is several dozen times as small as the data volume, the data size may be hundreds of times higher (when reflection is implemented at the underlying layer). We can see from Microsoft's original code that it sometimes uses a lot of reflection. Yes, I am not opposed to reflection, because I am an object-oriented loyal believer.

5. No reflection. Who has the greatest opinion? This is an interesting question. Who has the greatest comments? I think it is the most "Object-Oriented" opinion, because we cannot imagine how a program without reflection is object-oriented. When a demand is changed, it is necessary to modify your program code. This is a pain for programmers and violates the object-oriented principle. I think C # is a fully object-oriented language, when writing a program, should we be more object-oriented than a little bit of performance, and this waste of performance I think is worth doing.

Faith C #, Faith object-oriented

 

Thank you!

 

 

 

 

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.