Skip to content

Commit

Permalink
Use new query abstractions for retrieving entity metadata (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Oct 26, 2023
1 parent 9fdebe9 commit 6bbe68e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<IOrganizationServiceAsync>(serviceClient)
.BuildServiceProvider();

var crmQueryDispatcher = services.GetRequiredService<ICrmQueryDispatcher>();

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1428,23 +1427,6 @@ public async Task<Incident[]> GetIncidentsByContactId(Guid contactId, IncidentSt
return result.Entities.Select(e => e.ToEntity<Incident>()).ToArray();
}

public Task<EntityMetadata> GetEntityMetadata(string entityLogicalName, EntityFilters entityFilters = EntityFilters.Default) =>
GetEntityMetadata(_service, entityLogicalName, entityFilters);

public static async Task<EntityMetadata> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#nullable disable
using Microsoft.Xrm.Sdk.Metadata;

namespace TeachingRecordSystem.Core.Dqt;

Expand Down Expand Up @@ -89,8 +88,6 @@ Task<SetIttResultForTeacherResult> SetIttResultForTeacher(

Task<Incident[]> GetIncidentsByContactId(Guid contactId, IncidentState? state, string[] columnNames);

Task<EntityMetadata> GetEntityMetadata(string entityLogicalName, EntityFilters entityFilters = EntityFilters.Default);

Task<Contact[]> GetTeachersByInitialTeacherTrainingSlugId(string slugId, string[] columnNames, RequestBuilder requestBuilder, bool activeOnly = true);

IAsyncEnumerable<QtsAwardee[]> GetQtsAwardeesForDateRange(DateTime startDate, DateTime endDate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Microsoft.Xrm.Sdk.Metadata;

namespace TeachingRecordSystem.Core.Dqt.Queries;

public record GetEntityMetadataQuery(string EntityLogicalName, EntityFilters EntityFilters = EntityFilters.Default) : ICrmQuery<EntityMetadata>;
Original file line number Diff line number Diff line change
@@ -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<GetEntityMetadataQuery, EntityMetadata>
{
public async Task<EntityMetadata> Execute(GetEntityMetadataQuery query, IOrganizationServiceAsync organizationService)
{
var entityResponse = (RetrieveEntityResponse)await organizationService.ExecuteAsync(new RetrieveEntityRequest()
{
LogicalName = query.EntityLogicalName,
EntityFilters = query.EntityFilters
});

return entityResponse.EntityMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DqtReportingService> _logger;
Expand All @@ -35,14 +36,14 @@ public partial class DqtReportingService : BackgroundService
public DqtReportingService(
IOptions<DqtReportingOptions> optionsAccessor,
ICrmEntityChangesService crmEntityChangesService,
IDataverseAdapter dataverseAdapter,
ICrmQueryDispatcher crmQueryDispatcher,
IClock clock,
TelemetryClient telemetryClient,
ILogger<DqtReportingService> logger)
{
_options = optionsAccessor.Value;
_crmEntityChangesService = crmEntityChangesService;
_dataverseAdapter = dataverseAdapter;
_crmQueryDispatcher = crmQueryDispatcher;
_clock = clock;
_telemetryClient = telemetryClient;
_logger = logger;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }

Expand Down Expand Up @@ -59,18 +53,14 @@ public async Task WithService(Func<DqtReportingService, IObserver<IChangedItem[]
using var crmEntityChangesService = new TestableCrmEntityChangesService();
var changesObserver = crmEntityChangesService.GetChangedItemsObserver(Contact.EntityLogicalName);

var trnGenerationApiClient = Mock.Of<ITrnGenerationApiClient>();

var dataverseAdapter = new DataverseAdapter(_serviceClient, Clock, _memoryCache, trnGenerationApiClient);

var telemetryClient = new TelemetryClient(new Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration());

var logger = new NullLogger<DqtReportingService>();

var service = new DqtReportingService(
options,
crmEntityChangesService,
dataverseAdapter,
_crmClientFixture.CreateQueryDispatcher(),
Clock,
telemetryClient,
logger);
Expand Down

0 comments on commit 6bbe68e

Please sign in to comment.