Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EHD-1432: Reduce use of interfaces #605

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GenderPayGap.Database
{
public partial class GpgDatabaseContext : DbContext
public partial class GpgDatabaseContext
{

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
11 changes: 10 additions & 1 deletion GenderPayGap.Database/GpgDatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

namespace GenderPayGap.Database
{
public partial class GpgDatabaseContext : IDbContext
public interface IDbContext : IDisposable
{

DbSet<TEntity> Set<TEntity>() where TEntity : class;
void SaveChanges();
DatabaseFacade GetDatabase();

}

public partial class GpgDatabaseContext : DbContext, IDbContext
{

public static string ConnectionString = @"Server=127.0.0.1;Port=5432;Database=GpgDatabase;User Id=gpg_user;Password=local_gpg_database;";
Expand Down
15 changes: 0 additions & 15 deletions GenderPayGap.Database/IDbContext.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using GenderPayGap.Core.Interfaces;
using GenderPayGap.Database;
using GenderPayGap.WebUI.BackgroundJobs;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Cookies;
using GenderPayGap.WebUI.ExternalServices;
using GenderPayGap.WebUI.Repositories;
Expand Down Expand Up @@ -165,7 +164,7 @@ private IContainer BuildContainerIocForControllerOfType()
builder.RegisterType<EmailSendingService>().As<EmailSendingService>().InstancePerLifetimeScope();
builder.Register(g => new MockGovNotify()).As<IGovNotifyAPI>().InstancePerLifetimeScope();

builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope();
builder.RegisterType<UserRepository>().As<UserRepository>().InstancePerLifetimeScope();
builder.RegisterType<RegistrationRepository>().As<RegistrationRepository>().InstancePerLifetimeScope();
builder.RegisterType<AuditLogger>().As<AuditLogger>().InstancePerLifetimeScope();
builder.RegisterType<PinInThePostService>().As<PinInThePostService>().InstancePerLifetimeScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using GenderPayGap.Core;
using GenderPayGap.Core.Interfaces;
using GenderPayGap.Database;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Helpers;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -32,7 +32,7 @@ public void BeforeEach()
}

private Mock<IDataRepository> mockDataRepo;
private IUserRepository testUserRepo;
private GenderPayGap.WebUI.Repositories.UserRepository testUserRepo;

[Test]
public void CorrectPasswordShouldResetLoginAttempts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using GenderPayGap.Extensions;
using GenderPayGap.Tests.Common.Classes;
using GenderPayGap.Tests.Common.TestHelpers;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using GenderPayGap.WebUI.Tests.TestHelpers;
using Moq;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void BeforeEach()
}

private Mock<IDataRepository> mockDataRepo;
private IUserRepository testUserRepo;
private GenderPayGap.WebUI.Repositories.UserRepository testUserRepo;

[TestCase("[email protected]", UserStatuses.Active)]
[TestCase("[email protected]", UserStatuses.New, UserStatuses.New)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using GenderPayGap.Extensions;
using GenderPayGap.Tests.Common.Classes;
using GenderPayGap.Tests.Common.TestHelpers;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using GenderPayGap.WebUI.Tests.TestHelpers;
using Moq;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void BeforeEach()

private Mock<IDataRepository> mockDataRepo;

private IUserRepository testUserRepo;
private GenderPayGap.WebUI.Repositories.UserRepository testUserRepo;

[TestCase]
public void SavesRetiredStatus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using GenderPayGap.Extensions;
using GenderPayGap.Tests.Common.Classes;
using GenderPayGap.Tests.Common.TestHelpers;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using GenderPayGap.WebUI.Tests.TestHelpers;
using Moq;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void BeforeEach()

private Mock<IDataRepository> mockDataRepo;

private IUserRepository testUserRepo;
private GenderPayGap.WebUI.Repositories.UserRepository testUserRepo;

[TestCase("[email protected]", "[email protected]")]
public void SavesExpectedEmailFields(string testCurrentEmail, string testNewEmail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using GenderPayGap.Extensions;
using GenderPayGap.Tests.Common.Classes;
using GenderPayGap.Tests.Common.TestHelpers;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Helpers;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using GenderPayGap.WebUI.Tests.TestHelpers;
using Moq;
Expand Down Expand Up @@ -39,7 +39,7 @@ public void BeforeEach()

private Mock<IDataRepository> mockDataRepo;

private IUserRepository testUserRepo;
private GenderPayGap.WebUI.Repositories.UserRepository testUserRepo;

[TestCase]
public void SavesNewPasswordHash()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void BeforeEach()
private Mock<IDataRepository> mockDataRepository;

// sut
private IScopeBusinessLogic scopeBusinessLogic;
private WebUI.BusinessLogic.Services.ScopeBusinessLogic scopeBusinessLogic;

[TestCase(SectorTypes.Private)]
[TestCase(SectorTypes.Public)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void BeforeEach()
private List<Organisation> testOrgs;

// sut
private IScopeBusinessLogic scopeBusinessLogic;
private WebUI.BusinessLogic.Services.ScopeBusinessLogic scopeBusinessLogic;

[TestCase(1)]
[TestCase(2)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using GenderPayGap.Database;
using GenderPayGap.Database.Models;
using GenderPayGap.Extensions;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.Controllers.Account;
using GenderPayGap.WebUI.ExternalServices;
using GenderPayGap.WebUI.Models.Account;
using GenderPayGap.WebUI.Repositories;
using GenderPayGap.WebUI.Services;
using GenderPayGap.WebUI.Tests.Builders;
using GenderPayGap.WebUI.Tests.TestHelpers;
Expand All @@ -26,7 +26,7 @@ public class ChangePasswordControllerTests
{

private Mock<IDataRepository> mockDataRepo;
private IUserRepository mockUserRepo;
private UserRepository mockUserRepo;

[SetUp]
public void BeforeEach()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ScopeBusinessLogicTests : AssertionHelper

private IList<Organisation> testOrgData;
private IList<OrganisationScope> testOrgScopeData;
public IScopeBusinessLogic testScopeBL;
public ScopeBusinessLogic testScopeBL;

#region Test Data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using GenderPayGap.Tests.Common.Classes;
using GenderPayGap.Tests.Common.TestHelpers;
using GenderPayGap.WebUI.BackgroundJobs;
using GenderPayGap.WebUI.BusinessLogic.Abstractions;
using GenderPayGap.WebUI.BusinessLogic.Services;
using GenderPayGap.WebUI.Classes;
using GenderPayGap.WebUI.Classes.Presentation;
Expand Down Expand Up @@ -191,7 +190,7 @@ public static IContainer BuildContainerIoC(params object[] dbObjects)
//Create the mock repositories
// BL Repository
builder.Register(c => new SystemFileRepository()).As<IFileRepository>().SingleInstance();
builder.RegisterType<UserRepository>().As<IUserRepository>().SingleInstance();
builder.RegisterType<UserRepository>().As<UserRepository>().SingleInstance();
builder.RegisterType<RegistrationRepository>().As<RegistrationRepository>().SingleInstance();
builder.RegisterType<OrganisationService>().As<OrganisationService>().InstancePerLifetimeScope();
builder.RegisterType<ReturnService>().As<ReturnService>().InstancePerLifetimeScope();
Expand All @@ -201,27 +200,21 @@ public static IContainer BuildContainerIoC(params object[] dbObjects)
builder.RegisterInstance(Config.Configuration);
builder.RegisterType<UpdateFromCompaniesHouseService>().As<UpdateFromCompaniesHouseService>().InstancePerLifetimeScope();

builder.Register(
c => c.ResolveAsMock<ScopeBusinessLogic>(
false,
typeof(IDataRepository))
.Object)
.As<IScopeBusinessLogic>()
.InstancePerLifetimeScope();
builder.RegisterType<ScopeBusinessLogic>().As<ScopeBusinessLogic>().SingleInstance();

builder.Register(g => new MockGovNotify()).As<IGovNotifyAPI>().SingleInstance();

builder.RegisterType<PinInThePostService>().As<PinInThePostService>().SingleInstance();

builder.RegisterType<CompareViewService>().As<ICompareViewService>().InstancePerLifetimeScope();
builder.RegisterType<CompareViewService>().As<CompareViewService>().InstancePerLifetimeScope();

builder.RegisterType<AuditLogger>().As<AuditLogger>().SingleInstance();
builder.RegisterType<AutoCompleteSearchService>().As<AutoCompleteSearchService>().InstancePerLifetimeScope();
builder.RegisterType<ViewingSearchService>().As<ViewingSearchService>().InstancePerLifetimeScope();


//Register WebTracker
builder.Register(c => Mock.Of<IWebTracker>()).As<IWebTracker>().InstancePerLifetimeScope();
builder.RegisterType<GoogleAnalyticsTracker>().As<GoogleAnalyticsTracker>().InstancePerLifetimeScope();

//Register all controllers - this is required to ensure KeyFilter is resolved in constructors
builder.RegisterType<ErrorController>().InstancePerLifetimeScope();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"DefaultEncryptionKey": "", // some unit tests require this key to be empty
"OffsetCurrentDateTimeForSite": "917"
"OffsetCurrentDateTimeForSite": "917",
"GoogleAnalyticsAccountId": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace GenderPayGap.WebUI.BackgroundJobs.ScheduledJobs

public class SetPresumedScopesJob
{
private readonly IScopeBusinessLogic scopeBusinessLogic;
private readonly ScopeBusinessLogic scopeBusinessLogic;

public SetPresumedScopesJob(
IScopeBusinessLogic scopeBusinessLogic)
ScopeBusinessLogic scopeBusinessLogic)
{
this.scopeBusinessLogic = scopeBusinessLogic;
}
Expand Down
21 changes: 0 additions & 21 deletions GenderPayGap.WebUI/BusinessLogic/Abstractions/IUserRepository.cs

This file was deleted.

16 changes: 1 addition & 15 deletions GenderPayGap.WebUI/BusinessLogic/Services/ScopeBusinessLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,7 @@
namespace GenderPayGap.WebUI.BusinessLogic.Services
{

public interface IScopeBusinessLogic
{

OrganisationScope GetLatestScopeBySnapshotYear(long organisationId, int snapshotYear = 0);

void SetPresumedScopes();

HashSet<OrganisationMissingScope> FindOrgsWhereScopeNotSet();
bool FillMissingScopes(Organisation org);

void SetScopeStatuses();

}

public class ScopeBusinessLogic : IScopeBusinessLogic
public class ScopeBusinessLogic
{

public ScopeBusinessLogic(IDataRepository dataRepo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class UpdateFromCompaniesHouseService
private const string SourceOfChange = "CoHo";
private const string DetailsOfChange = "Replaced by CoHo";

private readonly ICompaniesHouseAPI companiesHouseApi;
private readonly CompaniesHouseAPI companiesHouseApi;
private readonly IDataRepository dataRepository;

public UpdateFromCompaniesHouseService(IDataRepository dataRepository, ICompaniesHouseAPI companiesHouseApi)
public UpdateFromCompaniesHouseService(IDataRepository dataRepository, CompaniesHouseAPI companiesHouseApi)
{
this.dataRepository = dataRepository;
this.companiesHouseApi = companiesHouseApi;
Expand Down
24 changes: 10 additions & 14 deletions GenderPayGap.WebUI/Classes/GoogleAnalyticsTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -10,32 +11,24 @@

namespace GenderPayGap.WebUI.Classes
{
public interface IWebTracker
{
void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null);
}


/// <summary>
/// Uses open HttpClient see
/// https://www.codeproject.com/Articles/1194406/Using-HttpClient-as-it-was-intended-because-you-re
/// </summary>
public class GoogleAnalyticsTracker : IWebTracker, IDisposable
public class GoogleAnalyticsTracker : IDisposable
{

public static Uri BaseUri = new Uri("https://www.google-analytics.com");
private static readonly Uri endpointUri = new Uri(BaseUri, "/collect");

private readonly HttpClient _httpClient;

private readonly IHttpContextAccessor _httpContextAccessor;
private readonly string googleTrackingId; // UA-XXXXXXXXX-XX

private readonly string googleVersion = "1";

public GoogleAnalyticsTracker(IHttpContextAccessor httpContextAccessor, HttpClient httpClient, string trackingId)
public GoogleAnalyticsTracker(HttpClient httpClient, string trackingId)
{
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
_httpClient = httpClient;
googleTrackingId = trackingId;
}
Expand All @@ -46,8 +39,7 @@ public void Dispose()
{
_httpClient?.Dispose();
}



public void TrackPageView(Controller controller, string pageTitle = null, string pageUrl = null)
{
if (string.IsNullOrWhiteSpace(pageTitle))
Expand All @@ -71,10 +63,14 @@ public void TrackPageView(Controller controller, string pageTitle = null, string

SendPageViewTracking(pageTitle, pageUrl);
}



private async void SendPageViewTracking(string title, string url)
{
if (googleTrackingId == null)
{
return;
}

if (string.IsNullOrWhiteSpace(title))
{
throw new ArgumentNullException(nameof(title));
Expand Down
Loading