diff --git a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs index f2e045c..9a372d8 100644 --- a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs +++ b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs @@ -4,31 +4,12 @@ using Microsoft.EntityFrameworkCore; namespace Cap.Consistency.EntityFrameworkCore { - /// - /// Base class for the Entity Framework database context used for consistency. - /// - public class ConsistencyDbContext : ConsistencyDbContext - { - /// - /// Initializes a new instance of the . - /// - public ConsistencyDbContext() { } - - /// - /// Initializes a new instance of the . - /// - /// The options to be used by a . - public ConsistencyDbContext(DbContextOptions options) : base(options) { } - } - /// /// Base class for the Entity Framework database context used for consistency. /// /// The type of message objects. /// The type of the primarky key for messages. - public abstract class ConsistencyDbContext : DbContext - where TMessage : ConsistencyMessage - where Tkey : IEquatable + public class ConsistencyDbContext : DbContext { /// /// Initializes a new instance of the . @@ -42,9 +23,9 @@ namespace Cap.Consistency.EntityFrameworkCore public ConsistencyDbContext(DbContextOptions options) : base(options) { } /// - /// Gets or sets the of Messages. + /// Gets or sets the of Messages. /// - public DbSet Messages { get; set; } + public DbSet Messages { get; set; } /// /// Configures the schema for the identity framework. @@ -53,7 +34,7 @@ namespace Cap.Consistency.EntityFrameworkCore /// The builder being used to construct the model for this context. /// protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity(b => { + modelBuilder.Entity(b => { b.HasKey(m => m.Id); b.ToTable("ConsistencyMessages"); }); diff --git a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs index d2cfc2d..c913cf5 100644 --- a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs +++ b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs @@ -1,8 +1,5 @@ -using System; -using System.Reflection; -using Cap.Consistency; -using Cap.Consistency.EntityFrameworkCore; -using Cap.Consistency.Infrastructure; +using Cap.Consistency.EntityFrameworkCore; +using Cap.Consistency.Store; using Microsoft.EntityFrameworkCore; namespace Microsoft.Extensions.DependencyInjection @@ -16,27 +13,14 @@ namespace Microsoft.Extensions.DependencyInjection /// Adds an Entity Framework implementation of message stores. /// /// The Entity Framework database context to use. - /// The instance this method extends. + /// The instance this method extends. /// The instance this method extends. public static ConsistencyBuilder AddEntityFrameworkStores(this ConsistencyBuilder builder) where TContext : DbContext { - builder.Services.AddScoped(typeof(IConsistencyMessageStore<>).MakeGenericType(builder.MessageType), - typeof(ConsistencyMessageStore<,>).MakeGenericType(typeof(ConsistencyMessage), typeof(TContext))); + builder.Services.AddScoped>(); return builder; } - - private static TypeInfo FindGenericBaseType(Type currentType, Type genericBaseType) { - var type = currentType.GetTypeInfo(); - while (type.BaseType != null) { - type = type.BaseType.GetTypeInfo(); - var genericType = type.IsGenericType ? type.GetGenericTypeDefinition() : null; - if (genericType != null && genericType == genericBaseType) { - return type; - } - } - return null; - } } } \ No newline at end of file diff --git a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs index c36bd12..5ba1035 100644 --- a/src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs +++ b/src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs @@ -3,71 +3,23 @@ using System.ComponentModel; using System.Threading; using System.Threading.Tasks; using Cap.Consistency.Infrastructure; +using Cap.Consistency.Store; using Microsoft.EntityFrameworkCore; namespace Cap.Consistency.EntityFrameworkCore { - /// - /// Represents a new instance of a persistence store for messages, using the default implementation - /// of with a string as a primary key. - /// - public class ConsistencyMessageStore : ConsistencyMessageStore - { - /// - /// Constructs a new instance of . - /// - /// The . - public ConsistencyMessageStore(DbContext context) : base(context) { - } - } - - /// - /// Creates a new instance of a persistence store for the specified message type. - /// - /// The type representing a message. - public class ConsistencyMessageStore : ConsistencyMessageStore - where TMessage : ConsistencyMessage - { - /// - /// Constructs a new instance of . - /// - /// The . - public ConsistencyMessageStore(DbContext context) : base(context) { - } - } - /// /// Represents a new instance of a persistence store for the specified message types. /// - /// The type representing a message. - /// The type of the data context class used to access the store. - public class ConsistencyMessageStore : ConsistencyMessageStore - where TMessage : ConsistencyMessage - where TContext : DbContext - { - /// - /// Constructs a new instance of . - /// - /// The . - public ConsistencyMessageStore(TContext context) : base(context) { - } - } - - /// - /// Represents a new instance of a persistence store for the specified message types. - /// - /// The type representing a message. + /// The type representing a message. /// The type of the data context class used to access the store. /// The type of the primary key for a message. - public abstract class ConsistencyMessageStore : IConsistencyMessageStore - where TMessage : ConsistencyMessage - where TContext : DbContext - where TKey : IEquatable + public class ConsistencyMessageStore : IConsistencyMessageStore where TContext : DbContext { private bool _disposed; /// - /// Constructs a new instance of . + /// Constructs a new instance of . /// /// The . public ConsistencyMessageStore(TContext context) { @@ -79,7 +31,7 @@ namespace Cap.Consistency.EntityFrameworkCore public TContext Context { get; private set; } - private DbSet MessageSet { get { return Context.Set(); } } + private DbSet MessageSet { get { return Context.Set(); } } /// /// Creates the specified in the consistency message store. @@ -87,7 +39,7 @@ namespace Cap.Consistency.EntityFrameworkCore /// The message to create. /// The used to propagate notifications that the operation should be canceled. /// The that represents the asynchronous operation, containing the of the creation operation. - public async virtual Task CreateAsync(TMessage message, CancellationToken cancellationToken) { + public async virtual Task CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (message == null) { @@ -104,7 +56,7 @@ namespace Cap.Consistency.EntityFrameworkCore /// The message to delete. /// The used to propagate notifications that the operation should be canceled. /// The that represents the asynchronous operation, containing the of the update operation. - public async virtual Task DeleteAsync(TMessage message, CancellationToken cancellationToken) { + public async virtual Task DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (message == null) { @@ -129,35 +81,10 @@ namespace Cap.Consistency.EntityFrameworkCore /// /// The that represents the asynchronous operation, containing the message matching the specified if it exists. /// - public virtual Task FindByIdAsync(string messageId, CancellationToken cancellationToken) { + public virtual Task FindByIdAsync(string messageId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); - var id = ConvertIdFromString(messageId); - return MessageSet.FindAsync(new object[] { id }, cancellationToken); - } - - /// - /// Converts the provided to a strongly typed key object. - /// - /// The id to convert. - /// An instance of representing the provided . - public virtual TKey ConvertIdFromString(string id) { - if (id == null) { - return default(TKey); - } - return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id); - } - - /// - /// Converts the provided to its string representation. - /// - /// The id to convert. - /// An representation of the provided . - public virtual string ConvertIdToString(TKey id) { - if (object.Equals(id, default(TKey))) { - return null; - } - return id.ToString(); + return MessageSet.FindAsync(new object[] { messageId }, cancellationToken); } /// @@ -166,13 +93,13 @@ namespace Cap.Consistency.EntityFrameworkCore /// The message whose identifier should be retrieved. /// The used to propagate notifications that the operation should be canceled. /// The that represents the asynchronous operation, containing the identifier for the specified . - public Task GetMessageIdAsync(TMessage message, CancellationToken cancellationToken) { + public Task GeConsistencyMessageIdAsync(ConsistencyMessage message, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (message == null) { throw new ArgumentNullException(nameof(message)); } - return Task.FromResult(ConvertIdToString(message.Id)); + return Task.FromResult(message.Id); } /// @@ -181,7 +108,7 @@ namespace Cap.Consistency.EntityFrameworkCore /// The message to update. /// The used to propagate notifications that the operation should be canceled. /// The that represents the asynchronous operation, containing the of the update operation. - public async virtual Task UpdateAsync(TMessage message, CancellationToken cancellationToken) { + public async virtual Task UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); if (message == null) {