Dependency Injection (DI) is a mechanism that allows your code to access various services and program functionality. Imagine you have a toolbox (DI container), and you can simply ask for the tool you need by name instead of building it yourself.
In simple terms:
This approach helps you instantly see which parts of the program the script interacts with — it's clear what "tools" the script requires.
This not only simplifies script maintenance but also allows for some optimization techniques — when it's clear what the script needs, unused parts can be ignored to improve efficiency.
Using init
properties is a simple and safe way to receive dependencies. Such properties are initialized once at script startup and cannot be changed later.
Example:
ISendInputScriptingApi SendInput { get; init; } // Automatically initialized when the script starts
SendInput.MouseMoveTo(200, 100); // Moves the mouse to X200 Y100
SendInput.MouseRightClick(); // Performs a right mouse click
Dependency
AttributeThe [Dependency]
attribute tells EyeAuras to automatically initialize a property when the script starts. Unlike init
, such a property can be changed after the script starts.
Example:
[Dependency] ISendInputScriptingApi SendInput { get; set; }
SendInput.MouseMoveTo(200, 100); // Moves the mouse to X200 Y100
SendInput.MouseRightClick(); // Performs a right mouse click
When to use Dependency
?
GetService
Method (Most Powerful and Flexible Option)The GetService
method is the most flexible way to access services in your script. It allows you to "on the fly" request the necessary dependency and use it immediately.
Example for mouse control:
var sendInput = GetService<ISendInputScriptingApi>();
sendInput.MouseMoveTo(200, 100); // Moves the mouse to X200 Y100
sendInput.MouseRightClick(); // Performs a right mouse click
Example for playing sounds:
using PoeShared.Audio.Services; // Don't forget to include the namespace
var sound = GetService<IPlaySoundScriptingApi>();
sound.PlaySound(AudioNotificationType.Minions); // Plays a sound
When to use GetService
?
init
properties for the easiest way to get access to services.[Dependency]
if you want the ability to change services while the script is running.GetService
in situations where a service is needed temporarily or may change dynamically.