Category: Configuring the options

Revisiting the Factory pattern – Dependency Injection

A factory creates other objects; it is like a literal real-world factory. We explored in the previous chapter how to leverage the Abstract Factory pattern to create families of objects. A factory can be as simple as an interface with one or more Create[Object] methods or, even more, a simple delegate. We explore a DI-oriented […]

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 […]

Using the implementations – Dependency Injection

We can now use any of the two implementations without impacting the rest of the program. That demonstrates the strength of DI when it comes to dependency management. Moreover, we control the lifetime of the dependencies from the composition root.If we were to use the IApplicationState interface in another class, say SomeConsumer, its usage could […]

First implementation – Dependency Injection

The first implementation uses the memory cache system, and I thought it would be educational to show that to you. Caching data in memory is something you might need to do sooner rather than later. However, we are hiding the cache system behind our implementation, which is also educational.Here is the ApplicationMemoryCache class: public class […]

Revisiting the Singleton pattern – Dependency Injection

The Singleton pattern is obsolete, goes against the SOLID principles, and we replace it with a lifetime, as we’ve already seen. This section explores that lifetime and recreates the good old application state, which is nothing more than a singleton-scoped dictionary.We explore two examples: one about the application state, in case you were wondering where […]

Understanding guard clauses – Dependency Injection

A guard clause represents a condition the code must meet before executing a method. Essentially, it’s a type of code that “guards” against continuing the execution of the method if certain conditions aren’t met.In most cases, guard clauses are implemented at the beginning of a method to throw an exception early when the conditions necessary […]

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 […]



         


          Terms of Use | Accessibility Privacy