Dependency Injection- Dependency Injection is widely used in Java Spring framework. With dependency injection, we don’t have to initialize dependent resources in the application code, which is very convenient.

Can ABAP also support dependency injection at the language level and enjoy the convenience of this design approach? Let’s give it a try.

Try the scene

In real life, each light is controlled by a switch. Press the switch, the light is turned on; Press it again and the light goes out.

Let’s start with a general implementation without dependency injection:

Design an ABAP interface ZIF_SWITCHABLE that provides two methods corresponding to on and off respectively.

Naturally, I have an ABAP class, ZCL_LAMP, that implements the above interface. Each instance of ZCL_LAMP is a lamp.

CLASS ZCL_LAMP IMPLEMENTATION.

method ZIF_SWITCHABLE~OFF.

WRITE: / 'lamp off'.

endmethod.

method ZIF_SWITCHABLE~ON.

WRITE: / 'lamp on'.

endmethod.

ENDCLASS.
Copy the code

Design a switch class that has a member variable mo_Switchable that points to the ZIF_SWITCHABLE interface.

The switch has a “press” method. Once pressed, if the current state is on, then the off method of the mo_Switchable member is called to turn it off and the switch state is set to off. And vice versa.

METHOD push.

IF isswitchon = abap_true.

mo_switchable->off( ).

isswitchon = abap_false.

ELSE.

mo_switchable->on( ).

isswitchon = abap_true.

ENDIF.

ENDMETHOD.
Copy the code

Provide a setter method that injects a variable of type ZIF_SWITCHABLE into the member variable mo_Switchable.

method SET_SWITCHABLE.

mo_switchable = io_switchable.

endmethod.
Copy the code

I put the two classes created so far, ZCL_LAMP and ZCL_SWITCH, into package $ZDEV_INVERSION.

ABAP Summer framework consumption code

As you can see from the code below, ZCL_SWITCH and ZCL_LAMP have a strong dependency. This dependency is manually injected by the application developer by calling the set method.

To summarize, the above code is completely avoidable in Java Spring with the idea of dependency injection.

Line 8: Manually create an instance of ZCL_LAMP.

Line 9: Manually create an instance of ZCL_SWITCH.

Line 11: Call the set method to manually inject the lamp and switch dependencies.

Dependency injection is implemented using the ABAP Summer framework

I personally simulated Java Spring’s dependency injection framework with ABAP and developed a prototype called ABAP Summer, which corresponds to Java’s Spring.

Think about how this simple example is implemented using Java Spring. A Java programmer could easily write the following code using the Spring annotation @Inject without manually instantiating ISwitchable and calling the set method to create the dependency. The Spring framework does this for us.

Now, how do you implement these “magic tricks” with ABAP?

Add @inject into the description field of the ZCL_SWITCH member variable mo_Switchable to tell the ABAP Summer framework that I want the MO_Switchable member to be automatically injected with a correct dependency. What kind of dependency is right? How does the Summer framework know how to inject? Read on.

Note: The ABAP language, unlike Java, does not support annotations at the language level, so the @Inject maintained here on the Description field is just a simulation.

2. Look at ABAP consumption code after the adoption of dependency injection, is it a lot of fresh?

data(summer) = zcl_summer=>get_instance( ).

data(lo_switch) = cast zcl_switch( summer->get_bean( EXPORTING iv_bean_name = 'ZCL_SWITCH' ) ).

lo_switch->push( ).

lo_switch->push( ).
Copy the code

The following figure is a comparison of two sets of consumer code based on ABAP conventional implementation and ABAP dependency injection. It is clear that after ABAP dependency injection is adopted,

These three manual operations mentioned earlier are completely avoided. The GET_BEAN method returns an instance of the switch whose member variable mo_Switchable contains an instance of the automatically injected ZCL_LAMP class.

Line 8: Manually create an instance of ZCL_LAMP.

Line 9: Manually create an instance of ZCL_SWITCH.

Line 11: Call the set method to manually inject the lamp and switch dependencies.

Let’s take a look at the real consumer code in Java Spring and make sure that we invented ABAP Summer as a real dependency injection.

Implementation principle of ABAP Summer dependency injection

The ABAP dependency injection framework is available on Github:

Github.com/i042416/jer…

There are many well-written books on the web about Java Spring dependency injection.

The following is the core implementation of ABAP Summer dependency injection, referring to the above book on Java Spring.

For more of Jerry’s original technical articles, please follow the public account “Wang Zixi” or scan the following QR code: