Announcing AnnotationServiceBuilder: Simplify Dependency Injection in .NET!

Genna Ian - Aug 30 - - Dev Community

Hello, everyone!

I'm thrilled to introduce AnnotationServiceBuilder—a powerful .NET library designed to streamline dependency injection using custom annotations. With this library, you can effortlessly register services with different lifetimes (Singleton, Scoped, Transient) and integrate Refit clients with minimal code.

📦 Key Features:

  • Automatic Service Registration: Eliminate the need for manual setup in your Startup.cs or Program.cs.

  • Support for Multiple Lifetimes: Easily manage service lifetimes using [SingletonService], [ScopedService], and [TransientService] annotations.

  • Effortless Refit Integration: Automatically register Refit clients with just a few lines of code.

🔗 Learn More and Download:


Usage

The AnnotationServiceBuilder library provides a straightforward way to register services with different lifetimes and integrate Refit clients. Below are examples of how to use each annotation in your project.

1. Using Scoped Services

To register a service with a scoped lifetime, use the [ScopedService] annotation. This ensures that a new instance of the service is created for each HTTP request in web applications.

using AnnotationServiceBuilder.Annotations.Scoped;

[ScopedService(typeof(IMyScopedService))]
public class MyScopedService : IMyScopedService
{
    // Implementation...
}
Enter fullscreen mode Exit fullscreen mode

In this example, MyScopedService is registered as a scoped service, meaning it will be instantiated once per HTTP request or per scope in other contexts.

2. Using Singleton Services

To register a service with a singleton lifetime, use the [SingletonService] annotation. A singleton service is created the first time it is requested and then reused for all subsequent requests.

using AnnotationServiceBuilder.Annotations.Singleton;

[SingletonService]
public class MySingletonService
{
    // Implementation...
}
Enter fullscreen mode Exit fullscreen mode

Here, MySingletonService is registered as a singleton, meaning only one instance of this service will exist throughout the application's lifetime.

3. Using Transient Services

To register a service with a transient lifetime, use the [TransientService] annotation. A new instance of the service is created every time it is requested.

using AnnotationServiceBuilder.Annotations.Transient_Services;

[TransientService]
public class MyTransientService
{
    // Implementation...
}
Enter fullscreen mode Exit fullscreen mode

In this example, MyTransientService is registered as a transient service, meaning a new instance will be created each time the service is requested.

4. Example of a Refit Client

The AnnotationServiceBuilder also supports easy integration with Refit, a REST API client library. Use the [RefitClient] annotation to register Refit clients automatically.

using AnnotationServiceBuilder.Annotations.Refit;
using AnnotationServiceBuilder.Data.Models;
using Refit;

namespace AnnotationServiceBuilder.Network.Repositories
{
    [RefitClient]
    public interface IPostsApi
    {
        [Get("/posts")]
        Task<List<Post>> GetPostsAsync();

        [Get("/posts/{id}")]
        Task<Post> GetPostByIdAsync(int id);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, IPostsApi is a Refit client interface that automatically handles HTTP requests and responses for the defined endpoints. The [RefitClient] annotation ensures that the client is registered with the dependency injection container, making it available for injection and use throughout your application.


.
Terabox Video Player