Automatic injection function, making the code more simple and flexible java in the spring, so take that in transplanted to c #, the next step by step analysis of the implementation process
1. Automatic injection Scene Analysis
In asp.net mvc, regardless of what code is logical hierarchy, the final presentation layer for the Controller layer, so we injection point is in the Controller, where we need to replace the default target ControllerFactory, scan the code marks need to inject performs examples of implant
FastControllerFactory is our custom a Controller factory, override CreateController method of marking the AutoWired this argument defined annotations, examples taken from Bean container assignment, we also need the Start method in the Global file, make default plant replacement
Achieve 2.IOC container
c # custom container has a lot of mature open source frameworks such as AutoFac, etc., where we own realization is a lightweight version
Source Address: https://gitee.com/grassprogramming/FastIOC
Let us focus here on how to use asp.net mvc is, first of all we need to Bean object needs to be injected mark, the mark is called Component,
In the Start method asp.net mvc Global file, we need the entire project to be automatically injected into the vessel Bean
Here matters Controller level has been completed, then you need to initialize the IOC container Bean instance method further treatment
GetInstance Bean object is to instantiate the core of the method is very simple, is to create objects through reflection, of which there are two points to note
1) The need to scan all the time in a variable Bean Bean initialization, if there is internal nested object dependency injection, it is necessary to use a recursive, until there is no need to inject the Field
2) I used here is the single-mode embodiment, as there may be performed during testing of B class A dependency injection, injection of A dependency of class B, the conventional process of creating, if the scanned recursively, will into the loop, overflow of memory, the use of a single subject embodiment, it has been created into the dictionary, if the subject is scanned again to be injected, taken directly used, to avoid circular references
3. Other
Controller class are not used in other relies injection, it is necessary to use directly remove the container from the IOC Bean