Przeglądaj źródła

refactor

master
yangxiaodong 7 lat temu
rodzic
commit
65e496d3a4
3 zmienionych plików z 20 dodań i 128 usunięć
  1. +4
    -23
      src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs
  2. +4
    -20
      src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs
  3. +12
    -85
      src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs

+ 4
- 23
src/Cap.Consistency.EntityFrameworkCore/ConsistencyDbContext.cs Wyświetl plik

@@ -4,31 +4,12 @@ using Microsoft.EntityFrameworkCore;

namespace Cap.Consistency.EntityFrameworkCore
{
/// <summary>
/// Base class for the Entity Framework database context used for consistency.
/// </summary>
public class ConsistencyDbContext : ConsistencyDbContext<ConsistencyMessage, string>
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsistencyDbContext"/>.
/// </summary>
public ConsistencyDbContext() { }

/// <summary>
/// Initializes a new instance of the <see cref="ConsistencyDbContext"/>.
/// </summary>
/// <param name="options">The options to be used by a <see cref="DbContext"/>.</param>
public ConsistencyDbContext(DbContextOptions options) : base(options) { }
}

/// <summary>
/// Base class for the Entity Framework database context used for consistency.
/// </summary>
/// <typeparam name="TMessage">The type of message objects.</typeparam>
/// <typeparam name="Tkey">The type of the primarky key for messages.</typeparam>
public abstract class ConsistencyDbContext<TMessage, Tkey> : DbContext
where TMessage : ConsistencyMessage<Tkey>
where Tkey : IEquatable<Tkey>
public class ConsistencyDbContext : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsistencyDbContext"/>.
@@ -42,9 +23,9 @@ namespace Cap.Consistency.EntityFrameworkCore
public ConsistencyDbContext(DbContextOptions options) : base(options) { }

/// <summary>
/// Gets or sets the <see cref="DbSet{TMessage}"/> of Messages.
/// Gets or sets the <see cref="DbSet{ConsistencyMessage}"/> of Messages.
/// </summary>
public DbSet<TMessage> Messages { get; set; }
public DbSet<ConsistencyMessage> Messages { get; set; }

/// <summary>
/// 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.
/// </param>
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<TMessage>(b => {
modelBuilder.Entity<ConsistencyMessage>(b => {
b.HasKey(m => m.Id);
b.ToTable("ConsistencyMessages");
});


+ 4
- 20
src/Cap.Consistency.EntityFrameworkCore/ConsistencyEntityFrameworkBuilderExtensions.cs Wyświetl plik

@@ -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.
/// </summary>
/// <typeparam name="TContext">The Entity Framework database context to use.</typeparam>
/// <param name="builder">The <see cref="ConsistencyBuilder"/> instance this method extends.</param>
/// <param name="services">The <see cref="ConsistencyBuilder"/> instance this method extends.</param>
/// <returns>The <see cref="ConsistencyBuilder"/> instance this method extends.</returns>
public static ConsistencyBuilder AddEntityFrameworkStores<TContext>(this ConsistencyBuilder builder)
where TContext : DbContext {

builder.Services.AddScoped(typeof(IConsistencyMessageStore<>).MakeGenericType(builder.MessageType),
typeof(ConsistencyMessageStore<,>).MakeGenericType(typeof(ConsistencyMessage), typeof(TContext)));
builder.Services.AddScoped<IConsistencyMessageStore, ConsistencyMessageStore<TContext>>();

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;
}
}
}

+ 12
- 85
src/Cap.Consistency.EntityFrameworkCore/ConsistencyMessageStore.cs Wyświetl plik

