Category: Revisiting the Factory pattern

Learning the building blocks – Options, Settings, and Configuration

There are four main interfaces to use settings: IOptionsMonitor<TOptions>, IOptionsFactory<TOptions>, IOptionsSnapshot<TOptions>, and IOptions<TOptions>. We must inject that dependency into a class to use the available settings. TOptions is the type that represents the settings that we want to access.The framework returns an empty instance of your options class if you don’t configure it. We learn […]

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

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

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

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