Dependency injection: Difference between revisions

Content deleted Content added
Borkf (talk | contribs)
Removed a misplaced comma. There either needs to be a comma on either side of that clause or no commas. I prefer the latter.
Xilef97 (talk | contribs)
m C++: fix typo: wrong type was used for the member variable, code wouldn't compile
 
(3 intermediate revisions by one other user not shown)
Line 295:
 
The <code>ng-controller</code> directive triggers the injector to create an instance of the controller and its dependencies.
 
=== C++ ===
This sample provides an example of constructor injection in [[C++]].
<syntaxhighlight lang="c++">
import std;
 
class DatabaseConnection {
public:
void connect() {
std::println("Connecting to database...");
}
};
 
class DatabaseService {
private:
DatabaseConnection& dbConn;
public:
explicit DatabaseService(DatabaseConnection& db):
dbConn{db} {}
 
void execute() {
dbConn.connect();
std::println("Executing database service...");
}
};
 
int main(int argc, char* argv[]) {
DatabaseConnection db;
DatabaseService sv(db);
sv.execute();
}
</syntaxhighlight>
 
This sample provides an example of interface injection in C++.
<syntaxhighlight lang="c++">
import std;
 
enum class DatabaseConnectionError {
NoConnection,
// more errors here
};
 
class IConnection {
public:
virtual void connect() = 0;
virtual ~IConnection() = default;
};
 
class DatabaseConnection: public IConnection {
public:
DatabaseConnection() = default;
 
void connect() override {
std::println("Connecting to database...");
}
};
 
class DatabaseService {
private:
std::shared_ptr<IConnection> conn;
public:
DatabaseService() = default;
 
void setConnection(std::shared_ptr<IConnection> nextConn) noexcept {
conn = nextConn;
}
 
std::expected<void, DatabaseConnectionError> execute() {
if (conn) {
conn->connect();
std::println("Executing database service...");
} else {
return std::unexpected(DatabaseConnectionError::NoConnection);
}
}
};
 
int main(int argc, char* argv[]) {
std::shared_ptr<DatabaseConnection> db = std::make_shared<DatabaseConnection>();
DatabaseService sv;
sv.setConnection(db);
sv.execute();
}
</syntaxhighlight>
 
=== C# ===
Line 304 ⟶ 389:
 
// Our client will only know about this interface, not which specific gamepad it is using.
interface IGamepadFunctionalityIGamePadFunctionality {
{
string GetGamepadNameGetGamePadName();
void SetVibrationPower(float power);
}
Line 311 ⟶ 397:
// The following services provide concrete implementations of the above interface.
 
class XboxGamepadXboxGamePad : IGamepadFunctionalityIGamePadFunctionality {
{
float vibrationPower = 1.0f;
public string GetGamepadNameGetGamePadName() => "Xbox controller";
public void SetVibrationPower(float power) => this.vibrationPower = Math.Clamp(power, 0.0f, 1.0f);
}
 
class PlayStationJoystick : IGamePadFunctionality
class PlaystationJoystick : IGamepadFunctionality {
{
float vibratingPower = 100.0f;
public string GetGamepadNameGetGamePadName() => "PlayStation controller";
public void SetVibrationPower(float power) => this.vibratingPower = Math.Clamp(power * 100.0f, 0.0f, 100.0f);
}
 
class SteamController : IGamepadFunctionalityIGamePadFunctionality {
{
double vibrating = 1.0;
public string GetGamepadNameGetGamePadName() => "Steam controller";
public void SetVibrationPower(float power) => this.vibrating = Convert.ToDouble(Math.Clamp(power, 0.0f, 1.0f));
Line 336 ⟶ 425:
 
// This class is the client which receives a service.
class Gamepad {GamePad
{
IGamepadFunctionality gamepadFunctionality;
IGamePadFunctionality gamePadFunctionality;
 
// The service is injected through the constructor and stored in the above field.
public GamepadGamePad(IGamepadFunctionalityIGamePadFunctionality gamepadFunctionalitygamePadFunctionality) => this.gamepadFunctionalitygamePadFunctionality = gamepadFunctionalitygamePadFunctionality;
 
public void Showcase() {
{
// The injected service is used.
varstring gamepadNamegamePadName = this.gamepadFunctionalitygamePadFunctionality.GetGamepadNameGetGamePadName();
varstring message = $"We're using the {gamepadNamegamePadName} right now, do you want to change the vibrating power?";
Console.WriteLine(message);
}
Line 351 ⟶ 442:
 
class Program {
static void Main(string[] args) {
{
varSteamController steamController = new SteamController();
// We could have also passed in an XboxController, PlaystationJoystickPlayStationJoystick, etc.
// The gamepad doesn't know what it's using and doesn't need to.
varGamePad gamepad = new GamepadGamePad(steamController);
gamepad.Showcase();