๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป & ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป ๐—ถ๐—ป ๐—”๐—ฆ๐—ฃ.๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ โ€“ ๐—” ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ & ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฎ๐—น ๐—š๐˜‚๐—ถ๐—ฑ๐—ฒ

๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ๐—ถ๐—ป๐—ด ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป & ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป ๐—ถ๐—ป ๐—”๐—ฆ๐—ฃ.๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ โ€“ ๐—” ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ & ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฎ๐—น ๐—š๐˜‚๐—ถ๐—ฑ๐—ฒ

Publish Date: May 14
0 0

Are you building scalable, testable applications in ASP.NET Core?
Then you must embrace the ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป ๐—ฃ๐—ฟ๐—ถ๐—ป๐—ฐ๐—ถ๐—ฝ๐—น๐—ฒ (๐——๐—œ๐—ฃ) and implement it using ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป (๐——๐—œ).

Hereโ€™s a simple, clear example to help you understand how both work together in a real ASP.NET Core project. ๐Ÿš€

๐Ÿ”น Step 1: Define an Interface and Implementation (DIP)

public interface IMessageService
{
string GetMessage();
}

public class HelloMessageService : IMessageService
{
public string GetMessage()
{
return "Hello from the message service!";
}
}

๐Ÿ’ก Your controller now depends on an abstraction (interface) โ€“ this is ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป ๐—ฃ๐—ฟ๐—ถ๐—ป๐—ฐ๐—ถ๐—ฝ๐—น๐—ฒ in action.

๐Ÿ”ธ Step 2: Inject the Dependency into the Controller (DI)

public class MessageController : ControllerBase
{
private readonly IMessageService _messageService;

public MessageController(IMessageService messageService)
{
    _messageService = messageService;
}

public IActionResult Get()
{
    return Ok(_messageService.GetMessage());
}
Enter fullscreen mode Exit fullscreen mode

}

This is ๐—ฐ๐—ผ๐—ป๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐—ผ๐—ฟ ๐—ถ๐—ป๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป, one of the most common forms of ๐——๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐˜† ๐—œ๐—ป๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป in ASP.NET Core.

๐Ÿ”น Step 3: Register the Service in Program.cs

builder.Services.AddScoped();

Now your service is automatically injected by the .๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ ๐—ฏ๐˜‚๐—ถ๐—น๐˜-๐—ถ๐—ป ๐——๐—œ ๐—ฐ๐—ผ๐—ป๐˜๐—ฎ๐—ถ๐—ป๐—ฒ๐—ฟ.

When /message is called, the controller gets its dependency injected from the container. No tight coupling. Easy to test. Clean architecture. ๐Ÿ’ช

๐Ÿ’ฌ ๐—›๐—ฎ๐˜ƒ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฒ๐—ฑ ๐—บ๐˜‚๐—น๐˜๐—ถ๐—ฝ๐—น๐—ฒ ๐˜€๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฐ๐—ฒ ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐—ผ๐—ฟ ๐˜‚๐˜€๐—ฒ๐—ฑ ๐—บ๐—ผ๐—ฐ๐—ธ ๐˜€๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฐ๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐˜๐—ฒ๐˜€๐˜๐—ถ๐—ป๐—ด ๐˜„๐—ถ๐˜๐—ต ๐——๐—œ ๐—ถ๐—ป ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜? ๐—ช๐—ผ๐˜‚๐—น๐—ฑ ๐—น๐—ผ๐˜ƒ๐—ฒ ๐˜๐—ผ ๐—ต๐—ฒ๐—ฎ๐—ฟ ๐—ต๐—ผ๐˜„ ๐˜†๐—ผ๐˜‚ ๐˜€๐˜๐—ฟ๐˜‚๐—ฐ๐˜๐˜‚๐—ฟ๐—ฒ๐—ฑ ๐—ถ๐˜! ๐Ÿ‘‡

Comments 0 total

    Add comment