The Sharp Factory
Esc
↑ ↓ to moveEnter to open
Getting Started / Dependency Injection

Dependency Injection

Register the repository container with the .NET dependency injection container.

.NET 10#

Setting up Dependency Injection is easy.

The RepositoryContainer class is the only class that needs to be injected and it implements the IRepositoryContainer interface.

The code below illustrates how to set up DI in the Program.cs file of a .NET 10 application. Console and worker apps do the same with Host.CreateApplicationBuilder.

C#
// MyApp is the root namespace chosen when the model was created: use yoursusing MyApp.Repository.Common;using MyApp.Repository.Container;using MyApp.Repository.Container.Interfaces;var builder = WebApplication.CreateBuilder(args);// setup dependency injection as a singletonbuilder.Services.AddSingleton<IRepositoryContainer, RepositoryContainer>();// the rest of your code goes here..........var app = builder.Build();app.Run();

Something wrong or unclear on this page? Tell us on GitHub