Skip to content

Registering the services

Jon P Smith edited this page Jan 12, 2021 · 2 revisions

To use the Soft Delete services you need an instance of the specific service you want. Each type of the Soft Delete services have their own service.

  • SingleSoftDeleteService<TInterface> and SingleSoftDeleteServiceAsync<TInterface>
  • CascadeSoftDelService<TInterface> and CascadeSoftDelServiceAsync<TInterface>

Note that the services are generic and require the interface you are using, and a Soft Delete configuration (which has the same interface).

There are two ways to obtain an instance of these services

  1. Using DI (Dependency Injection), e.g. with ASP.NET Core
  2. Manually creating an instance of the service - useful in some applications and in unit testing.

1. Using DI (Dependency Injection)

Typically you will use this library in a application that uses DI, so the library contains a extension method that will do this. The code below shows an example of calling this method inside ASP.Net Core’s startup method.

public void ConfigureServices(IServiceCollection services)
{
    //other setup code left out
    var softLogs = services.RegisterSoftDelServicesAndYourConfigurations(
             Assembly.GetAssembly(typeof(ConfigSoftDeleted))
        );
}

This will scan the assembly which has the ConfigSoftDeleted class in and register all the configuration classes it finds there, and also registers the correct versions of the single or cascade services. In this example you would have three services configured

  • ConfigSoftDeleted as SingleSoftDeleteConfiguration<ISingleSoftDelete>
  • SingleSoftDeleteService<ISingleSoftDelete>
  • SingleSoftDeleteServiceAsync<ISingleSoftDelete>

A few features here:

  • You can provide multiple assemblies to scan.
  • If you don’t provide any assemblies, then it scans the assembly that called it
  • The method outputs a series of logs (see var softLogs in the code) when it finds/registers services. This can be useful for debugging if your soft delete methods don’t work. The listing below shows the output for my use of this library in my BookApp.UI ASP.NET Core project, where the ConfigSoftDelete class was in the ASP.NET Core project (hence the first line, that says no assemblies were provided)
No assemblies provided so only scanning the calling assembly 'BookApp.UI'
Starting scanning assembly BookApp.UI for your soft delete configurations.
Registered your configuration class ConfigSoftDelete as SingleSoftDeleteConfiguration<ISoftDelete>
SoftDeleteServices registered as SingleSoftDeleteService<ISoftDelete>
SoftDeleteServicesAsync registered as SingleSoftDeleteServiceAsync<ISoftDelete>

2. Manually creating an instance of the service

You can easily create a version of the service. Here is a unit test from the Tst project in this repo. You can just the same in an application.

[Fact]
public void TestSoftDeleteServiceSetSoftDeleteOk()
{
    //SETUP
    var options = SqliteInMemory.CreateOptions<SingleSoftDelDbContext>();
    using var context = new SingleSoftDelDbContext(options);
    context.Database.EnsureCreated();
    var book = context.AddBookWithReviewToDb();

    var config = new ConfigSoftDeleteWithUserId(context);
    var service = new SingleSoftDeleteService<ISingleSoftDelete>(config);

    //ATTEMPT
    var status = service.SetSoftDelete(book);

    //Rest of the unit test left out
}

Clone this wiki locally