Skip to content

Commit

Permalink
Added public API XML comments for ChangeEventQueries
Browse files Browse the repository at this point in the history
  • Loading branch information
codemunkie15 committed Dec 19, 2024
1 parent 08ad0bb commit 5636a42
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 15 deletions.
24 changes: 24 additions & 0 deletions src/EFCore.ChangeTriggers.ChangeEventQueries/ChangeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
/// </summary>
public class ChangeEvent
{
/// <summary>
/// The description of the change event
/// </summary>
public required string Description { get; set; }

/// <summary>
/// Gets or sets the old/previous value.
/// </summary>
public string? OldValue { get; set; }

/// <summary>
/// Gets or sets the new/next value.
/// </summary>
public string? NewValue { get; set; }

/// <summary>
/// Gets or sets when the change occured.
/// </summary>
public DateTimeOffset ChangedAt { get; set; }
}

Expand All @@ -19,8 +31,14 @@ public class ChangeEvent
/// </summary>
public class ChangeEvent<TChangedBy, TChangeSource> : ChangeEvent
{
/// <summary>
/// Gets or sets who made the change.
/// </summary>
public TChangedBy? ChangedBy { get; set; }

/// <summary>
/// Gets or sets where the change originated from.
/// </summary>
public TChangeSource? ChangeSource { get; set; }
}
}
Expand All @@ -34,6 +52,9 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.ChangedBy
/// </summary>
public class ChangeEvent<TChangedBy> : ChangeEvent
{
/// <summary>
/// Gets or sets who made the change.
/// </summary>
public TChangedBy? ChangedBy { get; set; }
}
}
Expand All @@ -47,6 +68,9 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.ChangeSource
/// </summary>
public class ChangeEvent<TChangeSource> : ChangeEvent
{
/// <summary>
/// Gets or sets where the change originated from.
/// </summary>
public TChangeSource? ChangeSource { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace EFCore.ChangeTriggers.ChangeEventQueries.Configuration.Builders
{
/// <summary>
/// Provides an API for configuring change events.
/// </summary>
public class ChangeEventConfigurationBuilder
{
private ChangeEventConfiguration changeEventConfiguration;
Expand All @@ -14,6 +17,12 @@ public ChangeEventConfigurationBuilder()
{
}

/// <summary>
/// Configures an entity for change events.
/// </summary>
/// <typeparam name="TChangeEntity">The change entity type to configure.</typeparam>
/// <param name="configureEntity">An action to configure the entity.</param>
/// <returns></returns>
public ChangeEventConfigurationBuilder Configure<TChangeEntity>(Action<ChangeEventEntityConfigurationBuilder<TChangeEntity>> configureEntity)
{
var entityConfiguration = new ChangeEventEntityConfiguration(typeof(TChangeEntity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace EFCore.ChangeTriggers.ChangeEventQueries.Configuration.Builders
{
/// <summary>
/// Provides an API for configuring an entity for change events.
/// </summary>
/// <typeparam name="TChangeEntity"></typeparam>
public class ChangeEventEntityConfigurationBuilder<TChangeEntity>
{
private readonly ChangeEventEntityConfiguration entityConfiguration;
Expand All @@ -11,6 +15,11 @@ public ChangeEventEntityConfigurationBuilder(ChangeEventEntityConfiguration Enti
this.entityConfiguration = EntityConfiguration;
}

/// <summary>
/// Configures a property to be included in change event queries.
/// </summary>
/// <param name="propertySelector">A function to select the property to configure.</param>
/// <returns>The same builder instance so multiple calls can be chained.</returns>
public ChangeEventEntityPropertyConfigurationBuilder AddProperty(Expression<Func<TChangeEntity, string>> propertySelector)
{
var propertyConfiguration = new ChangeEventEntityPropertyConfiguration(propertySelector);
Expand All @@ -20,12 +29,20 @@ public ChangeEventEntityPropertyConfigurationBuilder AddProperty(Expression<Func
return new ChangeEventEntityPropertyConfigurationBuilder(propertyConfiguration);
}

/// <summary>
/// Configures inserts to be included in change event queries.
/// </summary>
/// <returns>The same builder instance so multiple calls can be chained.</returns>
public ChangeEventEntityConfigurationBuilder<TChangeEntity> AddInserts()
{
entityConfiguration.AddInserts = true;
return this;
}

/// <summary>
/// Configures deletes to be included in change event queries.
/// </summary>
/// <returns>The same builder instance so multiple calls can be chained.</returns>
public ChangeEventEntityConfigurationBuilder<TChangeEntity> AddDeletes()
{
entityConfiguration.AddDeletes = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace EFCore.ChangeTriggers.ChangeEventQueries.Configuration.Builders
{
/// <summary>
/// Provides an API for configuring a property included in change event queries.
/// </summary>
public class ChangeEventEntityPropertyConfigurationBuilder
{
private readonly ChangeEventEntityPropertyConfiguration propertyConfiguration;
Expand All @@ -9,6 +12,11 @@ public ChangeEventEntityPropertyConfigurationBuilder(ChangeEventEntityPropertyCo
this.propertyConfiguration = propertyConfiguration;
}

/// <summary>
/// Configures the description of this property.
/// </summary>
/// <param name="description"></param>
/// <returns></returns>
public ChangeEventEntityPropertyConfigurationBuilder WithDescription(string description)
{
this.propertyConfiguration.Description = description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

namespace EFCore.ChangeTriggers.ChangeEventQueries.Configuration
{
/// <summary>
/// Provides global configuration for change event queries.
/// </summary>
public class ChangeEventConfiguration
{
/// <summary>
/// Gets all entity configurations.
/// </summary>
public IReadOnlyDictionary<Type, ChangeEventEntityConfiguration> EntityConfigurations => entityConfigurations.AsReadOnly();

private readonly Dictionary<Type, ChangeEventEntityConfiguration> entityConfigurations = [];
Expand All @@ -14,6 +20,10 @@ public ChangeEventConfiguration()

}

/// <summary>
/// Creates a new change event configuration instance.
/// </summary>
/// <param name="buildAction">An action to build the configuration.</param>
public ChangeEventConfiguration(Action<ChangeEventConfigurationBuilder> buildAction)
{
var builder = new ChangeEventConfigurationBuilder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace EFCore.ChangeTriggers.ChangeEventQueries.Configuration
{
/// <summary>
/// Allows configuration of a single change entity for change event queries.
/// </summary>
/// <typeparam name="TChangeEntity"></typeparam>
public interface IChangeEventEntityConfiguration<TChangeEntity>
{
/// <summary>
/// Configures an entity for change events.
/// </summary>
/// <param name="builder"></param>
void Configure(ChangeEventEntityConfigurationBuilder<TChangeEntity> builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
namespace EFCore.ChangeTriggers.ChangeEventQueries
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
public static class ChangeTriggersDbContextOptionsBuilderExtensions
public static class DbContextOptionsBuilderExtensions
{
/// <summary>
/// Configures the DbContext to use change event queries.
/// </summary>
/// <param name="optionsBuilder">The builder being used to configure the context.</param>
/// <returns>The options builder so that further configuration can be chained.</returns>
public static void UseChangeEventQueries<TBuilder, TExtension>(
this ChangeTriggersDbContextOptionsBuilder<TBuilder, TExtension> builder,
this ChangeTriggersDbContextOptionsBuilder<TBuilder, TExtension> optionsBuilder,
Assembly configurationsAssembly,
Action<ChangeEventsDbContextOptionsBuilder>? optionsAction = null)
where TBuilder : ChangeTriggersDbContextOptionsBuilder<TBuilder, TExtension>
where TExtension : ChangeTriggersDbContextOptionsExtension, new()
{
var extension = builder.OptionsBuilder.GetOrCreateExtension<ChangeEventsDbContextOptionsExtension>()
var extension = optionsBuilder.OptionsBuilder.GetOrCreateExtension<ChangeEventsDbContextOptionsExtension>()
.WithConfigurationsAssembly(configurationsAssembly);

builder.OptionsBuilder.AsInfrastructure().AddOrUpdateExtension(extension);
optionsBuilder.OptionsBuilder.AsInfrastructure().AddOrUpdateExtension(extension);

builder.OptionsBuilder
optionsBuilder.OptionsBuilder
.ApplyConfiguration(optionsAction)
.ReplaceService<IAsyncQueryProvider, DbContextAwareEntityQueryProvider>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,43 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries
{
public static class IQueryableExtensions
{
/// <summary>
/// Projects change entities into human readable change events.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent> ToChangeEvents(this IQueryable query)
{
return new RootQueryBuilder(query).Build();
}

/// <summary>
/// Projects change entities into human readable change events, overriding any previous configuration.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <param name="configuration">The configuration to use for this query.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent> ToChangeEvents(this IQueryable query, ChangeEventConfiguration configuration)
{
return new RootQueryBuilder(query, configuration).Build();
}

/// <summary>
/// Projects change entities into human readable change events.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangedBy, TChangeSource>> ToChangeEvents<TChangedBy, TChangeSource>(this IQueryable query)
{
return new RootQueryBuilder<TChangedBy, TChangeSource>(query).Build();
}

/// <summary>
/// Projects change entities into human readable change events, overriding any previous configuration.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <param name="configuration">The configuration to use for this query.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangedBy, TChangeSource>> ToChangeEvents<TChangedBy, TChangeSource>(this IQueryable query, ChangeEventConfiguration configuration)
{
return new RootQueryBuilder<TChangedBy, TChangeSource>(query, configuration).Build();
Expand All @@ -35,7 +57,22 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.ChangedBy
{
public static class IQueryableExtensions
{
/// <summary>
/// Projects change entities into human readable change events.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangedBy>> ToChangeEvents<TChangedBy>(this IQueryable query)
{
return new ChangedByRootQueryBuilder<TChangedBy>(query).Build();
}

/// <summary>
/// Projects change entities into human readable change events, overriding any previous configuration.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <param name="configuration">The configuration to use for this query.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangedBy>> ToChangeEvents<TChangedBy>(this IQueryable query, ChangeEventConfiguration configuration)
{
return new ChangedByRootQueryBuilder<TChangedBy>(query, configuration).Build();
Expand All @@ -49,7 +86,22 @@ namespace EFCore.ChangeTriggers.ChangeEventQueries.ChangeSource
{
public static class IQueryableExtensions
{
/// <summary>
/// Projects change entities into human readable change events.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangeSource>> ToChangeEvents<TChangeSource>(this IQueryable query)
{
return new ChangeSourceRootQueryBuilder<TChangeSource>(query).Build();
}

/// <summary>
/// Projects change entities into human readable change events, overriding any previous configuration.
/// </summary>
/// <param name="query">The EF core queryable to project.</param>
/// <param name="configuration">The configuration to use for this query.</param>
/// <returns>The built change event query.</returns>
public static IQueryable<ChangeEvent<TChangeSource>> ToChangeEvents<TChangeSource>(this IQueryable query, ChangeEventConfiguration configuration)
{
return new ChangeSourceRootQueryBuilder<TChangeSource>(query, configuration).Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ public ChangeEventsDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilde
this.optionsBuilder = optionsBuilder;
}

/// <summary>
/// Configures the context to include insert events for every change entity.
/// </summary>
/// <param name="triggerNameFactory">Whether to include or exclude insert events for every change entity.</param>
/// <returns>The options builder so that further calls can be chained.</returns>
public ChangeEventsDbContextOptionsBuilder IncludeInserts(bool include = true)
=> WithOption(e => e.WithIncludeInserts(include));

/// <summary>
/// Configures the context to include delete events for every change entity.
/// </summary>
/// <param name="triggerNameFactory">Whether to include or exclude delete events for every change entity.</param>
/// <returns>The options builder so that further calls can be chained.</returns>
public ChangeEventsDbContextOptionsBuilder IncludeDeletes(bool include = true)
=> WithOption(e => e.WithIncludeDeletes(include));

Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.ChangeTriggers/Exceptions/ExceptionStrings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace EFCore.ChangeTriggers.Exceptions
namespace EFCore.ChangeTriggers.Exceptions
{
internal class ExceptionStrings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
using System.Diagnostics;

namespace EFCore.ChangeTriggers.Migrations
{
Expand Down
5 changes: 4 additions & 1 deletion src/EFCore.ChangeTriggers/_.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace EFCore.ChangeTriggers
{
/// <summary>
/// A dummy struct for use with nameof()
/// </summary>
internal struct _
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using EFCore.ChangeTriggers.ChangeEventQueries.Configuration;
using EFCore.ChangeTriggers.ChangeEventQueries.Configuration.Builders;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TestHarness.DbModels.Users;

namespace TestHarness.Configuration
{
public class UserChangeConfiguration : IChangeEventEntityConfiguration<UserChange>
public class UserChangeConfiguration : IChangeEventEntityConfiguration<UserChange>, IEntityTypeConfiguration<UserChange>
{
public void Configure(ChangeEventEntityConfigurationBuilder<UserChange> builder)
{
Expand All @@ -13,5 +15,10 @@ public void Configure(ChangeEventEntityConfigurationBuilder<UserChange> builder)
builder.AddProperty(uc => uc.PrimaryPaymentMethod!.Id.ToString())
.WithDescription("Primary payment method updated");
}

public void Configure(EntityTypeBuilder<UserChange> builder)
{
throw new NotImplementedException();
}
}
}
1 change: 0 additions & 1 deletion tests/TestHarness/TestHarness/MyDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EFCore.ChangeTriggers;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using TestHarness.DbModels.PaymentMethods;
using TestHarness.DbModels.Permissions;
using TestHarness.DbModels.Users;
Expand Down
Loading

0 comments on commit 5636a42

Please sign in to comment.