Year: 2022

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

Loading the configuration – Options, Settings, and Configuration

Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter covers the .NET Options pattern, a building block of any application. .NET Core introduced new predefined mechanisms […]

Summary – Dependency Injection

This chapter delved into Dependency Injection, understanding its crucial role in crafting adaptable systems. We learned how DI applies the Inversion of Control principle, shifting dependency creation from the objects to the composition root. We explored the IoC container’s role in object management, service resolution and injection, and dependency lifetime management. We tackled the Control […]

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

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



         


          Terms of Use | Accessibility Privacy