@@ -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
{
/// <summary>
/// Represents a new instance of a persistence store for messages, using the default implementation
/// of <see cref="ConsistencyMessage{TKey}"/> with a string as a primary key.
/// </summary>
public class ConsistencyMessageStore : ConsistencyMessageStore<ConsistencyMessage, DbContext, string>
{
/// <summary>
/// Constructs a new instance of <see cref="ConsistencyMessageStore"/>.
/// </summary>
/// <param name="context">The <see cref="DbContext"/>.</param>
public ConsistencyMessageStore(DbContext context) : base(context) {
}
}

/// <summary>
/// Creates a new instance of a persistence store for the specified message type.
/// </summary>
/// <typeparam name="TMessage">The type representing a message.</typeparam>
public class ConsistencyMessageStore<TMessage> : ConsistencyMessageStore<TMessage, DbContext, string>
where TMessage : ConsistencyMessage<string>
{
/// <summary>
/// Constructs a new instance of <see cref="ConsistencyMessageStore{TMessage}"/>.
/// </summary>
/// <param name="context">The <see cref="DbContext"/>.</param>
public ConsistencyMessageStore(DbContext context) : base(context) {
}
}

/// <summary>
/// Represents a new instance of a persistence store for the specified message types.
/// </summary>
/// <typeparam name="TMessage">The type representing a message.</typeparam>
/// <typeparam name="TContext">The type of the data context class used to access the store.</typeparam>
public class ConsistencyMessageStore<TMessage, TContext> : ConsistencyMessageStore<TMessage, TContext, string>
where TMessage : ConsistencyMessage<string>
where TContext : DbContext
{
/// <summary>
/// Constructs a new instance of <see cref="ConsistencyMessageStore{TMessage, TContext, TKey}"/>.
/// </summary>
/// <param name="context">The <see cref="DbContext"/>.</param>
public ConsistencyMessageStore(TContext context) : base(context) {
}
}

/// <summary>
/// Represents a new instance of a persistence store for the specified message types.
/// </summary>
/// <typeparam name="TMessage">The type representing a message.</typeparam>
/// <typeparam name="ConsistencyMessage">The type representing a message.</typeparam>
/// <typeparam name="TContext">The type of the data context class used to access the store.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a message.</typeparam>
public abstract class ConsistencyMessageStore<TMessage, TContext, TKey> : IConsistencyMessageStore<TMessage>
where TMessage : ConsistencyMessage<TKey>
where TContext : DbContext
where TKey : IEquatable<TKey>
public class ConsistencyMessageStore<TContext> : IConsistencyMessageStore where TContext : DbContext
{
private bool _disposed;

/// <summary>
/// Constructs a new instance of <see cref="ConsistencyMessageStore{TMessage, TContext, TKey}"/>.
/// Constructs a new instance of <see cref="ConsistencyMessageStore{ConsistencyMessage, TContext, TKey}"/>.
/// </summary>
/// <param name="context">The <see cref="DbContext"/>.</param>
public ConsistencyMessageStore(TContext context) {
@@ -79,7 +31,7 @@ namespace Cap.Consistency.EntityFrameworkCore

public TContext Context { get; private set; }

private DbSet<TMessage> MessageSet { get { return Context.Set<TMessage>(); } }
private DbSet<ConsistencyMessage> MessageSet { get { return Context.Set<ConsistencyMessage>(); } }

/// <summary>
/// Creates the specified <paramref name="message"/> in the consistency message store.
@@ -87,7 +39,7 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <param name="message">The message to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="OperateResult"/> of the creation operation.</returns>
public async virtual Task<OperateResult> CreateAsync(TMessage message, CancellationToken cancellationToken) {
public async virtual Task<OperateResult> CreateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (message == null) {
@@ -104,7 +56,7 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <param name="message">The message to delete.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="OperateResult"/> of the update operation.</returns>
public async virtual Task<OperateResult> DeleteAsync(TMessage message, CancellationToken cancellationToken) {
public async virtual Task<OperateResult> DeleteAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (message == null) {
@@ -129,35 +81,10 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <returns>
/// The <see cref="Task"/> that represents the asynchronous operation, containing the message matching the specified <paramref name="messageId"/> if it exists.
/// </returns>
public virtual Task<TMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
public virtual Task<ConsistencyMessage> FindByIdAsync(string messageId, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
var id = ConvertIdFromString(messageId);
return MessageSet.FindAsync(new object[] { id }, cancellationToken);
}

/// <summary>
/// Converts the provided <paramref name="id"/> to a strongly typed key object.
/// </summary>
/// <param name="id">The id to convert.</param>
/// <returns>An instance of <typeparamref name="TKey"/> representing the provided <paramref name="id"/>.</returns>
public virtual TKey ConvertIdFromString(string id) {
if (id == null) {
return default(TKey);
}
return (TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(id);
}

/// <summary>
/// Converts the provided <paramref name="id"/> to its string representation.
/// </summary>
/// <param name="id">The id to convert.</param>
/// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns>
public virtual string ConvertIdToString(TKey id) {
if (object.Equals(id, default(TKey))) {
return null;
}
return id.ToString();
return MessageSet.FindAsync(new object[] { messageId }, cancellationToken);
}

/// <summary>
@@ -166,13 +93,13 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <param name="message">The message whose identifier should be retrieved.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the identifier for the specified <paramref name="message"/>.</returns>
public Task<string> GetMessageIdAsync(TMessage message, CancellationToken cancellationToken) {
public Task<string> 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);
}

/// <summary>
@@ -181,7 +108,7 @@ namespace Cap.Consistency.EntityFrameworkCore
/// <param name="message">The message to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="OperateResult"/> of the update operation.</returns>
public async virtual Task<OperateResult> UpdateAsync(TMessage message, CancellationToken cancellationToken) {
public async virtual Task<OperateResult> UpdateAsync(ConsistencyMessage message, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (message == null) {


Ładowanie…
Anuluj
Zapisz