What is the difference between the SCALAROUTING of the Play Frame, depending on the difference between the injection router and the static router?

What is the difference between the dependency injection router and the static router in ScalaRouting of the Play framework?

The Play framework documentation instructs to use the dependency injection router when creating a new Play application using the Play seed Activator template.

But I am still confused about the two routers.< /p>

If anyone knows, please teach me.

in short

Dynamic routers can better control controller instantiation. It allows you to use dependency injection framework to manage dependencies and make controller testing easier. Static routers can be preferred for prototyping because of it The default setting. It is also an obvious choice for assets because they are also static.

Extended answer

It all starts with a static router. In Play 2.0, you can only control The controller is created as a class with static methods (in Java) or as an object (in Scala). Its design takes into account that MVC controllers are stateless, so you don’t need multiple instances of such class. However, static The problem with methods and objects is that they are difficult to unit test, especially mock or stub dependencies are the problem. Dependency injection is also very painful.

Thanks for the positive criticism, Play 2.1 introduced new mechanism to deal with this problem, It allows dynamic object instantiation. From this point on, you can choose between the two routing methods by placing @ in front of the class name in the path file.

Before 2.4 dynamic creation, in The so-called Global object is completed using the getControllerInstance() method, but this mechanism was removed in 2.4 and replaced with a more complex dependency injection method.

Static routers are mainly used for very simple use cases and due to backward compatibility However, for newer projects, I recommend using a dynamic router because it does not have the shortcomings of its predecessor.

The static router in the ScalaRouting that relies on the injection router and the Play framework What’s the difference?

The Play framework documentation instructs to use the dependency injection router when creating a new Play application using the Play seed Activator template.

But I am still confused about the two routers.< /p>

If anyone knows, please teach me.

In short

Dynamic routers can be better Ground control of controller instantiation. It allows you to use a dependency injection framework to manage dependencies and make controller testing easier. Static routers can be preferred for prototyping because it is set by default. It is also an obvious choice for assets because of them It is also static.

Extended answer

It all starts with a static router. In Play 2.0, you can just create a controller with static methods (in Java) or A class as an object (in Scala). It is designed to take into account that MVC controllers are stateless, so you don’t need multiple instances of such class. However, the problem with static methods and objects is that they are difficult to unit test , Especially mocking or stubbing dependencies are the problem. Dependency injection is also very painful.

Thanks for the positive criticism, Play 2.1 introduced new mechanism to deal with this problem, it allows dynamic object instantiation. From this point on, You can choose between the two routing methods by placing @ in front of the class name in the path file.

Before 2.4 dynamic creation, use the getControllerInstance() method in the so-called Global object to complete, But this mechanism was removed in 2.4 and replaced with a more complex dependency injection method.

Static routers are mainly used for very simple use cases, and due to backward compatibility, but for newer projects, I recommend using Dynamic router, because it does not have the shortcomings of its predecessor.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2343 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.