From 60a5f9c89e46475dfab33fdcf0815bd6db27569f Mon Sep 17 00:00:00 2001 From: John0King Date: Wed, 17 Jan 2018 22:52:33 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E6=83=B3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abstractions/HubHandlerData.cs | 20 ++++ .../Abstractions/INotifyDataConverter.cs | 22 +++++ .../Abstractions/INotifyHubHandler.cs | 18 ++++ .../Abstractions/INotifyResponse.cs | 21 ++++ .../Abstractions/IPaymentNotifyData.cs | 13 +++ src/ICanPay.Core/Abstractions/NotifyHub.cs | 98 +++++++++++++++++++ .../Abstractions/ProcessResult.cs | 28 ++++++ 7 files changed, 220 insertions(+) create mode 100644 src/ICanPay.Core/Abstractions/HubHandlerData.cs create mode 100644 src/ICanPay.Core/Abstractions/INotifyDataConverter.cs create mode 100644 src/ICanPay.Core/Abstractions/INotifyHubHandler.cs create mode 100644 src/ICanPay.Core/Abstractions/INotifyResponse.cs create mode 100644 src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs create mode 100644 src/ICanPay.Core/Abstractions/NotifyHub.cs create mode 100644 src/ICanPay.Core/Abstractions/ProcessResult.cs diff --git a/src/ICanPay.Core/Abstractions/HubHandlerData.cs b/src/ICanPay.Core/Abstractions/HubHandlerData.cs new file mode 100644 index 0000000..2b20c06 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/HubHandlerData.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + public class HubHandlerData + { + public string GatewayOrder { get; set; } + public string BusinessOrder { get; set; } + + public DateTimeOffset ProcessTime { get; set; } + + public string GatewayType { get; set; } + + public object RawData { get; set; } + } +} diff --git a/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs b/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs new file mode 100644 index 0000000..3b75211 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Core.Abstractions +{ + /// + /// 通知数据转换器, 他从HttpContext 转换为强类型数据 + /// + /// HttpContext在实现的类中采用构造函数注入, + public interface INotifyDataConverter + { + Task ConvertDataAsync(); + } + + public interface INotifyDataConverter : INotifyDataConverter + { + new Task ConvertDataAsync(); + } +} diff --git a/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs b/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs new file mode 100644 index 0000000..7f632f9 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + /// + /// NotifyHub 处理器 + /// + public interface INotifyHubHandler + { + ProcessResult Process(IPaymentNotifyData notifyData); + + } + +} diff --git a/src/ICanPay.Core/Abstractions/INotifyResponse.cs b/src/ICanPay.Core/Abstractions/INotifyResponse.cs new file mode 100644 index 0000000..8f0130c --- /dev/null +++ b/src/ICanPay.Core/Abstractions/INotifyResponse.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + public interface INotifyResponse + { + + INotifyResponse SetStateCode(int code); + INotifyResponse SetContextType(string type); + INotifyResponse AddHeader(IDictionary headers); + + INotifyResponse AddCookies(IDictionary cookies); + + Task WriteBodyAsync(Stream bodyStream); + } +} diff --git a/src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs b/src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs new file mode 100644 index 0000000..e79b8f9 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + public interface IPaymentNotifyData + { + + } +} diff --git a/src/ICanPay.Core/Abstractions/NotifyHub.cs b/src/ICanPay.Core/Abstractions/NotifyHub.cs new file mode 100644 index 0000000..39a6eb6 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/NotifyHub.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + public sealed class NotifyHub + { + private readonly INotifyHubHandler _hubHandler; + private Func _successHandler; + private Func _failHandler; + private Func _beforeProcessHandler; + private Func _afterProcessHandler; + private Func _exceptionHandler; + public NotifyHub(INotifyHubHandler hubHandler) + { + _hubHandler = hubHandler; + } + + public NotifyHub WhenSuccess(Func deleget) + { + _successHandler = deleget; + return this; + } + public NotifyHub WhenFail(Func deleget) + { + _failHandler = deleget; + return this; + } + + public NotifyHub WhenBeforeProcess(Func deleget) + { + _beforeProcessHandler = deleget; + return this; + } + public NotifyHub WhenAfterProcess(Func deleget) + { + _afterProcessHandler = deleget; + return this; + } + + public NotifyHub WhenException(Func deleget) + { + _exceptionHandler = deleget; + return this; + } + + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + public async Task ProcessAsync(object data) + { + // 首先检查, 成功和异常处理必须注册 + + if (_successHandler == null) + { + throw new InvalidOperationException("No Success delegate registed"); + } + if (_exceptionHandler == null) + { + throw new InvalidOperationException("No Exception delegate registed"); + } + + // before + if(_beforeProcessHandler != null) + { + await _beforeProcessHandler(null); + } + + try + { + var result = _hubHandler.Process(null); + if (result.IsSuccess) + { + await _successHandler(null); + } + else + { + if (_failHandler != null) + { + await _failHandler(null); + } + } + } + catch(Exception e) + { + await _exceptionHandler(e); + } + if(_afterProcessHandler != null) + { + await _afterProcessHandler(null); + } + + + + } + } +} diff --git a/src/ICanPay.Core/Abstractions/ProcessResult.cs b/src/ICanPay.Core/Abstractions/ProcessResult.cs new file mode 100644 index 0000000..4a4d282 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/ProcessResult.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + public class ProcessResult + { + public bool IsSuccess { get; private set; } + public HubHandlerData Data { get; private set; } + private ProcessResult(bool state,HubHandlerData data) + { + IsSuccess = state; + Data = data; + } + public static ProcessResult Success(HubHandlerData data) + { + return new ProcessResult(true, data); + } + + public static ProcessResult Fail(HubHandlerData data) + { + return new ProcessResult(false, data); + } + } +} From eb5b163b49fad47c9d843e4f9f4e0e4b0f3dfd5a Mon Sep 17 00:00:00 2001 From: John0King Date: Wed, 7 Mar 2018 23:19:20 +0800 Subject: [PATCH 02/14] a few code change --- .../Abstractions/HubHandlerData.cs | 20 ------ src/ICanPay.Core/Abstractions/HubOrder.cs | 61 +++++++++++++++++++ .../Abstractions/INotifyDataConverter.cs | 2 +- .../Abstractions/INotifyHubHandler.cs | 1 + src/ICanPay.Core/Abstractions/NotifyHub.cs | 2 +- .../Abstractions/ProcessResult.cs | 8 +-- 6 files changed, 68 insertions(+), 26 deletions(-) delete mode 100644 src/ICanPay.Core/Abstractions/HubHandlerData.cs create mode 100644 src/ICanPay.Core/Abstractions/HubOrder.cs diff --git a/src/ICanPay.Core/Abstractions/HubHandlerData.cs b/src/ICanPay.Core/Abstractions/HubHandlerData.cs deleted file mode 100644 index 2b20c06..0000000 --- a/src/ICanPay.Core/Abstractions/HubHandlerData.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ICanPay.Abstractions -{ - public class HubHandlerData - { - public string GatewayOrder { get; set; } - public string BusinessOrder { get; set; } - - public DateTimeOffset ProcessTime { get; set; } - - public string GatewayType { get; set; } - - public object RawData { get; set; } - } -} diff --git a/src/ICanPay.Core/Abstractions/HubOrder.cs b/src/ICanPay.Core/Abstractions/HubOrder.cs new file mode 100644 index 0000000..c75d0af --- /dev/null +++ b/src/ICanPay.Core/Abstractions/HubOrder.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + /// + /// 通用的处理数据 + /// + public class HubOrder + { + /// + /// 支付机构订单Id + /// + public string GatewayOrderId { get; set; } + + /// + /// 业务逻辑的订单Id + /// + public string BusinessOrderId { get; set; } + + /// + /// 支付机构此次处理的时间 + /// + public DateTimeOffset ProcessTime { get; set; } + + /// + /// 支付处理器给的类型 + /// + /// 可以利用此值决定 的类型 + public string GatewayType { get; set; } + + /// + /// 原本的数据 + /// + public object RawData { get; set; } + + /// + /// 状态, 当为 null 时,为未知 + /// + public HubHandlerDataStatus? Status { get; set; } + } + + /// + /// 支付数据状态 + /// + public enum HubHandlerDataStatus + { + /// + /// 支付成功 + /// + Success, + + /// + /// 支付失败 + /// + Fail + } +} diff --git a/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs b/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs index 3b75211..17c90f2 100644 --- a/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs +++ b/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Core.Abstractions +namespace ICanPay.Abstractions { /// /// 通知数据转换器, 他从HttpContext 转换为强类型数据 diff --git a/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs b/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs index 7f632f9..b9bb6fe 100644 --- a/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs +++ b/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs @@ -11,6 +11,7 @@ namespace ICanPay.Abstractions /// public interface INotifyHubHandler { + INotifyDataConverter Converter { get; } ProcessResult Process(IPaymentNotifyData notifyData); } diff --git a/src/ICanPay.Core/Abstractions/NotifyHub.cs b/src/ICanPay.Core/Abstractions/NotifyHub.cs index 39a6eb6..3fc8dfe 100644 --- a/src/ICanPay.Core/Abstractions/NotifyHub.cs +++ b/src/ICanPay.Core/Abstractions/NotifyHub.cs @@ -47,7 +47,7 @@ public NotifyHub WhenException(Func deleget) return this; } - [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + //[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public async Task ProcessAsync(object data) { // 首先检查, 成功和异常处理必须注册 diff --git a/src/ICanPay.Core/Abstractions/ProcessResult.cs b/src/ICanPay.Core/Abstractions/ProcessResult.cs index 4a4d282..d985145 100644 --- a/src/ICanPay.Core/Abstractions/ProcessResult.cs +++ b/src/ICanPay.Core/Abstractions/ProcessResult.cs @@ -9,18 +9,18 @@ namespace ICanPay.Abstractions public class ProcessResult { public bool IsSuccess { get; private set; } - public HubHandlerData Data { get; private set; } - private ProcessResult(bool state,HubHandlerData data) + public HubOrder Data { get; private set; } + private ProcessResult(bool state,HubOrder data) { IsSuccess = state; Data = data; } - public static ProcessResult Success(HubHandlerData data) + public static ProcessResult Success(HubOrder data) { return new ProcessResult(true, data); } - public static ProcessResult Fail(HubHandlerData data) + public static ProcessResult Fail(HubOrder data) { return new ProcessResult(false, data); } From e94d4ad2e35003193ef7523c44f5dc6346ecf011 Mon Sep 17 00:00:00 2001 From: John0King Date: Wed, 21 Mar 2018 20:59:25 +0800 Subject: [PATCH 03/14] concept of IKeyValueProvider --- .../HttpContextKeyValueProvider.cs | 96 +++++++++++++++++++ ICanPay.AspNetCore/ICanPay.AspNetCore.csproj | 11 +++ ICanPay.sln | 6 ++ .../Abstractions/IKeyValueProvider.cs | 40 ++++++++ src/ICanPay.Core/Abstractions/NotifyHub.cs | 18 ++-- test/ICanPay.UnitTest/CodeWrite.cs | 19 ++++ test/ICanPay.UnitTest/ICanPay.UnitTest.csproj | 1 + 7 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 ICanPay.AspNetCore/HttpContextKeyValueProvider.cs create mode 100644 ICanPay.AspNetCore/ICanPay.AspNetCore.csproj create mode 100644 src/ICanPay.Core/Abstractions/IKeyValueProvider.cs create mode 100644 test/ICanPay.UnitTest/CodeWrite.cs diff --git a/ICanPay.AspNetCore/HttpContextKeyValueProvider.cs b/ICanPay.AspNetCore/HttpContextKeyValueProvider.cs new file mode 100644 index 0000000..3ccdbcf --- /dev/null +++ b/ICanPay.AspNetCore/HttpContextKeyValueProvider.cs @@ -0,0 +1,96 @@ +using ICanPay.Abstractions; +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace ICanPay.AspNetCore +{ + /// + /// Asp.Net-Core 的键值提供器 + /// + public class HttpContextKeyValueProvider : IKeyValueProvider + { + public HttpContext HttpContext { get; } + + public IEnumerable SupportedParts => _supportedParts; + + private IEnumerable _supportedParts = new[] { "QueryString", "Form" }; + + public HttpContextKeyValueProvider(IHttpContextAccessor httpContextAccessor) + { + HttpContext = httpContextAccessor.HttpContext; + } + + /// + public byte[] Get() + { + using(var ms = new MemoryStream()) + { + HttpContext.Request.Body.CopyTo(ms); + return ms.ToArray(); + } + + } + + /// + public IEnumerable GetKeys(string part = null) + { + var request = HttpContext.Request; + var result = new List(); + if(NullEq(part,"QueryString")) + { + result.AddRange(request.Query.Keys); + + + } + if(NullEq(part, "Form")) + { + if (request.HasFormContentType && Eq("POST", request.Method)) + { + result.AddRange(request.Form.Keys); + } + } + + return result.Distinct(); + } + + /// + public string GetValue(string key, string part = null) + { + var request = HttpContext.Request; + if (NullEq(part, "QueryString")) + { + if (request.Query.ContainsKey(key)) + { + return request.Query[key]; + } + } + + if (NullEq(part, "Form")) + { + if (request.HasFormContentType && Eq("POST", request.Method)) + { + if (request.Form.ContainsKey(key)) + { + return request.Form[key]; + } + } + + } + + return null; + } + + + private bool NullEq(string val,string compare) + { + return string.IsNullOrEmpty(val) || string.Equals(val, compare, StringComparison.OrdinalIgnoreCase); + } + private bool Eq(string str1, string str2) + { + return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/ICanPay.AspNetCore/ICanPay.AspNetCore.csproj b/ICanPay.AspNetCore/ICanPay.AspNetCore.csproj new file mode 100644 index 0000000..53b425f --- /dev/null +++ b/ICanPay.AspNetCore/ICanPay.AspNetCore.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/ICanPay.sln b/ICanPay.sln index 31f6ae8..7a003d8 100644 --- a/ICanPay.sln +++ b/ICanPay.sln @@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICanPay.Demo(Net)", "sample EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICanPay.Core.Mvc", "src\ICanPay.Core.Mvc\ICanPay.Core.Mvc.csproj", "{2E3EBDA4-F467-4A50-AA12-1DAD40450A97}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICanPay.AspNetCore", "ICanPay.AspNetCore\ICanPay.AspNetCore.csproj", "{9675DF28-FC50-49BD-BFDF-C200DE9873B9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {2E3EBDA4-F467-4A50-AA12-1DAD40450A97}.Debug|Any CPU.Build.0 = Debug|Any CPU {2E3EBDA4-F467-4A50-AA12-1DAD40450A97}.Release|Any CPU.ActiveCfg = Release|Any CPU {2E3EBDA4-F467-4A50-AA12-1DAD40450A97}.Release|Any CPU.Build.0 = Release|Any CPU + {9675DF28-FC50-49BD-BFDF-C200DE9873B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9675DF28-FC50-49BD-BFDF-C200DE9873B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9675DF28-FC50-49BD-BFDF-C200DE9873B9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9675DF28-FC50-49BD-BFDF-C200DE9873B9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ICanPay.Core/Abstractions/IKeyValueProvider.cs b/src/ICanPay.Core/Abstractions/IKeyValueProvider.cs new file mode 100644 index 0000000..ab6deb7 --- /dev/null +++ b/src/ICanPay.Core/Abstractions/IKeyValueProvider.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ICanPay.Abstractions +{ + /// + /// 键值提供器,用于从 HttpContext.Request 中提取数据。 + /// + /// 因为 HttpContext 在不同的框架中类不同,所以该接口用于协调这些专用类 + public interface IKeyValueProvider + { + /// + /// 获取 HttpContext.Request.Body 数据 + /// + /// Body的所有的字节 + byte[] Get(); + + /// + /// 获取所有的键 + /// + /// 该值指示从哪个部分中提取 比如:QueryString 或 Form , + /// 注意:该值为null时从所有的部分获取,而且应该忽略大小写 + /// + IEnumerable GetKeys(string part = null); + + /// + /// 通过提供键,获取值 + /// + /// 键 + /// 该值指示从哪个部分中提取 比如:QueryString 或 Form , + /// 注意:该值为null时从所有的部分获取,而且应该忽略大小写 + /// + string GetValue(string key, string part = null); + + IEnumerable SupportedParts { get; } + } +} diff --git a/src/ICanPay.Core/Abstractions/NotifyHub.cs b/src/ICanPay.Core/Abstractions/NotifyHub.cs index 3fc8dfe..608bf00 100644 --- a/src/ICanPay.Core/Abstractions/NotifyHub.cs +++ b/src/ICanPay.Core/Abstractions/NotifyHub.cs @@ -9,33 +9,33 @@ namespace ICanPay.Abstractions public sealed class NotifyHub { private readonly INotifyHubHandler _hubHandler; - private Func _successHandler; - private Func _failHandler; - private Func _beforeProcessHandler; - private Func _afterProcessHandler; + private Func _successHandler; + private Func _failHandler; + private Func _beforeProcessHandler; + private Func _afterProcessHandler; private Func _exceptionHandler; public NotifyHub(INotifyHubHandler hubHandler) { _hubHandler = hubHandler; } - public NotifyHub WhenSuccess(Func deleget) + public NotifyHub WhenSuccess(Func deleget) { _successHandler = deleget; return this; } - public NotifyHub WhenFail(Func deleget) + public NotifyHub WhenFail(Func deleget) { _failHandler = deleget; return this; } - public NotifyHub WhenBeforeProcess(Func deleget) + public NotifyHub WhenBeforeProcess(Func deleget) { _beforeProcessHandler = deleget; return this; } - public NotifyHub WhenAfterProcess(Func deleget) + public NotifyHub WhenAfterProcess(Func deleget) { _afterProcessHandler = deleget; return this; @@ -48,7 +48,7 @@ public NotifyHub WhenException(Func deleget) } //[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] - public async Task ProcessAsync(object data) + public async Task ProcessAsync(HubOrder data) { // 首先检查, 成功和异常处理必须注册 diff --git a/test/ICanPay.UnitTest/CodeWrite.cs b/test/ICanPay.UnitTest/CodeWrite.cs new file mode 100644 index 0000000..b6d972b --- /dev/null +++ b/test/ICanPay.UnitTest/CodeWrite.cs @@ -0,0 +1,19 @@ +using ICanPay.Abstractions; +using ICanPay.AspNetCore; +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICanPay.UnitTest +{ + class CodeWrite + { + public void Write() + { + var provider = new HttpContextKeyValueProvider(new HttpContextAccessor()); + var hub = new NotifyHub(null); + hub. + } + } +} diff --git a/test/ICanPay.UnitTest/ICanPay.UnitTest.csproj b/test/ICanPay.UnitTest/ICanPay.UnitTest.csproj index 6a289b7..40f094a 100644 --- a/test/ICanPay.UnitTest/ICanPay.UnitTest.csproj +++ b/test/ICanPay.UnitTest/ICanPay.UnitTest.csproj @@ -13,6 +13,7 @@ + From 44a7079195653d6f52a8d9c277202e2a0a6b8a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=87=91=E7=94=9F?= Date: Wed, 18 Apr 2018 14:49:59 +0800 Subject: [PATCH 04/14] ServiceCollectionExtensions --- ICanPay.AspNetCore/DI/GatewayBuilder.cs | 19 +++++++++++ ICanPay.AspNetCore/DI/IGatewayBuilder.cs | 13 +++++++ ICanPay.AspNetCore/DI/IGatewayProvider.cs | 12 +++++++ .../DI/ServiceCollectionExtensions.cs | 34 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 ICanPay.AspNetCore/DI/GatewayBuilder.cs create mode 100644 ICanPay.AspNetCore/DI/IGatewayBuilder.cs create mode 100644 ICanPay.AspNetCore/DI/IGatewayProvider.cs create mode 100644 ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs diff --git a/ICanPay.AspNetCore/DI/GatewayBuilder.cs b/ICanPay.AspNetCore/DI/GatewayBuilder.cs new file mode 100644 index 0000000..f6db047 --- /dev/null +++ b/ICanPay.AspNetCore/DI/GatewayBuilder.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICanPay.AspNetCore.DI +{ + public class GatewayBuilder : IGatewayBuilder + { + public void Add(object gateway) + { + throw new NotImplementedException(); + } + + public IGatewayProvider Build() + { + throw new NotImplementedException(); + } + } +} diff --git a/ICanPay.AspNetCore/DI/IGatewayBuilder.cs b/ICanPay.AspNetCore/DI/IGatewayBuilder.cs new file mode 100644 index 0000000..940a550 --- /dev/null +++ b/ICanPay.AspNetCore/DI/IGatewayBuilder.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICanPay.AspNetCore.DI +{ + public interface IGatewayBuilder + { + void Add(object gateway); + + IGatewayProvider Build(); + } +} diff --git a/ICanPay.AspNetCore/DI/IGatewayProvider.cs b/ICanPay.AspNetCore/DI/IGatewayProvider.cs new file mode 100644 index 0000000..30ea770 --- /dev/null +++ b/ICanPay.AspNetCore/DI/IGatewayProvider.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICanPay.AspNetCore.DI +{ + public interface IGatewayProvider + { + T GetGateway(); + T GetGateway(string gatewayName); + } +} diff --git a/ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs b/ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..52c87f8 --- /dev/null +++ b/ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs @@ -0,0 +1,34 @@ +using ICanPay.AspNetCore.DI; +using Microsoft.Extensions.DependencyInjection.Extensions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddICanPay(this IServiceCollection services) + { + services = services ?? throw new ArgumentNullException(nameof(services)); + services.AddICanPay(null); + return services; + } + + public static IServiceCollection AddICanPay(this IServiceCollection services,Action buildAction) + { + services.TryAddSingleton(); + services.TryAddScoped(svcs => + { + var builder = svcs.GetRequiredService(); + if (builder != null) + { + buildAction(builder); + } + return builder.Build(); + }); + + return services; + } + } +} From 221ccdb81ce96a60cd6a25b0166166fb12ddd381 Mon Sep 17 00:00:00 2001 From: John0King Date: Sat, 28 Apr 2018 22:50:29 +0800 Subject: [PATCH 05/14] sync project --- PaySharp.sln | 6 ++++++ .../PaySharp.AspNetCore}/DI/GatewayBuilder.cs | 2 +- .../PaySharp.AspNetCore}/DI/IGatewayBuilder.cs | 2 +- .../PaySharp.AspNetCore}/DI/IGatewayProvider.cs | 2 +- .../PaySharp.AspNetCore}/DI/ServiceCollectionExtensions.cs | 2 +- .../PaySharp.AspNetCore}/HttpContextKeyValueProvider.cs | 4 ++-- .../PaySharp.AspNetCore/PaySharp.AspNetCore.csproj | 0 .../Abstractions/HubOrder.cs | 2 +- .../Abstractions/IKeyValueProvider.cs | 2 +- .../Abstractions/INotifyDataConverter.cs | 2 +- .../Abstractions/INotifyHubHandler.cs | 2 +- .../Abstractions/INotifyResponse.cs | 2 +- .../Abstractions/IPaymentNotifyData.cs | 2 +- .../Abstractions/NotifyHub.cs | 2 +- .../Abstractions/ProcessResult.cs | 2 +- 15 files changed, 20 insertions(+), 14 deletions(-) rename {ICanPay.AspNetCore => src/PaySharp.AspNetCore}/DI/GatewayBuilder.cs (91%) rename {ICanPay.AspNetCore => src/PaySharp.AspNetCore}/DI/IGatewayBuilder.cs (85%) rename {ICanPay.AspNetCore => src/PaySharp.AspNetCore}/DI/IGatewayProvider.cs (85%) rename {ICanPay.AspNetCore => src/PaySharp.AspNetCore}/DI/ServiceCollectionExtensions.cs (96%) rename {ICanPay.AspNetCore => src/PaySharp.AspNetCore}/HttpContextKeyValueProvider.cs (97%) rename ICanPay.AspNetCore/ICanPay.AspNetCore.csproj => src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj (100%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/HubOrder.cs (97%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/IKeyValueProvider.cs (97%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/INotifyDataConverter.cs (94%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/INotifyHubHandler.cs (91%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/INotifyResponse.cs (93%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/IPaymentNotifyData.cs (84%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/NotifyHub.cs (98%) rename src/{ICanPay.Core => PaySharp.Core}/Abstractions/ProcessResult.cs (95%) diff --git a/PaySharp.sln b/PaySharp.sln index d57d5ac..cbbd5cb 100644 --- a/PaySharp.sln +++ b/PaySharp.sln @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.UnitTest", "test\P EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Demo", "sample\PaySharp.Demo\PaySharp.Demo.csproj", "{8A0208C0-7046-44A7-A67F-DA122AB280A3}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.AspNetCore", "src\PaySharp.AspNetCore\PaySharp.AspNetCore.csproj", "{E649A330-1B5C-487B-99F7-208248161B8F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Debug|Any CPU.Build.0 = Debug|Any CPU {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Release|Any CPU.ActiveCfg = Release|Any CPU {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Release|Any CPU.Build.0 = Release|Any CPU + {E649A330-1B5C-487B-99F7-208248161B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E649A330-1B5C-487B-99F7-208248161B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ICanPay.AspNetCore/DI/GatewayBuilder.cs b/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs similarity index 91% rename from ICanPay.AspNetCore/DI/GatewayBuilder.cs rename to src/PaySharp.AspNetCore/DI/GatewayBuilder.cs index f6db047..25fb6f2 100644 --- a/ICanPay.AspNetCore/DI/GatewayBuilder.cs +++ b/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace ICanPay.AspNetCore.DI +namespace PaySharp.AspNetCore.DI { public class GatewayBuilder : IGatewayBuilder { diff --git a/ICanPay.AspNetCore/DI/IGatewayBuilder.cs b/src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs similarity index 85% rename from ICanPay.AspNetCore/DI/IGatewayBuilder.cs rename to src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs index 940a550..d850039 100644 --- a/ICanPay.AspNetCore/DI/IGatewayBuilder.cs +++ b/src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace ICanPay.AspNetCore.DI +namespace PaySharp.AspNetCore.DI { public interface IGatewayBuilder { diff --git a/ICanPay.AspNetCore/DI/IGatewayProvider.cs b/src/PaySharp.AspNetCore/DI/IGatewayProvider.cs similarity index 85% rename from ICanPay.AspNetCore/DI/IGatewayProvider.cs rename to src/PaySharp.AspNetCore/DI/IGatewayProvider.cs index 30ea770..3112c6f 100644 --- a/ICanPay.AspNetCore/DI/IGatewayProvider.cs +++ b/src/PaySharp.AspNetCore/DI/IGatewayProvider.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace ICanPay.AspNetCore.DI +namespace PaySharp.AspNetCore.DI { public interface IGatewayProvider { diff --git a/ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs b/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs similarity index 96% rename from ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs rename to src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs index 52c87f8..d5bcf6a 100644 --- a/ICanPay.AspNetCore/DI/ServiceCollectionExtensions.cs +++ b/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using ICanPay.AspNetCore.DI; +using PaySharp.AspNetCore.DI; using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Collections.Generic; diff --git a/ICanPay.AspNetCore/HttpContextKeyValueProvider.cs b/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs similarity index 97% rename from ICanPay.AspNetCore/HttpContextKeyValueProvider.cs rename to src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs index 3ccdbcf..a9dd764 100644 --- a/ICanPay.AspNetCore/HttpContextKeyValueProvider.cs +++ b/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs @@ -1,11 +1,11 @@ -using ICanPay.Abstractions; +using PaySharp.Abstractions; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.IO; using System.Linq; -namespace ICanPay.AspNetCore +namespace PaySharp.AspNetCore { /// /// Asp.Net-Core 的键值提供器 diff --git a/ICanPay.AspNetCore/ICanPay.AspNetCore.csproj b/src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj similarity index 100% rename from ICanPay.AspNetCore/ICanPay.AspNetCore.csproj rename to src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj diff --git a/src/ICanPay.Core/Abstractions/HubOrder.cs b/src/PaySharp.Core/Abstractions/HubOrder.cs similarity index 97% rename from src/ICanPay.Core/Abstractions/HubOrder.cs rename to src/PaySharp.Core/Abstractions/HubOrder.cs index c75d0af..7b95485 100644 --- a/src/ICanPay.Core/Abstractions/HubOrder.cs +++ b/src/PaySharp.Core/Abstractions/HubOrder.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { /// /// 通用的处理数据 diff --git a/src/ICanPay.Core/Abstractions/IKeyValueProvider.cs b/src/PaySharp.Core/Abstractions/IKeyValueProvider.cs similarity index 97% rename from src/ICanPay.Core/Abstractions/IKeyValueProvider.cs rename to src/PaySharp.Core/Abstractions/IKeyValueProvider.cs index ab6deb7..a69f6e8 100644 --- a/src/ICanPay.Core/Abstractions/IKeyValueProvider.cs +++ b/src/PaySharp.Core/Abstractions/IKeyValueProvider.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { /// /// 键值提供器,用于从 HttpContext.Request 中提取数据。 diff --git a/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs b/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs similarity index 94% rename from src/ICanPay.Core/Abstractions/INotifyDataConverter.cs rename to src/PaySharp.Core/Abstractions/INotifyDataConverter.cs index 17c90f2..22df1ee 100644 --- a/src/ICanPay.Core/Abstractions/INotifyDataConverter.cs +++ b/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { /// /// 通知数据转换器, 他从HttpContext 转换为强类型数据 diff --git a/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs similarity index 91% rename from src/ICanPay.Core/Abstractions/INotifyHubHandler.cs rename to src/PaySharp.Core/Abstractions/INotifyHubHandler.cs index b9bb6fe..0e65e24 100644 --- a/src/ICanPay.Core/Abstractions/INotifyHubHandler.cs +++ b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { /// /// NotifyHub 处理器 diff --git a/src/ICanPay.Core/Abstractions/INotifyResponse.cs b/src/PaySharp.Core/Abstractions/INotifyResponse.cs similarity index 93% rename from src/ICanPay.Core/Abstractions/INotifyResponse.cs rename to src/PaySharp.Core/Abstractions/INotifyResponse.cs index 8f0130c..57a85d8 100644 --- a/src/ICanPay.Core/Abstractions/INotifyResponse.cs +++ b/src/PaySharp.Core/Abstractions/INotifyResponse.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { public interface INotifyResponse { diff --git a/src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs b/src/PaySharp.Core/Abstractions/IPaymentNotifyData.cs similarity index 84% rename from src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs rename to src/PaySharp.Core/Abstractions/IPaymentNotifyData.cs index e79b8f9..3c76b9f 100644 --- a/src/ICanPay.Core/Abstractions/IPaymentNotifyData.cs +++ b/src/PaySharp.Core/Abstractions/IPaymentNotifyData.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { public interface IPaymentNotifyData { diff --git a/src/ICanPay.Core/Abstractions/NotifyHub.cs b/src/PaySharp.Core/Abstractions/NotifyHub.cs similarity index 98% rename from src/ICanPay.Core/Abstractions/NotifyHub.cs rename to src/PaySharp.Core/Abstractions/NotifyHub.cs index 608bf00..c549b7f 100644 --- a/src/ICanPay.Core/Abstractions/NotifyHub.cs +++ b/src/PaySharp.Core/Abstractions/NotifyHub.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { public sealed class NotifyHub { diff --git a/src/ICanPay.Core/Abstractions/ProcessResult.cs b/src/PaySharp.Core/Abstractions/ProcessResult.cs similarity index 95% rename from src/ICanPay.Core/Abstractions/ProcessResult.cs rename to src/PaySharp.Core/Abstractions/ProcessResult.cs index d985145..56b0bcb 100644 --- a/src/ICanPay.Core/Abstractions/ProcessResult.cs +++ b/src/PaySharp.Core/Abstractions/ProcessResult.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace ICanPay.Abstractions +namespace PaySharp.Abstractions { public class ProcessResult { From 9b52f8875669e23947a71afa04e6f6de398f6019 Mon Sep 17 00:00:00 2001 From: John0King Date: Sun, 29 Apr 2018 18:24:31 +0800 Subject: [PATCH 06/14] abstractions --- src/PaySharp.AspNetCore/DI/GatewayBuilder.cs | 44 +++++++++++++++++-- src/PaySharp.AspNetCore/DI/GatewayProvider.cs | 41 +++++++++++++++++ src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs | 13 ------ .../DI/IGatewayProvider.cs | 12 ----- .../DI/ServiceCollectionExtensions.cs | 5 +++ .../HttpContextKeyValueProvider.cs | 5 +++ .../PaySharp.AspNetCore.csproj | 2 +- .../Abstractions/IGatewayBuilder.cs | 27 ++++++++++++ .../Abstractions/IGatewayProvider.cs | 12 +++++ .../Abstractions/INotifyDataConverter.cs | 2 + .../Abstractions/INotifyHubHandler.cs | 1 + 11 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 src/PaySharp.AspNetCore/DI/GatewayProvider.cs delete mode 100644 src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs delete mode 100644 src/PaySharp.AspNetCore/DI/IGatewayProvider.cs create mode 100644 src/PaySharp.Core/Abstractions/IGatewayBuilder.cs create mode 100644 src/PaySharp.Core/Abstractions/IGatewayProvider.cs diff --git a/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs b/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs index 25fb6f2..ef8a10a 100644 --- a/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs +++ b/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs @@ -1,19 +1,57 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Text; +using PaySharp.Abstractions; namespace PaySharp.AspNetCore.DI { + /// public class GatewayBuilder : IGatewayBuilder { - public void Add(object gateway) + private readonly IDictionary> _store = new Dictionary>(); + + /// + public bool TryAdd(string name, T gateway) where T : class { - throw new NotImplementedException(); + try + { + var t = typeof(T); + if (_store.ContainsKey(t)) + { + var innerStore = _store[t]; + innerStore[name] = gateway; + return true; + } + else + { + var innerStore = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + [name] = gateway + }; + _store.Add(t, innerStore); + return true; + } + } + catch + { + return false; + } + + } + /// public IGatewayProvider Build() { - throw new NotImplementedException(); + return new GatewayProvider(_store); + } + + + private class GatewayStore + { + public string Name { get; set; } + public object Gateway { get; set; } } } } diff --git a/src/PaySharp.AspNetCore/DI/GatewayProvider.cs b/src/PaySharp.AspNetCore/DI/GatewayProvider.cs new file mode 100644 index 0000000..c00a671 --- /dev/null +++ b/src/PaySharp.AspNetCore/DI/GatewayProvider.cs @@ -0,0 +1,41 @@ +using PaySharp.Abstractions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.AspNetCore.DI +{ + /// + public class GatewayProvider : IGatewayProvider + { + private readonly IDictionary> _store; + + internal GatewayProvider(IDictionary> store) + { + _store = store; + } + + /// + public T GetGateway() where T:class + { + return GetGateway(string.Empty); + } + + /// + public T GetGateway(string gatewayName) where T:class + { + gatewayName = gatewayName ?? throw new ArgumentNullException(nameof(gatewayName), "Gateway name must not be null"); + if (_store.TryGetValue(typeof(T), out var innerStore)) + { + if (innerStore.TryGetValue(gatewayName, out var gateway)) + { + if(gateway is T) + { + return (T)gateway; + } + } + } + return null; + } + } +} diff --git a/src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs b/src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs deleted file mode 100644 index d850039..0000000 --- a/src/PaySharp.AspNetCore/DI/IGatewayBuilder.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace PaySharp.AspNetCore.DI -{ - public interface IGatewayBuilder - { - void Add(object gateway); - - IGatewayProvider Build(); - } -} diff --git a/src/PaySharp.AspNetCore/DI/IGatewayProvider.cs b/src/PaySharp.AspNetCore/DI/IGatewayProvider.cs deleted file mode 100644 index 3112c6f..0000000 --- a/src/PaySharp.AspNetCore/DI/IGatewayProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace PaySharp.AspNetCore.DI -{ - public interface IGatewayProvider - { - T GetGateway(); - T GetGateway(string gatewayName); - } -} diff --git a/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs b/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs index d5bcf6a..d7c56b1 100644 --- a/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs +++ b/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs @@ -3,11 +3,15 @@ using System; using System.Collections.Generic; using System.Text; +using PaySharp.Abstractions; +using System.ComponentModel; +using PaySharp.AspNetCore; namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { + [EditorBrowsable(EditorBrowsableState.Never)]//先不要让其他人访问 public static IServiceCollection AddICanPay(this IServiceCollection services) { services = services ?? throw new ArgumentNullException(nameof(services)); @@ -18,6 +22,7 @@ public static IServiceCollection AddICanPay(this IServiceCollection services) public static IServiceCollection AddICanPay(this IServiceCollection services,Action buildAction) { services.TryAddSingleton(); + services.TryAddScoped(); services.TryAddScoped(svcs => { var builder = svcs.GetRequiredService(); diff --git a/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs b/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs index a9dd764..6283e35 100644 --- a/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs +++ b/src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs @@ -23,6 +23,11 @@ public HttpContextKeyValueProvider(IHttpContextAccessor httpContextAccessor) HttpContext = httpContextAccessor.HttpContext; } + public HttpContextKeyValueProvider(HttpContext httpContext) + { + HttpContext = httpContext; + } + /// public byte[] Get() { diff --git a/src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj b/src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj index 53b425f..c24aa26 100644 --- a/src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj +++ b/src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs b/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs new file mode 100644 index 0000000..fa55285 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.Abstractions +{ + /// + /// 网关提供器的构造器 + /// + public interface IGatewayBuilder + { + /// + /// 添加一个命名的网关 + /// + /// 网关的类型 + /// 网关的名字 + /// 网关 + /// + bool TryAdd(string name,T gateway) where T:class; + + /// + /// 返回一个 的实例 + /// + /// + IGatewayProvider Build(); + } +} diff --git a/src/PaySharp.Core/Abstractions/IGatewayProvider.cs b/src/PaySharp.Core/Abstractions/IGatewayProvider.cs new file mode 100644 index 0000000..21cd8c0 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/IGatewayProvider.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.Abstractions +{ + public interface IGatewayProvider + { + T GetGateway() where T : class; + T GetGateway(string gatewayName) where T : class; + } +} diff --git a/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs b/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs index 22df1ee..a425ffa 100644 --- a/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs +++ b/src/PaySharp.Core/Abstractions/INotifyDataConverter.cs @@ -10,11 +10,13 @@ namespace PaySharp.Abstractions /// 通知数据转换器, 他从HttpContext 转换为强类型数据 /// /// HttpContext在实现的类中采用构造函数注入, + [Obsolete("似乎没什么用",true)] public interface INotifyDataConverter { Task ConvertDataAsync(); } + [Obsolete("似乎没什么用", true)] public interface INotifyDataConverter : INotifyDataConverter { new Task ConvertDataAsync(); diff --git a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs index 0e65e24..b3b7f6c 100644 --- a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs +++ b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs @@ -11,6 +11,7 @@ namespace PaySharp.Abstractions /// public interface INotifyHubHandler { + [Obsolete("似乎没什么用", true)] INotifyDataConverter Converter { get; } ProcessResult Process(IPaymentNotifyData notifyData); From 4241fb6afff538755b2eb9985ccdd3b068cf7410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=87=91=E7=94=9F?= Date: Thu, 3 May 2018 15:06:45 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=20Asp.Net=20?= =?UTF-8?q?=E7=9A=84=20Target?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PaySharp.sln | 6 ++ .../HttpContextKeyValueProvider.cs | 82 +++++++++++++++++++ src/PaySharp.AspNet/PaySharp.AspNet.csproj | 15 ++++ .../{DI => }/ServiceCollectionExtensions.cs | 17 +++- .../Internal}/GatewayBuilder.cs | 2 +- .../Internal}/GatewayProvider.cs | 2 +- .../PaySharp.UnitTest.csproj | 1 - 7 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 src/PaySharp.AspNet/HttpContextKeyValueProvider.cs create mode 100644 src/PaySharp.AspNet/PaySharp.AspNet.csproj rename src/PaySharp.AspNetCore/{DI => }/ServiceCollectionExtensions.cs (64%) rename src/{PaySharp.AspNetCore/DI => PaySharp.Core/Internal}/GatewayBuilder.cs (97%) rename src/{PaySharp.AspNetCore/DI => PaySharp.Core/Internal}/GatewayProvider.cs (97%) diff --git a/PaySharp.sln b/PaySharp.sln index cbbd5cb..a52f9f1 100644 --- a/PaySharp.sln +++ b/PaySharp.sln @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Demo", "sample\Pay EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.AspNetCore", "src\PaySharp.AspNetCore\PaySharp.AspNetCore.csproj", "{E649A330-1B5C-487B-99F7-208248161B8F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaySharp.AspNet", "src\PaySharp.AspNet\PaySharp.AspNet.csproj", "{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,6 +59,10 @@ Global {E649A330-1B5C-487B-99F7-208248161B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU {E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.Build.0 = Release|Any CPU + {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/PaySharp.AspNet/HttpContextKeyValueProvider.cs b/src/PaySharp.AspNet/HttpContextKeyValueProvider.cs new file mode 100644 index 0000000..7478d9c --- /dev/null +++ b/src/PaySharp.AspNet/HttpContextKeyValueProvider.cs @@ -0,0 +1,82 @@ +using PaySharp.Abstractions; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Web; + +namespace PaySharp.AspNetCore +{ + /// + /// Asp.Net 的键值提供器 + /// + public class HttpContextKeyValueProvider : IKeyValueProvider + { + public HttpContextBase HttpContext { get; } + + public IEnumerable SupportedParts => _supportedParts; + + private IEnumerable _supportedParts = new[] { "QueryString", "Form" }; + + public HttpContextKeyValueProvider(HttpContextBase httpContext) + { + HttpContext = HttpContext; + } + + /// + public byte[] Get() + { + using (var ms = new MemoryStream()) + { + HttpContext.Request.GetBufferedInputStream().CopyTo(ms); + return ms.ToArray(); + } + + } + + /// + public IEnumerable GetKeys(string part = null) + { + var request = HttpContext.Request; + var result = new List(); + if (NullEq(part, "QueryString")) + { + result.AddRange(request.QueryString.AllKeys); + + } + if (NullEq(part, "Form")) + { + result.AddRange(request.Form.AllKeys); + } + + return result.Distinct(); + } + + /// + public string GetValue(string key, string part = null) + { + var request = HttpContext.Request; + if (NullEq(part, "QueryString")) + { + return request.QueryString[key]; + } + + if (NullEq(part, "Form")) + { + return request.Form[key]; + } + + return null; + } + + + private bool NullEq(string val, string compare) + { + return string.IsNullOrEmpty(val) || string.Equals(val, compare, StringComparison.OrdinalIgnoreCase); + } + private bool Eq(string str1, string str2) + { + return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/src/PaySharp.AspNet/PaySharp.AspNet.csproj b/src/PaySharp.AspNet/PaySharp.AspNet.csproj new file mode 100644 index 0000000..c78cbcf --- /dev/null +++ b/src/PaySharp.AspNet/PaySharp.AspNet.csproj @@ -0,0 +1,15 @@ + + + + net45 + + + + + + + + + + + diff --git a/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs similarity index 64% rename from src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs rename to src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs index d7c56b1..a921da5 100644 --- a/src/PaySharp.AspNetCore/DI/ServiceCollectionExtensions.cs +++ b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using PaySharp.AspNetCore.DI; +using PaySharp.Internal; using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Collections.Generic; @@ -9,17 +9,26 @@ namespace Microsoft.Extensions.DependencyInjection { + /// + /// PaySharp 对 的扩展方法 + /// public static class ServiceCollectionExtensions { [EditorBrowsable(EditorBrowsableState.Never)]//先不要让其他人访问 - public static IServiceCollection AddICanPay(this IServiceCollection services) + public static IServiceCollection AddPaySharp(this IServiceCollection services) { services = services ?? throw new ArgumentNullException(nameof(services)); - services.AddICanPay(null); + AddPaySharp(services, null); return services; } - public static IServiceCollection AddICanPay(this IServiceCollection services,Action buildAction) + /// + /// 添加PaySharp相关服务 + /// + /// + /// + /// + public static IServiceCollection AddPaySharp(this IServiceCollection services,Action buildAction) { services.TryAddSingleton(); services.TryAddScoped(); diff --git a/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs b/src/PaySharp.Core/Internal/GatewayBuilder.cs similarity index 97% rename from src/PaySharp.AspNetCore/DI/GatewayBuilder.cs rename to src/PaySharp.Core/Internal/GatewayBuilder.cs index ef8a10a..a858941 100644 --- a/src/PaySharp.AspNetCore/DI/GatewayBuilder.cs +++ b/src/PaySharp.Core/Internal/GatewayBuilder.cs @@ -4,7 +4,7 @@ using System.Text; using PaySharp.Abstractions; -namespace PaySharp.AspNetCore.DI +namespace PaySharp.Internal { /// public class GatewayBuilder : IGatewayBuilder diff --git a/src/PaySharp.AspNetCore/DI/GatewayProvider.cs b/src/PaySharp.Core/Internal/GatewayProvider.cs similarity index 97% rename from src/PaySharp.AspNetCore/DI/GatewayProvider.cs rename to src/PaySharp.Core/Internal/GatewayProvider.cs index c00a671..efe5243 100644 --- a/src/PaySharp.AspNetCore/DI/GatewayProvider.cs +++ b/src/PaySharp.Core/Internal/GatewayProvider.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Text; -namespace PaySharp.AspNetCore.DI +namespace PaySharp.Internal { /// public class GatewayProvider : IGatewayProvider diff --git a/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj b/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj index cf6c887..a37520c 100644 --- a/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj +++ b/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj @@ -21,7 +21,6 @@ - From a4cbc73d335a9b1d7c9817da4af7c8f5969f8e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=87=91=E7=94=9F?= Date: Thu, 3 May 2018 15:29:02 +0800 Subject: [PATCH 08/14] [wip] notifyHub --- src/PaySharp.Core/Abstractions/HubOrder.cs | 12 ++--- .../Abstractions/INotifyHubHandler.cs | 2 +- .../Abstractions/IUserHandler.cs | 26 +++++++++++ src/PaySharp.Core/Abstractions/NotifyHub.cs | 45 ++++++++++++++----- 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 src/PaySharp.Core/Abstractions/IUserHandler.cs diff --git a/src/PaySharp.Core/Abstractions/HubOrder.cs b/src/PaySharp.Core/Abstractions/HubOrder.cs index 7b95485..444c6b6 100644 --- a/src/PaySharp.Core/Abstractions/HubOrder.cs +++ b/src/PaySharp.Core/Abstractions/HubOrder.cs @@ -14,33 +14,33 @@ public class HubOrder /// /// 支付机构订单Id /// - public string GatewayOrderId { get; set; } + public virtual string GatewayOrderId { get; set; } /// /// 业务逻辑的订单Id /// - public string BusinessOrderId { get; set; } + public virtual string BusinessOrderId { get; set; } /// /// 支付机构此次处理的时间 /// - public DateTimeOffset ProcessTime { get; set; } + public virtual DateTimeOffset ProcessTime { get; set; } /// /// 支付处理器给的类型 /// /// 可以利用此值决定 的类型 - public string GatewayType { get; set; } + public virtual string GatewayType { get; set; } /// /// 原本的数据 /// - public object RawData { get; set; } + public virtual object RawData { get; set; } /// /// 状态, 当为 null 时,为未知 /// - public HubHandlerDataStatus? Status { get; set; } + public virtual HubHandlerDataStatus? Status { get; set; } } /// diff --git a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs index b3b7f6c..8df5c12 100644 --- a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs +++ b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs @@ -13,7 +13,7 @@ public interface INotifyHubHandler { [Obsolete("似乎没什么用", true)] INotifyDataConverter Converter { get; } - ProcessResult Process(IPaymentNotifyData notifyData); + Task ProcessAsync(IKeyValueProvider valueProvider); } diff --git a/src/PaySharp.Core/Abstractions/IUserHandler.cs b/src/PaySharp.Core/Abstractions/IUserHandler.cs new file mode 100644 index 0000000..894b451 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/IUserHandler.cs @@ -0,0 +1,26 @@ +using PaySharp.Abstractions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Abstractions +{ + /// + /// 用户逻辑处理器 + /// + public interface IUserHandler + { + + Task OnProcessingAsync(IKeyValueProvider keyValueProvider); + + Task OnProcessedAsync(HubOrder hubOrder, IKeyValueProvider valueProvider); + + Task OnSuccessedAsync(HubOrder hubOrder, IKeyValueProvider provider); + + Task OnFailedAsync(HubOrder hubOrder, IKeyValueProvider provider); + + Task OnExceptionAsync(Exception exception, IKeyValueProvider provider); + } +} diff --git a/src/PaySharp.Core/Abstractions/NotifyHub.cs b/src/PaySharp.Core/Abstractions/NotifyHub.cs index c549b7f..653f163 100644 --- a/src/PaySharp.Core/Abstractions/NotifyHub.cs +++ b/src/PaySharp.Core/Abstractions/NotifyHub.cs @@ -6,6 +6,10 @@ namespace PaySharp.Abstractions { + + /// + /// 通知适配器 + /// public sealed class NotifyHub { private readonly INotifyHubHandler _hubHandler; @@ -14,11 +18,31 @@ public sealed class NotifyHub private Func _beforeProcessHandler; private Func _afterProcessHandler; private Func _exceptionHandler; + + /// + /// 通过一个 来实例化 该适配器 + /// + /// 要用来处理的通知处理器 public NotifyHub(INotifyHubHandler hubHandler) { _hubHandler = hubHandler; } + /// + /// 一次性添加整个处理流程 + /// + /// 一个拥有所有处理流程的类的实例 + /// 返回配置的 + public NotifyHub UseUserHandler(IUserHandler handler) + { + _beforeProcessHandler = handler.OnProcessingAsync; + _successHandler = handler.OnSuccessedAsync; + _failHandler = handler.OnFailedAsync; + _exceptionHandler = handler.OnExceptionAsync; + _afterProcessHandler = handler.OnProcessedAsync; + return this; + } + public NotifyHub WhenSuccess(Func deleget) { _successHandler = deleget; @@ -48,7 +72,7 @@ public NotifyHub WhenException(Func deleget) } //[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] - public async Task ProcessAsync(HubOrder data) + public async Task ProcessAsync(IKeyValueProvider valueProvider) { // 首先检查, 成功和异常处理必须注册 @@ -64,35 +88,34 @@ public async Task ProcessAsync(HubOrder data) // before if(_beforeProcessHandler != null) { - await _beforeProcessHandler(null); + await _beforeProcessHandler(valueProvider); } try { - var result = _hubHandler.Process(null); + var result = await _hubHandler.ProcessAsync(valueProvider); if (result.IsSuccess) { - await _successHandler(null); + await _successHandler(result.Data); } else { if (_failHandler != null) { - await _failHandler(null); + await _failHandler(result.Data); } } + + if (_afterProcessHandler != null) + { + await _afterProcessHandler(result.Data); + } } catch(Exception e) { await _exceptionHandler(e); } - if(_afterProcessHandler != null) - { - await _afterProcessHandler(null); - } - - } } } From e57f639f6ba110059701d70a5863d31a8f3e3e2d Mon Sep 17 00:00:00 2001 From: John0King Date: Thu, 3 May 2018 17:52:35 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20DI=20=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8CSingleton=20=E7=9A=84=20=20IGatewayB?= =?UTF-8?q?uilder=20=20=E9=85=8D=E5=90=88=20Scoped=20IGatewayProvider=20?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4=E9=87=8D=E5=A4=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PaySharp.sln | 24 +++-- .../ServiceCollectionExtensions.cs | 2 +- src/PaySharp.Core/Abstractions/NotifyHub.cs | 46 ++------ src/PaySharp.Core/Internal/GatewayProvider.cs | 4 +- test/ICanPay.UnitTest/CodeWrite.cs | 19 ---- .../DI/DIExtensionTest.cs | 100 ++++++++++++++++++ .../PaySharp.AspNetCoreTest.csproj | 22 ++++ 7 files changed, 152 insertions(+), 65 deletions(-) delete mode 100644 test/ICanPay.UnitTest/CodeWrite.cs create mode 100644 test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs create mode 100644 test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj diff --git a/PaySharp.sln b/PaySharp.sln index a52f9f1..85ae512 100644 --- a/PaySharp.sln +++ b/PaySharp.sln @@ -13,14 +13,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Core.Mvc", "src\Pa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Wechatpay", "src\PaySharp.Wechatpay\PaySharp.Wechatpay.csproj", "{DC8772E4-9F01-4744-93A3-0A795117418C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.UnitTest", "test\PaySharp.UnitTest\PaySharp.UnitTest.csproj", "{B9DD57BE-96FC-4F78-99E9-99CEAD33CC5F}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Demo", "sample\PaySharp.Demo\PaySharp.Demo.csproj", "{8A0208C0-7046-44A7-A67F-DA122AB280A3}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.AspNetCore", "src\PaySharp.AspNetCore\PaySharp.AspNetCore.csproj", "{E649A330-1B5C-487B-99F7-208248161B8F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaySharp.AspNet", "src\PaySharp.AspNet\PaySharp.AspNet.csproj", "{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{9B98CFC6-581C-4006-ADB5-A757694915EF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.UnitTest", "test\PaySharp.UnitTest\PaySharp.UnitTest.csproj", "{909EE2D9-5B9E-444B-91DF-A8E35F4D1076}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaySharp.AspNetCoreTest", "test\PaySharp.AspNetCoreTest\PaySharp.AspNetCoreTest.csproj", "{7695B8A5-2737-444A-AC7B-09E43B09B9D9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -47,10 +51,6 @@ Global {DC8772E4-9F01-4744-93A3-0A795117418C}.Debug|Any CPU.Build.0 = Debug|Any CPU {DC8772E4-9F01-4744-93A3-0A795117418C}.Release|Any CPU.ActiveCfg = Release|Any CPU {DC8772E4-9F01-4744-93A3-0A795117418C}.Release|Any CPU.Build.0 = Release|Any CPU - {B9DD57BE-96FC-4F78-99E9-99CEAD33CC5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B9DD57BE-96FC-4F78-99E9-99CEAD33CC5F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B9DD57BE-96FC-4F78-99E9-99CEAD33CC5F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B9DD57BE-96FC-4F78-99E9-99CEAD33CC5F}.Release|Any CPU.Build.0 = Release|Any CPU {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Debug|Any CPU.Build.0 = Debug|Any CPU {8A0208C0-7046-44A7-A67F-DA122AB280A3}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -63,10 +63,22 @@ Global {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Debug|Any CPU.Build.0 = Debug|Any CPU {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.ActiveCfg = Release|Any CPU {D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.Build.0 = Release|Any CPU + {909EE2D9-5B9E-444B-91DF-A8E35F4D1076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {909EE2D9-5B9E-444B-91DF-A8E35F4D1076}.Debug|Any CPU.Build.0 = Debug|Any CPU + {909EE2D9-5B9E-444B-91DF-A8E35F4D1076}.Release|Any CPU.ActiveCfg = Release|Any CPU + {909EE2D9-5B9E-444B-91DF-A8E35F4D1076}.Release|Any CPU.Build.0 = Release|Any CPU + {7695B8A5-2737-444A-AC7B-09E43B09B9D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7695B8A5-2737-444A-AC7B-09E43B09B9D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7695B8A5-2737-444A-AC7B-09E43B09B9D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7695B8A5-2737-444A-AC7B-09E43B09B9D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {909EE2D9-5B9E-444B-91DF-A8E35F4D1076} = {9B98CFC6-581C-4006-ADB5-A757694915EF} + {7695B8A5-2737-444A-AC7B-09E43B09B9D9} = {9B98CFC6-581C-4006-ADB5-A757694915EF} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {35DA7CB4-3AB7-45FF-93D2-DBC4760221EF} EndGlobalSection diff --git a/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs index a921da5..eae2dae 100644 --- a/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs @@ -30,7 +30,7 @@ public static IServiceCollection AddPaySharp(this IServiceCollection services) /// public static IServiceCollection AddPaySharp(this IServiceCollection services,Action buildAction) { - services.TryAddSingleton(); + services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(svcs => { diff --git a/src/PaySharp.Core/Abstractions/NotifyHub.cs b/src/PaySharp.Core/Abstractions/NotifyHub.cs index 653f163..644cde9 100644 --- a/src/PaySharp.Core/Abstractions/NotifyHub.cs +++ b/src/PaySharp.Core/Abstractions/NotifyHub.cs @@ -13,11 +13,11 @@ namespace PaySharp.Abstractions public sealed class NotifyHub { private readonly INotifyHubHandler _hubHandler; - private Func _successHandler; - private Func _failHandler; + private Func _successHandler; + private Func _failHandler; private Func _beforeProcessHandler; - private Func _afterProcessHandler; - private Func _exceptionHandler; + private Func _afterProcessHandler; + private Func _exceptionHandler; /// /// 通过一个 来实例化 该适配器 @@ -42,35 +42,7 @@ public NotifyHub UseUserHandler(IUserHandler handler) _afterProcessHandler = handler.OnProcessedAsync; return this; } - - public NotifyHub WhenSuccess(Func deleget) - { - _successHandler = deleget; - return this; - } - public NotifyHub WhenFail(Func deleget) - { - _failHandler = deleget; - return this; - } - - public NotifyHub WhenBeforeProcess(Func deleget) - { - _beforeProcessHandler = deleget; - return this; - } - public NotifyHub WhenAfterProcess(Func deleget) - { - _afterProcessHandler = deleget; - return this; - } - - public NotifyHub WhenException(Func deleget) - { - _exceptionHandler = deleget; - return this; - } - + //[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public async Task ProcessAsync(IKeyValueProvider valueProvider) { @@ -96,24 +68,24 @@ public async Task ProcessAsync(IKeyValueProvider valueProvider) var result = await _hubHandler.ProcessAsync(valueProvider); if (result.IsSuccess) { - await _successHandler(result.Data); + await _successHandler(result.Data,valueProvider); } else { if (_failHandler != null) { - await _failHandler(result.Data); + await _failHandler(result.Data,valueProvider); } } if (_afterProcessHandler != null) { - await _afterProcessHandler(result.Data); + await _afterProcessHandler(result.Data,valueProvider); } } catch(Exception e) { - await _exceptionHandler(e); + await _exceptionHandler(e,valueProvider); } } diff --git a/src/PaySharp.Core/Internal/GatewayProvider.cs b/src/PaySharp.Core/Internal/GatewayProvider.cs index efe5243..6b59aac 100644 --- a/src/PaySharp.Core/Internal/GatewayProvider.cs +++ b/src/PaySharp.Core/Internal/GatewayProvider.cs @@ -10,9 +10,9 @@ public class GatewayProvider : IGatewayProvider { private readonly IDictionary> _store; - internal GatewayProvider(IDictionary> store) + public GatewayProvider(IDictionary> store) { - _store = store; + _store = store ?? throw new ArgumentNullException(nameof(store)); } /// diff --git a/test/ICanPay.UnitTest/CodeWrite.cs b/test/ICanPay.UnitTest/CodeWrite.cs deleted file mode 100644 index b6d972b..0000000 --- a/test/ICanPay.UnitTest/CodeWrite.cs +++ /dev/null @@ -1,19 +0,0 @@ -using ICanPay.Abstractions; -using ICanPay.AspNetCore; -using Microsoft.AspNetCore.Http; -using System; -using System.Collections.Generic; -using System.Text; - -namespace ICanPay.UnitTest -{ - class CodeWrite - { - public void Write() - { - var provider = new HttpContextKeyValueProvider(new HttpContextAccessor()); - var hub = new NotifyHub(null); - hub. - } - } -} diff --git a/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs new file mode 100644 index 0000000..8166b54 --- /dev/null +++ b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs @@ -0,0 +1,100 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using PaySharp.Abstractions; +using PaySharp.Internal; +using System; +using System.Collections.Generic; +using Xunit; + +namespace PaySharp.AspNetCoreTest +{ + public class DIExtensionTest + { + [Fact] + public void ConfigDI_Repeate_Test() + { + var service = new ServiceCollection(); + service.TryAddScoped(); + service.AddPaySharp(builder => + { + builder.TryAdd("A", new XGateway()); + }); + + var provider = service.BuildServiceProvider(); + + try + { + var gateway1 = GetGateway(provider); + } + catch(Exception e) + { + throw new Exception("第一次获取就出错了", e); + } + + + try + { + var gateway2 = GetGateway(provider); + } + catch(Exception e) + { + throw new Exception("第二次获取出错了", e); + } + } + + private XGateway GetGateway(IServiceProvider servicProvider) + { + using (var provider = servicProvider.CreateScope()) + { + var gateWays = provider.ServiceProvider.GetRequiredService(); + return gateWays.GetGateway("A"); + } + } + + private class XGateway + { + public Guid Id { get; } = Guid.NewGuid(); + } + + + private class TestGatewayBuilder : IGatewayBuilder + { + private readonly IDictionary> _store = new Dictionary>(); + + /// + public bool TryAdd(string name, T gateway) where T : class + { + var t = typeof(T); + if (_store.ContainsKey(t)) + { + var innerStore = _store[t]; + innerStore.Add(name, gateway); + return true; + } + else + { + var innerStore = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + [name] = gateway + }; + _store.Add(t, innerStore); + return true; + } + + } + + /// + public IGatewayProvider Build() + { + return new GatewayProvider(_store); + } + + + private class GatewayStore + { + public string Name { get; set; } + public object Gateway { get; set; } + } + } + } +} diff --git a/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj b/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj new file mode 100644 index 0000000..66419f8 --- /dev/null +++ b/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj @@ -0,0 +1,22 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection\2.0.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll + + + + + From 64fc6766a200d8af7d2d554ef5c746f2499b4974 Mon Sep 17 00:00:00 2001 From: John0King Date: Thu, 3 May 2018 17:57:16 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20nuget=20=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj b/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj index 66419f8..d7efbef 100644 --- a/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj +++ b/test/PaySharp.AspNetCoreTest/PaySharp.AspNetCoreTest.csproj @@ -5,6 +5,7 @@ + @@ -12,11 +13,6 @@ - - - C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection\2.0.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll - - From a68a105e69077ae8b74f93f53a368bd24ff8a3dc Mon Sep 17 00:00:00 2001 From: John0King Date: Fri, 4 May 2018 09:16:38 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DI/DIExtensionTest.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs index 8166b54..0dfaf23 100644 --- a/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs +++ b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs @@ -42,6 +42,30 @@ public void ConfigDI_Repeate_Test() } } + [Fact] + public void ConfigDI_Repeate_1scope_Test() + { + var service = new ServiceCollection(); + service.TryAddScoped(); + service.AddPaySharp(builder => + { + builder.TryAdd("A", new XGateway()); + }); + + var provider = service.BuildServiceProvider(); + using(var scope = provider.CreateScope()) + { + var b = scope.ServiceProvider.GetRequiredService(); + var gateway1 = b.GetGateway("A"); + var b2 = scope.ServiceProvider.GetRequiredService(); + var gateway2 = b2.GetGateway("A"); + + } + + + + } + private XGateway GetGateway(IServiceProvider servicProvider) { using (var provider = servicProvider.CreateScope()) @@ -57,6 +81,9 @@ private class XGateway } + /// + /// 采用Add 方法的Builder, 这将在重复添加的时候报错 + /// private class TestGatewayBuilder : IGatewayBuilder { private readonly IDictionary> _store = new Dictionary>(); From 458cee4c718263bd7c061f945551acdd02515058 Mon Sep 17 00:00:00 2001 From: John0King Date: Fri, 4 May 2018 18:37:05 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E4=B8=80=E7=82=B9=E7=82=B9=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Abstractions/HandlerResult.cs | 20 ++++ .../Abstractions/INotifyHubHandler.cs | 8 +- .../Abstractions/INotifyResponse.cs | 21 ---- src/PaySharp.Core/Abstractions/NotifyHub.cs | 21 ++-- .../Abstractions/NotifyResponse.cs | 103 ++++++++++++++++++ .../Abstractions/ProcessResult.cs | 28 ----- .../Abstractions/ResponseResult.cs | 15 +++ 7 files changed, 158 insertions(+), 58 deletions(-) create mode 100644 src/PaySharp.Core/Abstractions/HandlerResult.cs delete mode 100644 src/PaySharp.Core/Abstractions/INotifyResponse.cs create mode 100644 src/PaySharp.Core/Abstractions/NotifyResponse.cs delete mode 100644 src/PaySharp.Core/Abstractions/ProcessResult.cs create mode 100644 src/PaySharp.Core/Abstractions/ResponseResult.cs diff --git a/src/PaySharp.Core/Abstractions/HandlerResult.cs b/src/PaySharp.Core/Abstractions/HandlerResult.cs new file mode 100644 index 0000000..de72885 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/HandlerResult.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Abstractions +{ + public class HandlerResult + { + public HandlerResult(NotifyResponse response) + { + Response = response ?? throw new ArgumentNullException(nameof(response)); + } + public bool IsSuccess => Response.IsSuccess; + public HubOrder Data { get; set; } + + public NotifyResponse Response { get; set; } + } +} diff --git a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs index 8df5c12..5ef2c40 100644 --- a/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs +++ b/src/PaySharp.Core/Abstractions/INotifyHubHandler.cs @@ -13,7 +13,13 @@ public interface INotifyHubHandler { [Obsolete("似乎没什么用", true)] INotifyDataConverter Converter { get; } - Task ProcessAsync(IKeyValueProvider valueProvider); + Task ProcessAsync(IKeyValueProvider valueProvider); + + /// + /// 获取失败的 + /// + /// + Task GetFailedResponseAsync(); } diff --git a/src/PaySharp.Core/Abstractions/INotifyResponse.cs b/src/PaySharp.Core/Abstractions/INotifyResponse.cs deleted file mode 100644 index 57a85d8..0000000 --- a/src/PaySharp.Core/Abstractions/INotifyResponse.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PaySharp.Abstractions -{ - public interface INotifyResponse - { - - INotifyResponse SetStateCode(int code); - INotifyResponse SetContextType(string type); - INotifyResponse AddHeader(IDictionary headers); - - INotifyResponse AddCookies(IDictionary cookies); - - Task WriteBodyAsync(Stream bodyStream); - } -} diff --git a/src/PaySharp.Core/Abstractions/NotifyHub.cs b/src/PaySharp.Core/Abstractions/NotifyHub.cs index 644cde9..933f515 100644 --- a/src/PaySharp.Core/Abstractions/NotifyHub.cs +++ b/src/PaySharp.Core/Abstractions/NotifyHub.cs @@ -44,7 +44,7 @@ public NotifyHub UseUserHandler(IUserHandler handler) } //[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] - public async Task ProcessAsync(IKeyValueProvider valueProvider) + public async Task ProcessAsync(IKeyValueProvider valueProvider) { // 首先检查, 成功和异常处理必须注册 @@ -57,14 +57,17 @@ public async Task ProcessAsync(IKeyValueProvider valueProvider) throw new InvalidOperationException("No Exception delegate registed"); } - // before - if(_beforeProcessHandler != null) - { - await _beforeProcessHandler(valueProvider); - } + + try { + // before + if (_beforeProcessHandler != null) + { + await _beforeProcessHandler(valueProvider); + } + // handing var result = await _hubHandler.ProcessAsync(valueProvider); if (result.IsSuccess) { @@ -77,17 +80,19 @@ public async Task ProcessAsync(IKeyValueProvider valueProvider) await _failHandler(result.Data,valueProvider); } } - + // after if (_afterProcessHandler != null) { await _afterProcessHandler(result.Data,valueProvider); } + return result.Response; } catch(Exception e) { await _exceptionHandler(e,valueProvider); + throw; } - + } } } diff --git a/src/PaySharp.Core/Abstractions/NotifyResponse.cs b/src/PaySharp.Core/Abstractions/NotifyResponse.cs new file mode 100644 index 0000000..5c5f88d --- /dev/null +++ b/src/PaySharp.Core/Abstractions/NotifyResponse.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Abstractions +{ + public class NotifyResponse : IDisposable + { + public NotifyResponse(bool isSuccess) + { + IsSuccess = isSuccess; + } + + public virtual bool IsSuccess { get; private set; } + public virtual int StatusCode { get; private set; } + public virtual string ContentType { get; private set; } + public virtual IDictionary Headers { get; private set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); + public virtual IDictionary Cookies { get; private set; } = new Dictionary(StringComparer.OrdinalIgnoreCase); + + public virtual Stream Body { get; private set; } = new MemoryStream(); + + public virtual NotifyResponse SetStatusCode(int code) + { + StatusCode = code; + return this; + } + public virtual NotifyResponse SetContentType(string type) + { + ContentType = type; + return this; + } + public virtual NotifyResponse AddHeader(Action> headerConfig) + { + headerConfig?.Invoke(Headers); + return this; + } + + public virtual NotifyResponse AddCookies(Action> cookiesConfig) + { + cookiesConfig?.Invoke(Cookies); + return this; + } + + public virtual async Task SetBodyAsync(Func bodyConfig) + { + if(bodyConfig != null) + { + await bodyConfig(Body); + } + Body.Seek(0, SeekOrigin.Begin); + return this; + } + public virtual NotifyResponse SetBody(Action bodyConfig) + { + bodyConfig?.Invoke(Body); + Body.Seek(0, SeekOrigin.Begin); + return this; + } + + #region IDisposable Support + private bool _isDisposed = false; // 要检测冗余调用 + + protected virtual void Dispose(bool disposing) + { + if (!_isDisposed) + { + if (disposing) + { + Body?.Dispose(); + Headers?.Clear(); + Cookies?.Clear(); + } + + // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。 + // TODO: 将大型字段设置为 null。 + Body = null; + Headers = null; + Cookies = null; + + _isDisposed = true; + } + } + + // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。 + // ~NotifyResponse() { + // // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 + // Dispose(false); + // } + + // 添加此代码以正确实现可处置模式。 + public void Dispose() + { + // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 + Dispose(true); + // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。 + // GC.SuppressFinalize(this); + } + #endregion + } +} diff --git a/src/PaySharp.Core/Abstractions/ProcessResult.cs b/src/PaySharp.Core/Abstractions/ProcessResult.cs deleted file mode 100644 index 56b0bcb..0000000 --- a/src/PaySharp.Core/Abstractions/ProcessResult.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PaySharp.Abstractions -{ - public class ProcessResult - { - public bool IsSuccess { get; private set; } - public HubOrder Data { get; private set; } - private ProcessResult(bool state,HubOrder data) - { - IsSuccess = state; - Data = data; - } - public static ProcessResult Success(HubOrder data) - { - return new ProcessResult(true, data); - } - - public static ProcessResult Fail(HubOrder data) - { - return new ProcessResult(false, data); - } - } -} diff --git a/src/PaySharp.Core/Abstractions/ResponseResult.cs b/src/PaySharp.Core/Abstractions/ResponseResult.cs new file mode 100644 index 0000000..2410ee6 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/ResponseResult.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Abstractions +{ + public class ResponseResult + { + public bool IsSuccess { get; set; } + + public NotifyResponse Response { get; set; } + } +} From 36e27a8b4768c700ada4ba1573ff534803c547cb Mon Sep 17 00:00:00 2001 From: John0King Date: Sun, 3 Jun 2018 15:53:28 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E4=B8=80=E7=82=B9=E7=82=B9=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DI/GatewayBuilderExtensions.cs | 18 ++++++++++++++++++ .../Abstractions/IGatewayBuilder.cs | 2 +- test/ICanPay.UnitTest/CodeWrite.cs | 19 ------------------- test/PaySharp.UnitTest/CodeWrite.cs | 19 +++++++++++++++++++ .../PaySharp.UnitTest.csproj | 4 ++-- 5 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs delete mode 100644 test/ICanPay.UnitTest/CodeWrite.cs create mode 100644 test/PaySharp.UnitTest/CodeWrite.cs diff --git a/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs b/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs new file mode 100644 index 0000000..68988af --- /dev/null +++ b/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PaySharp.Abstractions; + +namespace PaySharp.Alipay +{ + public static class GatewayBuilderExtensions + { + public static IGatewayBuilder AddAlipay(this IGatewayBuilder builder) + { + + return builder; + } + } +} diff --git a/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs b/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs index fa55285..e71ee70 100644 --- a/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs +++ b/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs @@ -19,7 +19,7 @@ public interface IGatewayBuilder bool TryAdd(string name,T gateway) where T:class; /// - /// 返回一个 的实例 + /// 返回一个 的实例 /// /// IGatewayProvider Build(); diff --git a/test/ICanPay.UnitTest/CodeWrite.cs b/test/ICanPay.UnitTest/CodeWrite.cs deleted file mode 100644 index b6d972b..0000000 --- a/test/ICanPay.UnitTest/CodeWrite.cs +++ /dev/null @@ -1,19 +0,0 @@ -using ICanPay.Abstractions; -using ICanPay.AspNetCore; -using Microsoft.AspNetCore.Http; -using System; -using System.Collections.Generic; -using System.Text; - -namespace ICanPay.UnitTest -{ - class CodeWrite - { - public void Write() - { - var provider = new HttpContextKeyValueProvider(new HttpContextAccessor()); - var hub = new NotifyHub(null); - hub. - } - } -} diff --git a/test/PaySharp.UnitTest/CodeWrite.cs b/test/PaySharp.UnitTest/CodeWrite.cs new file mode 100644 index 0000000..0bbb7f6 --- /dev/null +++ b/test/PaySharp.UnitTest/CodeWrite.cs @@ -0,0 +1,19 @@ +using ICanPay.Abstractions; +using ICanPay.AspNetCore; +using Microsoft.AspNetCore.Http; +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICanPay.UnitTest +{ + //class CodeWrite + //{ + // public void Write() + // { + // var provider = new HttpContextKeyValueProvider(new HttpContextAccessor()); + // var hub = new NotifyHub(null); + // hub. + // } + //} +} diff --git a/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj b/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj index cf6c887..c94b01c 100644 --- a/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj +++ b/test/PaySharp.UnitTest/PaySharp.UnitTest.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.0 @@ -21,7 +21,7 @@ - + From 467333a742a39846a285d5f2ce65af8c9efa9674 Mon Sep 17 00:00:00 2001 From: John0King Date: Mon, 27 Aug 2018 21:36:33 +0800 Subject: [PATCH 14/14] some idea --- .../DI/GatewayBuilderExtensions.cs | 18 -- .../DI/PaySharpBuilderExtensions.cs | 22 ++ .../DI/PaySharpProviderExtensions.cs | 29 ++ src/PaySharp.Alipay/DI/fake/AlipayFactory.cs | 20 ++ .../DI/fake/FakeAlipayClient.cs | 12 + .../DI/fake/FakeAlipayListener.cs | 12 + src/PaySharp.Alipay/DI/fake/IAlipayClient.cs | 12 + .../DI/fake/IAlipayListener.cs | 12 + src/PaySharp.Alipay/Merchant.cs | 8 +- .../Internal/PaySharpBuilder.cs | 36 +++ .../Internal/PaySharpProvider.cs | 30 +++ .../ServiceCollectionExtensions.cs | 27 +- .../Abstractions/IGatewayProvider.cs | 12 - ...IGatewayBuilder.cs => IPaySharpBuilder.cs} | 13 +- .../Abstractions/IPaySharpOption.cs | 13 + .../Abstractions/IPaySharpProvider.cs | 16 ++ src/PaySharp.Core/Internal/GatewayBuilder.cs | 104 +++---- .../Internal/GatewayDefinition.cs | 45 ++++ src/PaySharp.Core/Internal/GatewayProvider.cs | 74 ++--- .../DI/DIExtensionTest.cs | 254 +++++++++--------- 20 files changed, 495 insertions(+), 274 deletions(-) delete mode 100644 src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs create mode 100644 src/PaySharp.Alipay/DI/PaySharpBuilderExtensions.cs create mode 100644 src/PaySharp.Alipay/DI/PaySharpProviderExtensions.cs create mode 100644 src/PaySharp.Alipay/DI/fake/AlipayFactory.cs create mode 100644 src/PaySharp.Alipay/DI/fake/FakeAlipayClient.cs create mode 100644 src/PaySharp.Alipay/DI/fake/FakeAlipayListener.cs create mode 100644 src/PaySharp.Alipay/DI/fake/IAlipayClient.cs create mode 100644 src/PaySharp.Alipay/DI/fake/IAlipayListener.cs create mode 100644 src/PaySharp.AspNetCore/Internal/PaySharpBuilder.cs create mode 100644 src/PaySharp.AspNetCore/Internal/PaySharpProvider.cs delete mode 100644 src/PaySharp.Core/Abstractions/IGatewayProvider.cs rename src/PaySharp.Core/Abstractions/{IGatewayBuilder.cs => IPaySharpBuilder.cs} (56%) create mode 100644 src/PaySharp.Core/Abstractions/IPaySharpOption.cs create mode 100644 src/PaySharp.Core/Abstractions/IPaySharpProvider.cs create mode 100644 src/PaySharp.Core/Internal/GatewayDefinition.cs diff --git a/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs b/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs deleted file mode 100644 index 68988af..0000000 --- a/src/PaySharp.Alipay/DI/GatewayBuilderExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using PaySharp.Abstractions; - -namespace PaySharp.Alipay -{ - public static class GatewayBuilderExtensions - { - public static IGatewayBuilder AddAlipay(this IGatewayBuilder builder) - { - - return builder; - } - } -} diff --git a/src/PaySharp.Alipay/DI/PaySharpBuilderExtensions.cs b/src/PaySharp.Alipay/DI/PaySharpBuilderExtensions.cs new file mode 100644 index 0000000..f207e84 --- /dev/null +++ b/src/PaySharp.Alipay/DI/PaySharpBuilderExtensions.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using PaySharp.Abstractions; +using PaySharp.Alipay.DI.fake; + +namespace PaySharp.Alipay +{ + public static class PaySharpBuilderExtensions + { + public static IPaySharpBuilder AddAlipay(this IPaySharpBuilder builder,Merchant option) + { + builder.AddOption(option); + builder.TryAddService(); + builder.TryAddService(); + builder.TryAddService(); + return builder; + } + } +} diff --git a/src/PaySharp.Alipay/DI/PaySharpProviderExtensions.cs b/src/PaySharp.Alipay/DI/PaySharpProviderExtensions.cs new file mode 100644 index 0000000..04fed5b --- /dev/null +++ b/src/PaySharp.Alipay/DI/PaySharpProviderExtensions.cs @@ -0,0 +1,29 @@ +using PaySharp.Abstractions; +using PaySharp.Alipay.DI.fake; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI +{ + public static class PaySharpProviderExtensions + { + public static IAlipayClient CreateAlipayClient(this IPaySharpProvider provider) + { + var option = provider.GetRequired(); + return CreateAlipayClient(provider,option); + } + public static IAlipayClient CreateAlipayClient(this IPaySharpProvider provider,Merchant merchant) + { + var factory = provider.GetRequired(); + return factory.CreateClient(merchant); + } + + public static IAlipayListener CreateAlipayListener(this IPaySharpProvider provider,Merchant merchant) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/PaySharp.Alipay/DI/fake/AlipayFactory.cs b/src/PaySharp.Alipay/DI/fake/AlipayFactory.cs new file mode 100644 index 0000000..b7b659e --- /dev/null +++ b/src/PaySharp.Alipay/DI/fake/AlipayFactory.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI.fake +{ + class AlipayFactory + { + public IAlipayClient CreateClient(Merchant merchant) + { + throw new NotImplementedException(); + } + public IAlipayListener CreateListener(Merchant merchant) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/PaySharp.Alipay/DI/fake/FakeAlipayClient.cs b/src/PaySharp.Alipay/DI/fake/FakeAlipayClient.cs new file mode 100644 index 0000000..62d56b0 --- /dev/null +++ b/src/PaySharp.Alipay/DI/fake/FakeAlipayClient.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI.fake +{ + class FakeAlipayClient:IAlipayClient + { + } +} diff --git a/src/PaySharp.Alipay/DI/fake/FakeAlipayListener.cs b/src/PaySharp.Alipay/DI/fake/FakeAlipayListener.cs new file mode 100644 index 0000000..e5fe250 --- /dev/null +++ b/src/PaySharp.Alipay/DI/fake/FakeAlipayListener.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI.fake +{ + class FakeAlipayListener:IAlipayListener + { + } +} diff --git a/src/PaySharp.Alipay/DI/fake/IAlipayClient.cs b/src/PaySharp.Alipay/DI/fake/IAlipayClient.cs new file mode 100644 index 0000000..2b627a1 --- /dev/null +++ b/src/PaySharp.Alipay/DI/fake/IAlipayClient.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI.fake +{ + public interface IAlipayClient + { + } +} diff --git a/src/PaySharp.Alipay/DI/fake/IAlipayListener.cs b/src/PaySharp.Alipay/DI/fake/IAlipayListener.cs new file mode 100644 index 0000000..2ee19b7 --- /dev/null +++ b/src/PaySharp.Alipay/DI/fake/IAlipayListener.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Alipay.DI.fake +{ + public interface IAlipayListener + { + } +} diff --git a/src/PaySharp.Alipay/Merchant.cs b/src/PaySharp.Alipay/Merchant.cs index 161f908..5ae2bd7 100644 --- a/src/PaySharp.Alipay/Merchant.cs +++ b/src/PaySharp.Alipay/Merchant.cs @@ -1,11 +1,17 @@ using PaySharp.Core; +using PaySharp.Abstractions; using System; using System.ComponentModel.DataAnnotations; namespace PaySharp.Alipay { - public class Merchant : IMerchant + public class Merchant : IMerchant,IPaySharpOption { + private string _name; + public string Name { + get => _name ?? AppId; + set => _name = value; + } #region 属性 /// diff --git a/src/PaySharp.AspNetCore/Internal/PaySharpBuilder.cs b/src/PaySharp.AspNetCore/Internal/PaySharpBuilder.cs new file mode 100644 index 0000000..4657dbd --- /dev/null +++ b/src/PaySharp.AspNetCore/Internal/PaySharpBuilder.cs @@ -0,0 +1,36 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using PaySharp.Abstractions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.AspNetCore.Internal +{ + public class PaySharpBuilder : IPaySharpBuilder + { + private readonly IServiceCollection _services; + + public PaySharpBuilder(IServiceCollection services) + { + _services = services; + } + + public void AddOption(T option) where T : class, IPaySharpOption + { + _services.AddScoped(s => option); + _services.AddScoped(s => option); + } + + + public void TryAddService(T service) where T : class + { + _services.TryAddScoped(s => service); + } + + public void TryAddService() where T:class where TImplementation :class, T + { + _services.TryAddScoped(); + } + } +} diff --git a/src/PaySharp.AspNetCore/Internal/PaySharpProvider.cs b/src/PaySharp.AspNetCore/Internal/PaySharpProvider.cs new file mode 100644 index 0000000..793e7fa --- /dev/null +++ b/src/PaySharp.AspNetCore/Internal/PaySharpProvider.cs @@ -0,0 +1,30 @@ +using PaySharp.Abstractions; +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.AspNetCore.Internal +{ + public class PaySharpProvider : IPaySharpProvider + { + public IEnumerable GetMutiple() where T : class + { + throw new NotImplementedException(); + } + + public T GetRequired() where T : class + { + throw new NotImplementedException(); + } + + T IPaySharpProvider.GetOption() + { + throw new NotImplementedException(); + } + + IEnumerable IPaySharpProvider.GetOptions() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs index eae2dae..30a25b0 100644 --- a/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs @@ -1,11 +1,11 @@ -using PaySharp.Internal; -using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.DependencyInjection.Extensions; using System; using System.Collections.Generic; using System.Text; using PaySharp.Abstractions; using System.ComponentModel; using PaySharp.AspNetCore; +using PaySharp.AspNetCore.Internal; namespace Microsoft.Extensions.DependencyInjection { @@ -14,13 +14,6 @@ namespace Microsoft.Extensions.DependencyInjection /// public static class ServiceCollectionExtensions { - [EditorBrowsable(EditorBrowsableState.Never)]//先不要让其他人访问 - public static IServiceCollection AddPaySharp(this IServiceCollection services) - { - services = services ?? throw new ArgumentNullException(nameof(services)); - AddPaySharp(services, null); - return services; - } /// /// 添加PaySharp相关服务 @@ -28,21 +21,13 @@ public static IServiceCollection AddPaySharp(this IServiceCollection services) /// /// /// - public static IServiceCollection AddPaySharp(this IServiceCollection services,Action buildAction) + public static IPaySharpBuilder AddPaySharp(this IServiceCollection services) { - services.TryAddScoped(); + services.TryAddScoped(); services.TryAddScoped(); - services.TryAddScoped(svcs => - { - var builder = svcs.GetRequiredService(); - if (builder != null) - { - buildAction(builder); - } - return builder.Build(); - }); + - return services; + return new PaySharpBuilder(services); } } } diff --git a/src/PaySharp.Core/Abstractions/IGatewayProvider.cs b/src/PaySharp.Core/Abstractions/IGatewayProvider.cs deleted file mode 100644 index 21cd8c0..0000000 --- a/src/PaySharp.Core/Abstractions/IGatewayProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace PaySharp.Abstractions -{ - public interface IGatewayProvider - { - T GetGateway() where T : class; - T GetGateway(string gatewayName) where T : class; - } -} diff --git a/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs b/src/PaySharp.Core/Abstractions/IPaySharpBuilder.cs similarity index 56% rename from src/PaySharp.Core/Abstractions/IGatewayBuilder.cs rename to src/PaySharp.Core/Abstractions/IPaySharpBuilder.cs index e71ee70..5f4e52f 100644 --- a/src/PaySharp.Core/Abstractions/IGatewayBuilder.cs +++ b/src/PaySharp.Core/Abstractions/IPaySharpBuilder.cs @@ -7,21 +7,18 @@ namespace PaySharp.Abstractions /// /// 网关提供器的构造器 /// - public interface IGatewayBuilder + public interface IPaySharpBuilder { /// /// 添加一个命名的网关 /// /// 网关的类型 /// 网关的名字 - /// 网关 + /// 网关 /// - bool TryAdd(string name,T gateway) where T:class; + void TryAddService(T service) where T : class; + void TryAddService() where T:class where TImplementation :class, T; + void AddOption(T option) where T : class, IPaySharpOption; - /// - /// 返回一个 的实例 - /// - /// - IGatewayProvider Build(); } } diff --git a/src/PaySharp.Core/Abstractions/IPaySharpOption.cs b/src/PaySharp.Core/Abstractions/IPaySharpOption.cs new file mode 100644 index 0000000..615aed1 --- /dev/null +++ b/src/PaySharp.Core/Abstractions/IPaySharpOption.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PaySharp.Abstractions +{ + public interface IPaySharpOption + { + string Name { get; set; } + } +} diff --git a/src/PaySharp.Core/Abstractions/IPaySharpProvider.cs b/src/PaySharp.Core/Abstractions/IPaySharpProvider.cs new file mode 100644 index 0000000..7b4730a --- /dev/null +++ b/src/PaySharp.Core/Abstractions/IPaySharpProvider.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PaySharp.Abstractions +{ + public interface IPaySharpProvider + { + T GetRequired() where T : class; + IEnumerable GetMutiple() where T : class; + + T GetOption() where T : class, IPaySharpOption; + + IEnumerable GetOptions() where T : class, IPaySharpOption; + } +} diff --git a/src/PaySharp.Core/Internal/GatewayBuilder.cs b/src/PaySharp.Core/Internal/GatewayBuilder.cs index a858941..81e34f2 100644 --- a/src/PaySharp.Core/Internal/GatewayBuilder.cs +++ b/src/PaySharp.Core/Internal/GatewayBuilder.cs @@ -1,57 +1,61 @@ -using System; -using System.Collections.Generic; -using System.Collections.Concurrent; -using System.Text; -using PaySharp.Abstractions; +//using System; +//using System.Collections.Generic; +//using System.Collections.Concurrent; +//using System.Text; +//using PaySharp.Abstractions; -namespace PaySharp.Internal -{ - /// - public class GatewayBuilder : IGatewayBuilder - { - private readonly IDictionary> _store = new Dictionary>(); +//namespace PaySharp.Internal +//{ +// /// +// public class GatewayBuilder : IPaySharpBuilder +// { +// private readonly IDictionary>> _store = new Dictionary>>(); - /// - public bool TryAdd(string name, T gateway) where T : class - { - try - { - var t = typeof(T); - if (_store.ContainsKey(t)) - { - var innerStore = _store[t]; - innerStore[name] = gateway; - return true; - } - else - { - var innerStore = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - [name] = gateway - }; - _store.Add(t, innerStore); - return true; - } - } - catch - { - return false; - } +// /// +// public bool TryAdd(string name, T gateway) where T : class +// { +// try +// { +// var t = typeof(T); +// if (_store.ContainsKey(t)) +// { +// var innerStore = _store[t]; +// innerStore[name] = new Lazy(() => gateway); +// return true; +// } +// else +// { +// var innerStore = new Dictionary>(StringComparer.OrdinalIgnoreCase) +// { +// [name] = new Lazy(() => gateway) +// }; +// _store.Add(t, innerStore); +// return true; +// } +// } +// catch +// { +// return false; +// } - } +// } - /// - public IGatewayProvider Build() - { - return new GatewayProvider(_store); - } +// /// +// public IGatewayProvider Build() +// { +// return new GatewayProvider(_store); +// } +// public bool TryAdd(string name, Func gateway) where T : class +// { +// throw new NotImplementedException(); +// } - private class GatewayStore - { - public string Name { get; set; } - public object Gateway { get; set; } - } - } -} +// private class GatewayStore +// { +// public string Name { get; set; } +// public object Gateway { get; set; } +// } +// } +//} diff --git a/src/PaySharp.Core/Internal/GatewayDefinition.cs b/src/PaySharp.Core/Internal/GatewayDefinition.cs new file mode 100644 index 0000000..f2bee9b --- /dev/null +++ b/src/PaySharp.Core/Internal/GatewayDefinition.cs @@ -0,0 +1,45 @@ +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; + +//namespace PaySharp.Core.Internal +//{ +// public class GatewayDefinition +// { +// public virtual string Name { get; set; } +// public IGatewayOption Option { get; set; } + +// public virtual Func Create { get; set; } +// } + +// public class GatewayDefinition :GatewayDefinition where T : IGatewayOption +// { +// public new T Option +// { +// get +// { +// if(base.Option is T t) +// { +// return t; +// } +// return default(T); +// } +// } + +// public new Func> Create +// { +// get +// { +// return base.Create as Func>; +// } +// set +// { +// base.Create = value; +// } +// } +// } + + +//} diff --git a/src/PaySharp.Core/Internal/GatewayProvider.cs b/src/PaySharp.Core/Internal/GatewayProvider.cs index 6b59aac..9603529 100644 --- a/src/PaySharp.Core/Internal/GatewayProvider.cs +++ b/src/PaySharp.Core/Internal/GatewayProvider.cs @@ -1,41 +1,41 @@ -using PaySharp.Abstractions; -using System; -using System.Collections.Generic; -using System.Text; +//using PaySharp.Abstractions; +//using System; +//using System.Collections.Generic; +//using System.Text; -namespace PaySharp.Internal -{ - /// - public class GatewayProvider : IGatewayProvider - { - private readonly IDictionary> _store; +//namespace PaySharp.Internal +//{ +// /// +// public class GatewayProvider : IGatewayProvider +// { +// private readonly IDictionary> _store; - public GatewayProvider(IDictionary> store) - { - _store = store ?? throw new ArgumentNullException(nameof(store)); - } +// public GatewayProvider(IDictionary> store) +// { +// _store = store ?? throw new ArgumentNullException(nameof(store)); +// } - /// - public T GetGateway() where T:class - { - return GetGateway(string.Empty); - } +// /// +// public T GetGateway() where T:class +// { +// return GetGateway(string.Empty); +// } - /// - public T GetGateway(string gatewayName) where T:class - { - gatewayName = gatewayName ?? throw new ArgumentNullException(nameof(gatewayName), "Gateway name must not be null"); - if (_store.TryGetValue(typeof(T), out var innerStore)) - { - if (innerStore.TryGetValue(gatewayName, out var gateway)) - { - if(gateway is T) - { - return (T)gateway; - } - } - } - return null; - } - } -} +// /// +// public T GetGateway(string gatewayName) where T:class +// { +// gatewayName = gatewayName ?? throw new ArgumentNullException(nameof(gatewayName), "Gateway name must not be null"); +// if (_store.TryGetValue(typeof(T), out var innerStore)) +// { +// if (innerStore.TryGetValue(gatewayName, out var gateway)) +// { +// if(gateway is T) +// { +// return (T)gateway; +// } +// } +// } +// return null; +// } +// } +//} diff --git a/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs index 0dfaf23..c415769 100644 --- a/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs +++ b/test/PaySharp.AspNetCoreTest/DI/DIExtensionTest.cs @@ -1,127 +1,127 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using PaySharp.Abstractions; -using PaySharp.Internal; -using System; -using System.Collections.Generic; -using Xunit; - -namespace PaySharp.AspNetCoreTest -{ - public class DIExtensionTest - { - [Fact] - public void ConfigDI_Repeate_Test() - { - var service = new ServiceCollection(); - service.TryAddScoped(); - service.AddPaySharp(builder => - { - builder.TryAdd("A", new XGateway()); - }); - - var provider = service.BuildServiceProvider(); - - try - { - var gateway1 = GetGateway(provider); - } - catch(Exception e) - { - throw new Exception("第一次获取就出错了", e); - } - - - try - { - var gateway2 = GetGateway(provider); - } - catch(Exception e) - { - throw new Exception("第二次获取出错了", e); - } - } - - [Fact] - public void ConfigDI_Repeate_1scope_Test() - { - var service = new ServiceCollection(); - service.TryAddScoped(); - service.AddPaySharp(builder => - { - builder.TryAdd("A", new XGateway()); - }); - - var provider = service.BuildServiceProvider(); - using(var scope = provider.CreateScope()) - { - var b = scope.ServiceProvider.GetRequiredService(); - var gateway1 = b.GetGateway("A"); - var b2 = scope.ServiceProvider.GetRequiredService(); - var gateway2 = b2.GetGateway("A"); - - } - - - - } - - private XGateway GetGateway(IServiceProvider servicProvider) - { - using (var provider = servicProvider.CreateScope()) - { - var gateWays = provider.ServiceProvider.GetRequiredService(); - return gateWays.GetGateway("A"); - } - } - - private class XGateway - { - public Guid Id { get; } = Guid.NewGuid(); - } - - - /// - /// 采用Add 方法的Builder, 这将在重复添加的时候报错 - /// - private class TestGatewayBuilder : IGatewayBuilder - { - private readonly IDictionary> _store = new Dictionary>(); - - /// - public bool TryAdd(string name, T gateway) where T : class - { - var t = typeof(T); - if (_store.ContainsKey(t)) - { - var innerStore = _store[t]; - innerStore.Add(name, gateway); - return true; - } - else - { - var innerStore = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - [name] = gateway - }; - _store.Add(t, innerStore); - return true; - } - - } - - /// - public IGatewayProvider Build() - { - return new GatewayProvider(_store); - } - - - private class GatewayStore - { - public string Name { get; set; } - public object Gateway { get; set; } - } - } - } -} +//using Microsoft.Extensions.DependencyInjection; +//using Microsoft.Extensions.DependencyInjection.Extensions; +//using PaySharp.Abstractions; +//using PaySharp.Internal; +//using System; +//using System.Collections.Generic; +//using Xunit; + +//namespace PaySharp.AspNetCoreTest +//{ +// public class DIExtensionTest +// { +// [Fact] +// public void ConfigDI_Repeate_Test() +// { +// var service = new ServiceCollection(); +// service.TryAddScoped(); +// service.AddPaySharp(builder => +// { +// builder.TryAdd("A", new XGateway()); +// }); + +// var provider = service.BuildServiceProvider(); + +// try +// { +// var gateway1 = GetGateway(provider); +// } +// catch (Exception e) +// { +// throw new Exception("第一次获取就出错了", e); +// } + + +// try +// { +// var gateway2 = GetGateway(provider); +// } +// catch (Exception e) +// { +// throw new Exception("第二次获取出错了", e); +// } +// } + +// [Fact] +// public void ConfigDI_Repeate_1scope_Test() +// { +// var service = new ServiceCollection(); +// service.TryAddScoped(); +// service.AddPaySharp(builder => +// { +// builder.TryAdd("A", new XGateway()); +// }); + +// var provider = service.BuildServiceProvider(); +// using (var scope = provider.CreateScope()) +// { +// var b = scope.ServiceProvider.GetRequiredService(); +// var gateway1 = b.GetGateway("A"); +// var b2 = scope.ServiceProvider.GetRequiredService(); +// var gateway2 = b2.GetGateway("A"); + +// } + + + +// } + +// private XGateway GetGateway(IServiceProvider servicProvider) +// { +// using (var provider = servicProvider.CreateScope()) +// { +// var gateWays = provider.ServiceProvider.GetRequiredService(); +// return gateWays.GetGateway("A"); +// } +// } + +// private class XGateway +// { +// public Guid Id { get; } = Guid.NewGuid(); +// } + + +// /// +// /// 采用Add 方法的Builder, 这将在重复添加的时候报错 +// /// +// private class TestGatewayBuilder : IPaySharpBuilder +// { +// private readonly IDictionary> _store = new Dictionary>(); + +// /// +// public bool TryAdd(string name, T gateway) where T : class +// { +// var t = typeof(T); +// if (_store.ContainsKey(t)) +// { +// var innerStore = _store[t]; +// innerStore.Add(name, gateway); +// return true; +// } +// else +// { +// var innerStore = new Dictionary(StringComparer.OrdinalIgnoreCase) +// { +// [name] = gateway +// }; +// _store.Add(t, innerStore); +// return true; +// } + +// } + +// /// +// public IGatewayProvider Build() +// { +// return new GatewayProvider(_store); +// } + + +// private class GatewayStore +// { +// public string Name { get; set; } +// public object Gateway { get; set; } +// } +// } +// } +//}