Category: Learning the building blocks

Implementing constructor injection – Dependency Injection

At this point, you should be familiar with constructor injection. Nonetheless, next is the controller’s code after migrating the Service Locator pattern to constructor injection: namespace ServiceLocator;public class ConstructorInjectionController : ControllerBase{    private readonly IMyService _myService;    public ConstructorInjectionController(IMyService myService)    {        _myService = myService ??throw new ArgumentNullException(nameof(myService));    }    [Route(“/constructor-injection”)]    public IActionResult GetUsingConstructorInjection()    {        _myService.Execute();        return Ok(“Success!”);    }} […]

Project – ServiceLocator – Dependency Injection

The best way to avoid something is to know about it, so let’s see how to implement the Service Locator pattern using IServiceProvider to find a dependency.The service we want to use is an implementation of IMyService. Let’s start with the interface: namespace ServiceLocator;public interface IMyService : IDisposable{    void Execute();} The interface inherits from the […]

Understanding the Service Locator pattern – Dependency Injection

Conclusion This section explored replacing the classic Singleton pattern with a standard instantiable class registered with a singleton lifetime. We looked at the old application state, learned that it was no more, and implemented two versions of it. We no longer need that, but it was a good way of learning about singletons.We then implemented […]

Project – Wishlist – Dependency Injection-2

That code leverages the new API, but we’ll stick to our simple implementation instead. Let’s look at the outline of the unit tests next because the whole code would take pages and be of low value: namespace Wishlist;public class InMemoryWishListTest{    // Constructor and private fields omitted    public class AddOrRefreshAsync : InMemoryWishListTest    {        [Fact]        public async […]

Project – Wishlist – Dependency Injection-1

Let’s get into another sample to illustrate using the singleton lifetime and DI. Seeing DI in action should help understand it and then leverage it to create SOLID software.Context: The application is a site-wide wishlist where users can add items. Items expire every 30 seconds. When a user adds an existing item, the system must […]

Use_the_InMemoryLocationService – Dependency Injection

Next, we use the in-memory location service to compose the controller like this: var inMemoryLocationService = new InMemoryLocationService();var devController = new InjectAbstractionLocationsController(    inMemoryLocationService); As we can see from the preceding code, we injected a different service into the controller, changing its behavior. This time, after calling the GetAsync method, the controller returned the ten Location […]

Injecting an abstraction in the controller – Dependency Injection

In this last controller, we leverage the SOLID principles, constructor injection, and, inherently, the Strategy pattern to build a controller that we can change from the outside. All we have to do to make the code flexible is inject the interface instead of its implementation, like this: using Microsoft.AspNetCore.Mvc;using Strategy.Services;namespace Strategy.Controllers;[Route(“travel/[controller]”)][ApiController]public class InjectAbstractionLocationsController : ControllerBase{    […]

Control Freak controllers – Dependency Injection

This first version of the code showcases the lack of flexibility that creating dependencies using the new keyword brings when the time to update the application arises. Here’s the initial controller that leverages an in-memory collection: using Microsoft.AspNetCore.Mvc;using Strategy.Services;namespace Strategy.Controllers;[Route(“travel/[controller]”)][ApiController]public class ControlFreakLocationsController : ControllerBase{    [HttpGet]    public async Task<IEnumerable<LocationSummary>> GetAsync(CancellationToken cancellationToken)    {        var locationService = new […]



         


          Terms of Use | Accessibility Privacy