L2J and Dependency Injection

This is not a Support area! Discuss about the Server here. Non-Server related discussion goes in Off-Topic Discussion.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

L2J and Dependency Injection

Post by Zoey76 »

Trying to improve L2J architecture, testability and overall design I have been experimenting with dependency injection in the core.

https://bitbucket.org/l2jserver/l2j-ser ... e6f2ec3cbd

I've used Spring Context (could have used Guice, but I may use more of the Spring Framework in the future), the basic idea is to create managed beans and avoid Singleton anti-pattern that we have favored so much in the past.

In this example server is configured to use a new application context and all DAOs are turned into injectable @Repository beans, also player loading and creation is segregated to a PlayerService class.

This @Autowire(d) components are not visible in datapack due compilation, need to experiment with AspectJ a little bit further to get it working.

Before moving forward on this approach, I'll work more on completing this DAO/Repository classes removing data access code from classes such as L2PcInstance.

I'd like to know what do you think about this approach, I believe it could help go towards TDD and more flexible and extensible Core implementation.
Powered by Eclipse 4.30 ๐ŸŒŒ | Eclipse Temurin 21 โ˜• | MariaDB 11.2.2 ๐Ÿ—ƒ๏ธ | L2J Server 2.6.3.0 - High Five ๐Ÿš€

๐Ÿ”— Join our Discord! ๐ŸŽฎ๐Ÿ’ฌ
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: L2J and Dependency Injection

Post by jurchiks »

Some of these injections look like they'd reduce CPU usage and speed of access (`DAOFactory.getInstance().getXDAO()`) and, conversely, potentially slightly increase memory usage due to persistent in-memory references that these calls avoided. No idea which is better, but I'd probably go for reduced CPU usage since RAM is cheap and plentiful these days.
However, some seem to be pointless (injecting an instance of a class when only static methods of that class are used). That's essentially an unused instance.
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: L2J and Dependency Injection

Post by Zoey76 »

jurchiks wrote: โ†‘Mon May 04, 2020 12:26 am Some of these injections look like they'd reduce CPU usage and speed of access (`DAOFactory.getInstance().getXDAO()`) and, conversely, potentially slightly increase memory usage due to persistent in-memory references that these calls avoided. No idea which is better, but I'd probably go for reduced CPU usage since RAM is cheap and plentiful these days.
However, some seem to be pointless (injecting an instance of a class when only static methods of that class are used). That's essentially an unused instance.
Agreed, that not all would benefit right away, this is not really about CPU or RAM, it's about design and extensibility, you can neglect most of this injection costs.
Powered by Eclipse 4.30 ๐ŸŒŒ | Eclipse Temurin 21 โ˜• | MariaDB 11.2.2 ๐Ÿ—ƒ๏ธ | L2J Server 2.6.3.0 - High Five ๐Ÿš€

๐Ÿ”— Join our Discord! ๐ŸŽฎ๐Ÿ’ฌ
User avatar
nonom
L2j Veteran
L2j Veteran
Posts: 649
Joined: Wed Mar 11, 2009 10:34 pm
Location: Magmeld

Re: L2J and Dependency Injection

Post by nonom »

I was using DI with PHP which is also awesome to decouple for testing. Sounds great.
Image
"There are three kinds of people in this world, those who can count and those who can't"
Mantosh
Posts: 21
Joined: Thu Aug 29, 2019 5:54 pm

Re: L2J and Dependency Injection

Post by Mantosh »

Great work! :clap: Keep it coming
odin
Posts: 1
Joined: Tue Apr 26, 2022 11:49 pm

Re: L2J and Dependency Injection

Post by odin »

Although this is an old thread I must say that adding DI is a big step forward. Specially if you aim for TDD. I honestly don't know how you can test the code now.

Aside from TDD, you could also be interested in DDD. I think it could be pretty good for this kind of complex projects.
buute
Posts: 1
Joined: Fri Jan 06, 2023 7:51 pm

Re: L2J and Dependency Injection

Post by buute »

Hi,

A few of these infusions see like they'd decrease CPU utilization and speed of get to alternately, possibly somewhat increment memory utilization due to diligent in-memory references that these calls dodged. No thought which is superior, but I'd likely go for diminished CPU utilization since Slam is cheap and ample these days. However, a few appear to be inconsequential (infusing an occasion of a lesson when as it were inactive strategies of that course are utilized). That's basically an unused occurrence.thanks! i am working on furniture.
Australia Base project for https://www.aushoppinghub.com/ for Online Shopping of Furniture and Homewares.
Post Reply