Skip to content

Commit

Permalink
Remove the factory and 'implement' in the container
Browse files Browse the repository at this point in the history
  • Loading branch information
killij committed Oct 31, 2023
1 parent 645f154 commit a4bfef4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Childrens_Social_Care_CPD_Tests.Controllers;

public class ResourcesControllerTests
{
private IResourcesSearchStrategyFactory _searchStrategyFactory;
private IResourcesSearchStrategy _searchStrategy;
private ResourcesController _resourcesController;
private IRequestCookieCollection _cookies;
private HttpContext _httpContext;
Expand All @@ -31,9 +31,9 @@ public void SetUp()
_httpRequest.Cookies.Returns(_cookies);
_httpContext.Request.Returns(_httpRequest);
controllerContext.HttpContext = _httpContext;
_searchStrategyFactory = Substitute.For<IResourcesSearchStrategyFactory>();
_resourcesController = new ResourcesController(_searchStrategyFactory)

_searchStrategy = Substitute.For<IResourcesSearchStrategy>();
_resourcesController = new ResourcesController(_searchStrategy)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
Expand All @@ -44,10 +44,8 @@ public void SetUp()
public async Task Search_Returns_Strategy_Model()
{
// arrange
var strategy = Substitute.For<IResourcesSearchStrategy>();
_searchStrategyFactory.Create().Returns(strategy);
var model = new ResourcesListViewModel(null, null, null, null);
strategy.SearchAsync(Arg.Any<ResourcesQuery>(), Arg.Any<CancellationToken>()).Returns(model);
_searchStrategy.SearchAsync(Arg.Any<ResourcesQuery>(), Arg.Any<CancellationToken>()).Returns(model);

// act
var actual = await _resourcesController.Search(query: null) as ViewResult;
Expand Down

This file was deleted.

12 changes: 6 additions & 6 deletions Childrens-Social-Care-CPD/Controllers/ResourcesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public ResourcesQuery()

public class ResourcesController : Controller
{
private readonly IResourcesSearchStrategyFactory _factory;
private readonly IResourcesSearchStrategy _strategy;

public ResourcesController(IResourcesSearchStrategyFactory factory)
public ResourcesController(IResourcesSearchStrategy strategy)
{
_factory = factory;
ArgumentNullException.ThrowIfNull(strategy);

_strategy = strategy;
}

[Route("resources", Name = "Resource")]
[HttpGet]
public async Task<IActionResult> Search([FromQuery] ResourcesQuery query, bool preferencesSet = false, CancellationToken cancellationToken = default)
{
var strategy = _factory.Create();

var contextModel = new ContextModel(string.Empty, "Resources", "Resources", "Resources", true, preferencesSet);
ViewData["ContextModel"] = contextModel;

var viewModel = await strategy.SearchAsync(query, cancellationToken);
var viewModel = await _strategy.SearchAsync(query, cancellationToken);
return View(viewModel);
}
}

This file was deleted.

This file was deleted.

15 changes: 13 additions & 2 deletions Childrens-Social-Care-CPD/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.SystemTextJson;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.ApplicationInsights;
using Microsoft.Extensions.Logging.AzureAppServices;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -29,8 +30,18 @@ public static void AddDependencies(this WebApplicationBuilder builder)
builder.Services.AddSingleton<ICookieHelper, CookieHelper>();
builder.Services.AddTransient<IFeaturesConfig, FeaturesConfig>();
builder.Services.AddTransient<IFeaturesConfigUpdater, FeaturesConfigUpdater>();
builder.Services.AddTransient<IResourcesRepository, ResourcesRepository>();
builder.Services.AddScoped<IResourcesSearchStrategyFactory, ResourcesSearchStrategyFactory>();
builder.Services.AddTransient<IResourcesRepository, ResourcesRepository>();

// Resources search feature
builder.Services.AddScoped<ResourcesDynamicTagsSearchStategy>();
builder.Services.AddScoped<ResourcesFixedTagsSearchStrategy>();
builder.Services.AddScoped<IResourcesSearchStrategy>(services =>
{
var featuresConfig = services.GetService<IFeaturesConfig>();
return featuresConfig.IsEnabled(Features.ResourcesUseDynamicTags)
? services.GetService<ResourcesDynamicTagsSearchStategy>()
: services.GetService<ResourcesFixedTagsSearchStrategy>();
});

builder.Services.AddScoped<IGraphQLWebSocketClient>(services => {
var config = services.GetService<IApplicationConfiguration>();
Expand Down

0 comments on commit a4bfef4

Please sign in to comment.