Browse Source

refactor

undefined
yangxiaodong 7 years ago
parent
commit
c04f65c89c
3 changed files with 9 additions and 32 deletions
  1. +5
    -19
      src/Cap.Consistency/Extensions/BuilderExtensions.cs
  2. +1
    -9
      src/Cap.Consistency/Extensions/ConsistencyOptions.cs
  3. +3
    -4
      src/Cap.Consistency/Extensions/ServiceCollectionExtensions.cs

+ 5
- 19
src/Cap.Consistency/Extensions/BuilderExtensions.cs View File

@@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Builder
/// </summary> /// </summary>
public static class BuilderExtensions public static class BuilderExtensions
{ {
/// <summary>
///<summary>
/// Enables Consistence for the current application /// Enables Consistence for the current application
/// </summary> /// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param> /// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param>
@@ -20,34 +20,20 @@ namespace Microsoft.AspNetCore.Builder
if (app == null) { if (app == null) {
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
var marker = app.ApplicationServices.GetService<ConsistencyMarkerService>(); var marker = app.ApplicationServices.GetService<ConsistencyMarkerService>();
if (marker == null) { if (marker == null) {
throw new InvalidOperationException("Add Consistency must be called on the service collection."); throw new InvalidOperationException("Add Consistency must be called on the service collection.");
} }


return app;
}

public static IApplicationBuilder UserRouter(this IApplicationBuilder builder, ITopicRoute router) {
if (builder == null) {
throw new ArgumentNullException(nameof(builder));
}

if (router == null) {
throw new ArgumentNullException(nameof(router));
}

var marker = builder.ApplicationServices.GetService<ConsistencyMarkerService>();
if (marker == null) {
throw new InvalidOperationException("Add Consistency must be called on the service collection.");
}
var router = app.ApplicationServices.GetService<ITopicRoute>();


var context = new TopicRouteContext(); var context = new TopicRouteContext();


router.RouteAsync(context).Wait(); router.RouteAsync(context).Wait();


return builder;
return app;
} }


} }

+ 1
- 9
src/Cap.Consistency/Extensions/ConsistencyOptions.cs View File

@@ -7,14 +7,6 @@ namespace Microsoft.AspNetCore.Builder
/// </summary> /// </summary>
public class ConsistencyOptions public class ConsistencyOptions
{ {
/// <summary>
/// Gets or sets the <see cref="BrokerOptions"/> for the consistency system.
/// </summary>
public BrokerOptions Broker { get; set; } = new BrokerOptions();


public string BrokerUrlList { get; set; }

public string BrokerUrlList { get; set; } = "localhost:9092";
} }
} }

+ 3
- 4
src/Cap.Consistency/Extensions/ServiceCollectionExtensions.cs View File

@@ -1,5 +1,6 @@
using System; using System;
using Cap.Consistency; using Cap.Consistency;
using Cap.Consistency.Infrastructure;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;


@@ -17,7 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="services">The services available in the application.</param> /// <param name="services">The services available in the application.</param>
/// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns> /// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns>
public static ConsistencyBuilder AddConsistency<TMessage>(this IServiceCollection services) public static ConsistencyBuilder AddConsistency<TMessage>(this IServiceCollection services)
where TMessage : class {
where TMessage : ConsistencyMessage {
return services.AddConsistency<TMessage>(setupAction: null); return services.AddConsistency<TMessage>(setupAction: null);
} }


@@ -28,13 +29,11 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param> /// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param>
/// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns> /// <returns>An <see cref="ConsistencyBuilder"/> for application services.</returns>
public static ConsistencyBuilder AddConsistency<TMessage>(this IServiceCollection services, Action<ConsistencyOptions> setupAction) public static ConsistencyBuilder AddConsistency<TMessage>(this IServiceCollection services, Action<ConsistencyOptions> setupAction)
where TMessage : class {
where TMessage : ConsistencyMessage {
services.TryAddSingleton<ConsistencyMarkerService>(); services.TryAddSingleton<ConsistencyMarkerService>();


services.TryAddScoped<ConsistencyMessageManager<TMessage>, ConsistencyMessageManager<TMessage>>(); services.TryAddScoped<ConsistencyMessageManager<TMessage>, ConsistencyMessageManager<TMessage>>();


services.AddSingleton<KafkaConsistency>();

if (setupAction != null) { if (setupAction != null) {
services.Configure(setupAction); services.Configure(setupAction);
} }


Loading…
Cancel
Save