From 6bbe68e1cca9f50915437836cbc30de756b9dc01 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 26 Oct 2023 17:58:37 +0100 Subject: [PATCH] Use new query abstractions for retrieving entity metadata (#876) --- .../Commands.GenerateReportingDbTable.cs | 13 ++++++++++- .../Dqt/DataverseAdapter.cs | 18 --------------- .../Dqt/IDataverseAdapter.cs | 3 --- .../Dqt/Queries/GetEntityMetadataQuery.cs | 5 +++++ .../QueryHandlers/GetEntityMetadataHandler.cs | 20 +++++++++++++++++ .../DqtReporting/DqtReportingService.cs | 10 +++++---- .../DqtReporting/DqtReportingFixture.cs | 22 +++++-------------- 7 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetEntityMetadataQuery.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetEntityMetadataHandler.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Cli/Commands.GenerateReportingDbTable.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Cli/Commands.GenerateReportingDbTable.cs index f891a23fa..8766329bc 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Cli/Commands.GenerateReportingDbTable.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Cli/Commands.GenerateReportingDbTable.cs @@ -1,5 +1,7 @@ +using Microsoft.Extensions.DependencyInjection; using Microsoft.Xrm.Sdk.Metadata; using TeachingRecordSystem.Core.Dqt; +using TeachingRecordSystem.Core.Dqt.Queries; using TeachingRecordSystem.Core.Dqt.Services.DqtReporting; namespace TeachingRecordSystem.Cli; @@ -23,7 +25,16 @@ public static Command CreateGenerateReportingDbTableCommand(IConfiguration confi async (string crmConnectionString, string entityType) => { var serviceClient = new ServiceClient(crmConnectionString); - var entityMetadata = await DataverseAdapter.GetEntityMetadata(serviceClient, entityType, EntityFilters.Default | EntityFilters.Attributes); + + var services = new ServiceCollection() + .AddCrmQueries() + .AddSingleton(serviceClient) + .BuildServiceProvider(); + + var crmQueryDispatcher = services.GetRequiredService(); + + var entityMetadata = await crmQueryDispatcher.ExecuteQuery( + new GetEntityMetadataQuery(entityType, EntityFilters.Default | EntityFilters.Attributes)); var entityTableMapping = EntityTableMapping.Create(entityMetadata); var sql = entityTableMapping.GetCreateTableSql(); await Console.Out.WriteLineAsync(sql); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/DataverseAdapter.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/DataverseAdapter.cs index 97af1042d..141b9e8d2 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/DataverseAdapter.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/DataverseAdapter.cs @@ -5,7 +5,6 @@ using Microsoft.PowerPlatform.Dataverse.Client; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; -using Microsoft.Xrm.Sdk.Metadata; using Microsoft.Xrm.Sdk.Query; using TeachingRecordSystem.Core.Services.TrnGenerationApi; @@ -1428,23 +1427,6 @@ public async Task GetIncidentsByContactId(Guid contactId, IncidentSt return result.Entities.Select(e => e.ToEntity()).ToArray(); } - public Task GetEntityMetadata(string entityLogicalName, EntityFilters entityFilters = EntityFilters.Default) => - GetEntityMetadata(_service, entityLogicalName, entityFilters); - - public static async Task GetEntityMetadata( - IOrganizationServiceAsync organizationService, - string entityLogicalName, - EntityFilters entityFilters = EntityFilters.Default) - { - var entityResponse = (RetrieveEntityResponse)await organizationService.ExecuteAsync(new RetrieveEntityRequest() - { - LogicalName = entityLogicalName, - EntityFilters = entityFilters - }); - - return entityResponse.EntityMetadata; - } - public async Task ClearTeacherIdentityInfo(Guid identityUserId, DateTime updateTimeUtc) { var query = new QueryByAttribute(Contact.EntityLogicalName) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/IDataverseAdapter.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/IDataverseAdapter.cs index 8fa0fb1b5..922f5300e 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/IDataverseAdapter.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/IDataverseAdapter.cs @@ -1,5 +1,4 @@ #nullable disable -using Microsoft.Xrm.Sdk.Metadata; namespace TeachingRecordSystem.Core.Dqt; @@ -89,8 +88,6 @@ Task SetIttResultForTeacher( Task GetIncidentsByContactId(Guid contactId, IncidentState? state, string[] columnNames); - Task GetEntityMetadata(string entityLogicalName, EntityFilters entityFilters = EntityFilters.Default); - Task GetTeachersByInitialTeacherTrainingSlugId(string slugId, string[] columnNames, RequestBuilder requestBuilder, bool activeOnly = true); IAsyncEnumerable GetQtsAwardeesForDateRange(DateTime startDate, DateTime endDate); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetEntityMetadataQuery.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetEntityMetadataQuery.cs new file mode 100644 index 000000000..de1317f7c --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetEntityMetadataQuery.cs @@ -0,0 +1,5 @@ +using Microsoft.Xrm.Sdk.Metadata; + +namespace TeachingRecordSystem.Core.Dqt.Queries; + +public record GetEntityMetadataQuery(string EntityLogicalName, EntityFilters EntityFilters = EntityFilters.Default) : ICrmQuery; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetEntityMetadataHandler.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetEntityMetadataHandler.cs new file mode 100644 index 000000000..ac4a4ac98 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetEntityMetadataHandler.cs @@ -0,0 +1,20 @@ +using Microsoft.PowerPlatform.Dataverse.Client; +using Microsoft.Xrm.Sdk.Messages; +using Microsoft.Xrm.Sdk.Metadata; +using TeachingRecordSystem.Core.Dqt.Queries; + +namespace TeachingRecordSystem.Core.Dqt.QueryHandlers; + +public class GetEntityMetadataHandler : ICrmQueryHandler +{ + public async Task Execute(GetEntityMetadataQuery query, IOrganizationServiceAsync organizationService) + { + var entityResponse = (RetrieveEntityResponse)await organizationService.ExecuteAsync(new RetrieveEntityRequest() + { + LogicalName = query.EntityLogicalName, + EntityFilters = query.EntityFilters + }); + + return entityResponse.EntityMetadata; + } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Services/DqtReporting/DqtReportingService.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Services/DqtReporting/DqtReportingService.cs index bb83cea48..89dfc0994 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Services/DqtReporting/DqtReportingService.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Services/DqtReporting/DqtReportingService.cs @@ -9,6 +9,7 @@ using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Metadata; using Microsoft.Xrm.Sdk.Query; +using TeachingRecordSystem.Core.Dqt.Queries; using TeachingRecordSystem.Core.Dqt.Services.CrmEntityChanges; namespace TeachingRecordSystem.Core.Dqt.Services.DqtReporting; @@ -26,7 +27,7 @@ public partial class DqtReportingService : BackgroundService private readonly DqtReportingOptions _options; private readonly ICrmEntityChangesService _crmEntityChangesService; - private readonly IDataverseAdapter _dataverseAdapter; + private readonly ICrmQueryDispatcher _crmQueryDispatcher; private readonly IClock _clock; private readonly TelemetryClient _telemetryClient; private readonly ILogger _logger; @@ -35,14 +36,14 @@ public partial class DqtReportingService : BackgroundService public DqtReportingService( IOptions optionsAccessor, ICrmEntityChangesService crmEntityChangesService, - IDataverseAdapter dataverseAdapter, + ICrmQueryDispatcher crmQueryDispatcher, IClock clock, TelemetryClient telemetryClient, ILogger logger) { _options = optionsAccessor.Value; _crmEntityChangesService = crmEntityChangesService; - _dataverseAdapter = dataverseAdapter; + _crmQueryDispatcher = crmQueryDispatcher; _clock = clock; _telemetryClient = telemetryClient; _logger = logger; @@ -97,7 +98,8 @@ internal async Task LoadEntityMetadata() try { - var entityMetadata = await _dataverseAdapter.GetEntityMetadata(entity, EntityFilters.Default | EntityFilters.Attributes); + var entityMetadata = await _crmQueryDispatcher.ExecuteQuery( + new GetEntityMetadataQuery(entity, EntityFilters.Default | EntityFilters.Attributes)); if (entityMetadata.ChangeTrackingEnabled != true) { diff --git a/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Dqt.Tests/Services/DqtReporting/DqtReportingFixture.cs b/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Dqt.Tests/Services/DqtReporting/DqtReportingFixture.cs index ddfc4a61d..0b5b9d1bb 100644 --- a/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Dqt.Tests/Services/DqtReporting/DqtReportingFixture.cs +++ b/TeachingRecordSystem/tests/TeachingRecordSystem.Core.Dqt.Tests/Services/DqtReporting/DqtReportingFixture.cs @@ -1,35 +1,29 @@ using System.Collections.Concurrent; using Microsoft.ApplicationInsights; -using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; -using Microsoft.PowerPlatform.Dataverse.Client; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using TeachingRecordSystem.Core.Dqt.Services.CrmEntityChanges; using TeachingRecordSystem.Core.Dqt.Services.DqtReporting; -using TeachingRecordSystem.Core.Services.TrnGenerationApi; namespace TeachingRecordSystem.Core.Dqt.Tests.Services.DqtReporting; public class DqtReportingFixture { - private readonly ServiceClient _serviceClient; - private readonly IMemoryCache _memoryCache; + private readonly CrmClientFixture _crmClientFixture; - public DqtReportingFixture(IConfiguration configuration, ServiceClient serviceClient, IMemoryCache memoryCache) + public DqtReportingFixture(CrmClientFixture crmClientFixture) { - _serviceClient = serviceClient; - _memoryCache = memoryCache; - ReportingDbConnectionString = configuration.GetRequiredValue("DqtReporting:ReportingDbConnectionString"); + _crmClientFixture = crmClientFixture; + ReportingDbConnectionString = crmClientFixture.Configuration.GetRequiredValue("DqtReporting:ReportingDbConnectionString"); var migrator = new Migrator(ReportingDbConnectionString); migrator.DropAllTables(); migrator.MigrateDb(); } - public IClock Clock { get; } = new TestableClock(); + public IClock Clock => _crmClientFixture.Clock; public string ReportingDbConnectionString { get; } @@ -59,10 +53,6 @@ public async Task WithService(Func(); - - var dataverseAdapter = new DataverseAdapter(_serviceClient, Clock, _memoryCache, trnGenerationApiClient); - var telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration()); var logger = new NullLogger(); @@ -70,7 +60,7 @@ public async Task WithService(Func