Skip to content

Ershad95/ServiceCollector

Repository files navigation

ServiceCollector

ServiceCollector

Static Badge Static Badge Static Badge Static Badge Static Badge Static Badge

ServiceCollector is a .NET library that provides utilities for service discovery and validation within your .NET applications.

build test

NuGet Downloads NuGet Version GitHub commit activity Libraries.io dependency status for GitHub repo GitHub repo size

Installation

You can install the ServiceCollector package via NuGet Package Manager Console or through the NuGet Package Manager UI in Visual Studio.

NuGet Package Manager Console

Install-Package ServiceCollector.Core

NuGet Package Manager UI

Search for "ServiceCollector.Core" in the NuGet Package Manager UI, then click "Install."

Usage

1. Add Services

create one class that implemented IServiceDiscovery(class name is not important) :

   public class ServiceManager : IServiceDiscovery
    {
        public void AddServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddScoped();
            serviceCollection.AddTransient();
            serviceCollection.AddSingleton();
              
            // add other services
        }
    }

you can create many classes that implemented IServiceDiscovery all of them detected and apply services in program.

2. Service Discovery

Service discovery allows you to automatically discover and add services to the service collection from specified assemblies.

using ServiceCollector.Core;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddServiceDiscovery();

var app = builder.Build();
app.Run();

3. Service Validation (optional)

Service validation allows you to validate services based on certain conditions, such as naming conventions.

install package :

Install-Package ServiceCollector.Validation

this validator check that all of the services register in DI,if one service meet the condition but not register validator detect it and throw exception
using ServiceCollector.Core;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddServiceDiscovery()
             .ValidationSetting()
             .WithStartName("Prefix")
             .WithEndName("Suffix")
             .Validate();

var app = builder.Build();
app.Run();

4. Mock Services (Test)

if you want write any test(UnitTest,IntegrationTest) you need some test-doubles
install package :

Install-Package ServiceCollector.Mock

you can easily mock all services and use it in your tests:
  public void X_X_X()
  {
    // Arrange
// you can set any classes that implemented IServiceDiscovery
var serviceType = typeof(ServiceManager); 

// create a mock for each service that defined in ServiceManager
var mockResult = Mock.MockCollection(serviceType);

// Act

// Assert

}

5. Fake Services (Production code)

Debugging without relying on external services and facilitating rapid service delivery to front-end developers without the need for implementation.

install packages :

Install-Package ServiceCollector.Fake

Nuget Packages :

ServiceCollector.Abstractions
ServiceCollector.Core
ServiceCollector.Validation
ServiceCollector.Mock
ServiceCollector.Fake
ServiceCollector.Fake.Configuration

License

This project is licensed under the MIT License