How to utilize TinyIoC in ASP.NET Core

Uncategorized

When working with reliance injection and inversion of control in ASP.NET Core applications, you might typically need a IoC container that is basic or light-weight to prevent unnecessary complexity or unneeded overhead. Most IoC containers consume a great deal of resources– not ideal when you don’t need a full-fledged IoC container in your application.In this article we’ll

go over the principles of inversion of control and reliance injection and introduce TinyIoC, an IoC container that is both simple and light-weight. Then we’ll shows how we can work with TinyIoC in ASP.NET Core applications.To use the code examples offered in this short article, you need to have Visual Studio 2022 set up in your system. If you do not already have a copy, you can download Visual Studio 2022 here. Inversion of control and reliance injection The inversion of control(IoC)and dependency injection(DI) architectural patterns

help you boost the modularity, testability, and maintainability of your application. IoC is a style pattern that allows you to decouple elements, replacement reliance executions, and help with testing. DI is a subset of the IoC principle and functions as one technique of executing IoC. In addition to DI, there are numerous alternative techniques to implement IoC such as events, delegates, template patterns, factory techniques, and service locators.In the sections that follow, we’ll examine how we can work with TinyIoC in ASP.NET Core. Before we begin, let’s develop a new ASP.NET Core Web API job in Visual Studio. We’ll use this job in the subsequent sections of this article.Create an ASP.NET Core Web API job in Visual Studio 2022 To develop an ASP.NET Core Web API project in Visual Studio 2022, follow the steps detailed below. Release the Visual Studio 2022 IDE. Click “Produce new project.”In the”Produce new project “window, select”ASP.NET Core Web API”from the list of design templates displayed. Click Next. In the”Configure your new project

“window, define the name and area for the brand-new task. Optionally check the “Location solution and project in the exact same directory”check box, depending on your choices. Click Next. In the “Additional Info”

  • window revealed next, leave the”
  • Use controllers (uncheck to use minimal APIs)” box inspected. We won’t be using very little APIs in this project.
  • Somewhere else in
  • the “Additional Details” window, leave the “Authentication Type”set to “None”(the default
  • )and ensure the check boxes”Enable Open API Support,”” Set up for HTTPS, “and”Enable Docker “stay unattended.
  • We will not be utilizing any
  • of those features here. Click Produce. We’ll use this ASP.NET Core Web API project to work TinyIoC in the areas below.Install the TinyIoC NuGet package Next add the TinyIoC NuGet bundle to the Web API
  • project you simply produced in Visual Studio. Select the project in the Solution Explorer window and right-click and choose “Handle NuGet Packages.” In the NuGet Bundle Manager window, search for the TinyIoC plan and install it. Additionally, you can install the TinyIoC package via the NuGet Package

    Manager console by getting in the line shown below.PM > Install-Package TinyIoC Due to the fact that we’re working with.NET Core, we need to use the TinyIoC Release Prospect NuGet plan. You should check the “Include prerelease” check box in the”Handle NuGet Packages … “window and set up the current TinyIoC RC NuGet package version as displayed in Figure 1 listed below.< img alt ="tinyioc 01"width="1200"height ="801" src ="https://images.idgesg.net/images/article/2023/10/tinyioc-01-100947065-large.jpg?auto=webp&quality=85,70"/ > IDG Figure 1: Setting Up TinyIoC RC for ASP.NET Core.

    Create a minimalistic class in ASP.NET Core In this example, we’ll develop a minimalistic class which we’ll utilize as a reliance in the API controller we’ll produce later on. To do this, develop a new class called MyService and change the generated code with the following code listing.using System.Diagnostics; namespace TinyIoC_Demo public class MyService: IMyService public string MyMethod()images.idgesg.net< The IMyService user interface includes the statement of the approach called MyMethod. namespace TinyIoC_Demo Configure a TinyIoC container in ASP.NET Core To sign up services using the TinyIoC container in ASP.NET Core, you must write the following piece of code in the Program.s file of the task we developed earlier.TinyIoCContainer.Current.Register(). AsSingleton (); If you have several dependences to register with the container, you can compose them inside an approach and

    then call it here once to ensure that the code is clean.internal static class Bootstrap p> internal fixed space RegisterDependencies ()TinyIoCContainer.Current.Register ( ). AsSingleton(); You

    can now conjure up the RegisterDependencies method of the Bootstrap class to register the services with the built-in container of the ASP.NET Core runtime as shown below.Bootstrap.RegisterDependencies(

    ); You can optionally supply a name when signing up the service with the container as shown in the code bit offered listed below. var myServiceInstance =new MyService( ); TinyIoCContainer.Current.Register (myServiceInstance,”MyService “); Complete source code of Program.cs The total source code of the Program.cs file is offered listed below for your reference.using TinyIoC; using TinyIoC_Demo; var home builder=WebApplication.CreateBuilder(args);// Include services to the container. Bootstrap.RegisterDependencies (); builder.Services.AddControllers(); var app = builder.Build();// Configure the HTTP request pipeline. app.UseAuthorization(); app.MapControllers (); app.Run( ); internal static class Bootstrap Fix dependences in ASP.NET Core As soon as dependences are registered and added to the container, you ought to be able to recover the dependences in your application and use them to invoke techniques. To

  • resolve the dependencies, you can use the Resolve method of the TinyIoCContainer class as shown

    in the code snippet provided below.var _ myService = TinyIoCContainer.Current.Resolve(); Note how the type has been passed to recover the suitable type of object from the container. You can now utilize this circumstances(called _ myService )to conjure up methods referring to the IMyService interface.Create an API controller in ASP.NET Core Produce a brand-new API controller and replace the generated source code with the following code listing.using Microsoft.AspNetCore.Mvc; using TinyIoC; namespace TinyIoC_Demo. Controllers [controller])] [. ApiController] public class TinyIOCDemoController: ControllerBase p> When you set a breakpoint in the HttpGet action technique and run the application, you’ll see that the circumstances of type IMyService has actually been fixed correctly in the controller and is readily available in the action method. IDG Figure 2: The TinyIoC container at work! Conclusion For the sake of simpleness, we have actually developed a minimalistic implementation of the TinyIoC container in ASP.NET Core. You can check out the system tests provided here for more information about how you can take advantage of the TinyIoC container. You can likewise build your own custom-made IoC container with TinyIoC. I ‘ll explore this in a future post here. Copyright © 2023 IDG Communications, Inc. Source

    Leave a Reply

    Your email address will not be published. Required fields are marked *