Skip to content

Commit

Permalink
So/remove options from usecase (#55)
Browse files Browse the repository at this point in the history
* Initial commit

* Removed unnecessary configuration dependency from CompositionRootServiceProvider

* Extended composition root tests slightly.

* Removed unused param comment

---------

Co-authored-by: Spencer O'HEGARTY <Spencer.OHEGARTY@EDUCATION.GOV.UK>
  • Loading branch information
spanersoraferty and spencerohegartyDfE authored Oct 22, 2024
1 parent 76baa95 commit 12c7d53
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
5 changes: 1 addition & 4 deletions Dfe.Data.SearchPrototype/Infrastructure/CompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ public static class CompositionRoot
/// <param name="services">
/// The originating application services onto which to register the search dependencies.
/// </param>
/// <param name="configuration">
/// The originating configuration block from which to derive search service settings.
/// </param>
/// <exception cref="ArgumentNullException">
/// The exception thrown if no valid <see cref="IServiceCollection"/> is provisioned.
/// </exception>
public static void AddCognitiveSearchAdaptorServices(this IServiceCollection services, IConfiguration configuration)
public static void AddCognitiveSearchAdaptorServices(this IServiceCollection services)
{
if (services is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public IServiceProvider SetUpServiceProvider(IConfiguration config)
services.AddSingleton<IConfiguration>(config);

// this is the extension method to add all the dependencies
services.AddCognitiveSearchAdaptorServices(config);
services.AddCognitiveSearchAdaptorServices();

// Replace Common.Infrastructure services with mocks
services.RemoveAll<ISearchByKeywordService>();
Expand All @@ -25,10 +25,10 @@ public IServiceProvider SetUpServiceProvider(IConfiguration config)
.WithSearchResults()
.Create())
.Create();
services.AddScoped<ISearchByKeywordService>(provider => mockSearchService);
services.AddScoped(provider => mockSearchService);
services.RemoveAll<ISearchFilterExpressionsBuilder>();
var mockFilterExpressionBuilder = new FilterExpressionBuilderTestDouble().Create();
services.AddScoped<ISearchFilterExpressionsBuilder>(provider => mockFilterExpressionBuilder);
services.AddScoped(provider => mockFilterExpressionBuilder);

return services.BuildServiceProvider();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Dfe.Data.SearchPrototype.Common.CleanArchitecture.Application.UseCase;
using Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.ServiceAdapters;
using Dfe.Data.SearchPrototype.SearchForEstablishments.Models;
using Microsoft.Extensions.Options;

namespace Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;

Expand All @@ -14,7 +13,7 @@ namespace Dfe.Data.SearchPrototype.SearchForEstablishments.ByKeyword.Usecase;
public sealed class SearchByKeywordUseCase : IUseCase<SearchByKeywordRequest, SearchByKeywordResponse>
{
private readonly ISearchServiceAdapter _searchServiceAdapter;
private readonly SearchByKeywordCriteria _searchByKeywordCriteriaOptions;
private readonly SearchByKeywordCriteria _searchByKeywordCriteria;

/// <summary>
/// The following dependencies include the core cognitive search service definition,
Expand All @@ -24,7 +23,7 @@ public sealed class SearchByKeywordUseCase : IUseCase<SearchByKeywordRequest, Se
/// The concrete implementation of the T:Dfe.Data.SearchPrototype.Search.ISearchServiceAdapter
/// defined within, and injected by the IOC container.
/// </param>
/// <param name="searchByKeywordCriteriaOptions">
/// <param name="searchByKeywordCriteria">
/// The <see cref="SearchByKeywordCriteria"/> define the search fields and facets on
/// which to conduct the underlying search. This is defined in configuration using
/// the options pattern as follows (note: fields and facets used are for explanatory use only),
Expand All @@ -44,10 +43,10 @@ public sealed class SearchByKeywordUseCase : IUseCase<SearchByKeywordRequest, Se
/// </param>
public SearchByKeywordUseCase(
ISearchServiceAdapter searchServiceAdapter,
IOptions<SearchByKeywordCriteria> searchByKeywordCriteriaOptions)
SearchByKeywordCriteria searchByKeywordCriteria)
{
ArgumentNullException.ThrowIfNull(searchByKeywordCriteriaOptions);
_searchByKeywordCriteriaOptions = searchByKeywordCriteriaOptions.Value;
ArgumentNullException.ThrowIfNull(searchByKeywordCriteria);
_searchByKeywordCriteria = searchByKeywordCriteria;
_searchServiceAdapter = searchServiceAdapter;
}

Expand Down Expand Up @@ -77,8 +76,8 @@ public async Task<SearchByKeywordResponse> HandleRequest(SearchByKeywordRequest
await _searchServiceAdapter.SearchAsync(
new SearchServiceAdapterRequest(
request.SearchKeyword,
_searchByKeywordCriteriaOptions.SearchFields,
_searchByKeywordCriteriaOptions.Facets,
_searchByKeywordCriteria.SearchFields,
_searchByKeywordCriteria.Facets,
request.FilterRequests));

return results switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dfe.Data.SearchPrototype.SearchForEstablishments.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Dfe.Data.SearchPrototype.SearchForEstablishments;

Expand Down Expand Up @@ -32,12 +33,17 @@ public static void AddSearchForEstablishmentServices(this IServiceCollection ser
throw new ArgumentNullException(nameof(services),
"A service collection is required to configure the search by keyword use-case.");
}

services.AddOptions<SearchByKeywordCriteria>()
.Configure<IConfiguration>(
(settings, configuration) =>
configuration
.GetSection(nameof(SearchByKeywordCriteria))
.Bind(settings));

services.AddSingleton(serviceProvider =>
serviceProvider.GetRequiredService<IOptions<SearchByKeywordCriteria>>().Value);

services.AddScoped<IUseCase<SearchByKeywordRequest, SearchByKeywordResponse>, SearchByKeywordUseCase>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public async Task AddSearchForEstablishmentServices_CanCreateUsecase()
var response = await usecase.HandleRequest(new SearchByKeywordRequest("searchkeyword"));

response.Should().NotBeNull();
response.Status.Should().Be(SearchResponseStatus.Success);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Dfe.Data.SearchPrototype.Tests.SearchForEstablishments.ByKeyword;
public sealed class SearchByKeywordUseCaseTests
{
private readonly SearchByKeywordUseCase _useCase;
private ISearchServiceAdapter _searchServiceAdapter;
private SearchResults _searchResults;
private SearchByKeywordCriteria _options = SearchByKeywordCriteriaTestDouble.Create();
private readonly ISearchServiceAdapter _searchServiceAdapter;
private readonly SearchResults _searchResults;
private readonly SearchByKeywordCriteria _searchByKeywordCriteriaStub = SearchByKeywordCriteriaTestDouble.Create();

public SearchByKeywordUseCaseTests()
{
Expand All @@ -23,7 +23,7 @@ public SearchByKeywordUseCaseTests()
_searchServiceAdapter =
SearchServiceAdapterTestDouble.MockFor(_searchResults);

_useCase = new(_searchServiceAdapter, IOptionsTestDouble.IOptionsMockFor(_options));
_useCase = new(_searchServiceAdapter, _searchByKeywordCriteriaStub);
}

[Fact]
Expand All @@ -45,8 +45,8 @@ public async Task HandleRequest_ValidRequest_CallsAdapterWithMappedRequestParams

// assert
adapterRequest!.SearchKeyword.Should().Be(request.SearchKeyword);
adapterRequest!.SearchFields.Should().BeEquivalentTo(_options.SearchFields);
adapterRequest!.Facets.Should().BeEquivalentTo(_options.Facets);
adapterRequest!.SearchFields.Should().BeEquivalentTo(_searchByKeywordCriteriaStub.SearchFields);
adapterRequest!.Facets.Should().BeEquivalentTo(_searchByKeywordCriteriaStub.Facets);
adapterRequest!.SearchFilterRequests.Should().BeEquivalentTo(request.FilterRequests);
}

Expand Down

0 comments on commit 12c7d53

Please sign in to comment.