Nice 3 years ago
parent
commit
6ef28be1be
  1. 2
      Gear.Test.Api/Controllers/WeatherForecastController.cs
  2. 16
      Gear.Test.Api/Program.cs
  3. 2
      Gear.sln
  4. 2
      Gear/Gear.csproj
  5. 50
      Gear/Gear/Abstractions/AppService.cs
  6. 1
      Gear/Gear/Abstractions/Use.cs
  7. 32
      Gear/Gear/Generations/SequentialGuidGenerator.cs
  8. 5
      Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs
  9. 4
      Gear/Gear/Infrastructure/Wapper/Base/EventBase.cs

2
Gear.Test.Api/Controllers/WeatherForecastController.cs

@ -18,7 +18,7 @@ namespace Gear.Test.Api.Controllers
}; };
private ITestUse TestUse => LazyServiceProvider.LazyGetService<ITestUse>(); private ITestUse TestUse => LazyServiceProvider.LazyGetService<ITestUse>();
public IEnumerable<WeatherForecast> Get() public IEnumerable<WeatherForecast> GetPostersAsync()
{ {
var d = LazyServiceProvider; var d = LazyServiceProvider;
var c = Configuration; var c = Configuration;

16
Gear.Test.Api/Program.cs

@ -1,3 +1,5 @@
using Gear.Generations;
using IdGen;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -13,6 +15,20 @@ namespace Gear.Test.Api
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
//var generator = new IdGenerator(0);
//var id = generator.CreateId();
//List<object> ls = new List<object>();
//var id1 = new SequentialGuidGenerator().Create();
//var id2 = new SequentialGuidGenerator().Create();
//var id3 = new SequentialGuidGenerator().Create();
//var id4 = new SequentialGuidGenerator().Create();
//ls.Add(id1);
//ls.Add(id2);
//ls.Add(id3);
//ls.Add(id4);
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();
} }

2
Gear.sln

@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.31605.320
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gear", "Gear\Gear.csproj", "{E98AD43A-28B5-4046-805E-313EF48D9AEE}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gear", "Gear\Gear.csproj", "{E98AD43A-28B5-4046-805E-313EF48D9AEE}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gear.Test.Api", "Gear.Test.Api\Gear.Test.Api.csproj", "{D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gear.Test.Api", "Gear.Test.Api\Gear.Test.Api.csproj", "{D8E0FF08-B8FA-45AE-B20A-6393B1DBCF18}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

2
Gear/Gear.csproj

@ -8,7 +8,7 @@
<Company>全网科技</Company> <Company>全网科技</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Gear</PackageId> <PackageId>Gear</PackageId>
<Version>0.1.8</Version> <Version>0.2.1</Version>
<Product>Gear</Product> <Product>Gear</Product>
<AssemblyName>Gear</AssemblyName> <AssemblyName>Gear</AssemblyName>
</PropertyGroup> </PropertyGroup>

50
Gear/Gear/Abstractions/AppService.cs

@ -13,7 +13,7 @@ namespace Gear.Abstractions
public abstract class AppService : Use public abstract class AppService : Use
{ {
public static string[] CommonPostfixes { get; } = { "AppService", "Service" }; public static string[] CommonPostfixes { get; } = { "AppService", "Service" };
public static string[] ActionPostfixes { get; } = { "Async", public static string[] ActionPostfixes { get; } = {
"GetAll", "GetList", "Get", "GetAll", "GetList", "Get",
"Post", "Create", "Add", "Insert", "Post", "Create", "Add", "Insert",
"Put", "Update", "Put", "Update",
@ -21,24 +21,16 @@ namespace Gear.Abstractions
"Patch" "Patch"
}; };
protected const string DaprPubSubName = "pubsub"; protected const string DaprPubSubName = "pubsub";
protected const string DaprStoreName = "store"; protected const string DaprStoreName = "store";
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> for user associated with the executing action.
/// </summary>
protected ClaimsPrincipal User => HttpContext?.User!; protected ClaimsPrincipal User => HttpContext?.User!;
/// <summary>
/// Gets the <see cref="Http.HttpContext"/> for the executing action.
/// </summary>
protected HttpContext HttpContext => LazyServiceProvider.LazyGetService<IHttpContextAccessor>().HttpContext; protected HttpContext HttpContext => LazyServiceProvider.LazyGetService<IHttpContextAccessor>().HttpContext;
protected IDecryptClaim DecryptClaim => LazyServiceProvider.LazyGetService<IDecryptClaim>(); protected IDecryptClaim DecryptClaim => LazyServiceProvider.LazyGetService<IDecryptClaim>();
//获得HttpMethod public static string GetHttpMethod(string actionName) //获得HttpMethod
public static string GetHttpMethod(string actionName)
{ {
if (actionName.StartsWith("Get")) if (actionName.StartsWith("Get"))
return "GET"; return "GET";
@ -55,8 +47,7 @@ namespace Gear.Abstractions
return "POST"; return "POST";
} }
//获得一个自定义的路由 public static string RouteTemplate(string controllerTypeName, string actionMethodName, string idStr) //获得一个自定义的路由
public static string RouteTemplate(string controllerTypeName, string actionMethodName, string idStr)
{ {
string url = "api"; string url = "api";
@ -79,21 +70,40 @@ namespace Gear.Abstractions
return url; return url;
} }
// 过滤控制器 public static string FiterControllerName(string controllerName) // 过滤控制器
public static string FiterControllerName(string controllerName)
{ {
foreach (var commonPostfixe in CommonPostfixes) foreach (var commonPostfixe in CommonPostfixes)
controllerName = controllerName.Replace(commonPostfixe, ""); {
if (controllerName.EndsWith(commonPostfixe))
{
controllerName = controllerName.Remove(controllerName.IndexOf(commonPostfixe));
break;
}
}
return controllerName; return controllerName;
} }
// 过滤方法
public static string FiterActionName(string actionName) public static string FiterActionName(string actionName) // 过滤方法
{ {
//对外方法不能有这些关键字 //对外方法不能有这些关键字
foreach (var old in ActionPostfixes) foreach (var actionPostfixe in ActionPostfixes)
actionName = actionName.Replace(old, ""); {
if (actionName.StartsWith(actionPostfixe))
{
actionName = actionName.Remove(actionName.IndexOf(actionPostfixe), actionPostfixe.Count());
if (actionName.EndsWith("Async"))
{
actionName = actionName.Remove(actionName.IndexOf("Async"));
}
break;
}
}
return actionName; return actionName;
} }
protected long GetUserId() protected long GetUserId()
{ {
if ((Configuration["LocalDebug"] ?? "") == "true") if ((Configuration["LocalDebug"] ?? "") == "true")

1
Gear/Gear/Abstractions/Use.cs

@ -49,7 +49,6 @@ namespace Gear.Abstractions
return ApiResponse.DROP(); return ApiResponse.DROP();
} }
// 代码优化之最佳WHEN 参考 IF
protected static void When(bool isOk, string errMessage) //卫语句 protected static void When(bool isOk, string errMessage) //卫语句
{ {
OVerify.When(isOk, errMessage); OVerify.When(isOk, errMessage);

32
Gear/Gear/Generations/SequentialGuidGenerator.cs

@ -2,7 +2,7 @@
using System; using System;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace Gear.Gear.Generations namespace Gear.Generations
{ {
public class SequentialGuidGenerator : ISequentialGuidGenerator, ITransientDependency public class SequentialGuidGenerator : ISequentialGuidGenerator, ITransientDependency
{ {
@ -10,49 +10,23 @@ namespace Gear.Gear.Generations
/// <summary> /// <summary>
/// 现在是SQLServer 的 算法 Guid 这个如果切库 需要换 /// 现在是SQLServer 的 算法 Guid 这个如果切库 需要换
/// 源码地址: https://github.com/abpframework/abp/blob/48c52625f4c4df007f04d5ac6368b07411aa7521/framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SequentialGuidGenerator.cs /// ABP:https://docs.abp.io/zh-Hans/abp/latest/Guid-Generation#abpsequentialguidgeneratoroptions
/// 源码地址:https://github.com/abpframework/abp/blob/48c52625f4c4df007f04d5ac6368b07411aa7521/framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SequentialGuidGenerator.cs
/// </summary> /// </summary>
public Guid Create() public Guid Create()
{ {
// We start with 16 bytes of cryptographically strong random data.
var randomBytes = new byte[10]; var randomBytes = new byte[10];
RandomNumberGenerator.GetBytes(randomBytes); RandomNumberGenerator.GetBytes(randomBytes);
// An alternate method: use a normally-created GUID to get our initial
// random data:
// byte[] randomBytes = Guid.NewGuid().ToByteArray();
// This is faster than using RNGCryptoServiceProvider, but I don't
// recommend it because the .NET Framework makes no guarantee of the
// randomness of GUID data, and future versions (or different
// implementations like Mono) might use a different method.
// Now we have the random basis for our GUID. Next, we need to
// create the six-byte block which will be our timestamp.
// We start with the number of milliseconds that have elapsed since
// DateTime.MinValue. This will form the timestamp. There's no use
// being more specific than milliseconds, since DateTime.Now has
// limited resolution.
// Using millisecond resolution for our 48-bit timestamp gives us
// about 5900 years before the timestamp overflows and cycles.
// Hopefully this should be sufficient for most purposes. :)
long timestamp = DateTime.UtcNow.Ticks / 10000L; long timestamp = DateTime.UtcNow.Ticks / 10000L;
// Then get the bytes
byte[] timestampBytes = BitConverter.GetBytes(timestamp); byte[] timestampBytes = BitConverter.GetBytes(timestamp);
// Since we're converting from an Int64, we have to reverse on
// little-endian systems.
if (BitConverter.IsLittleEndian) if (BitConverter.IsLittleEndian)
{ {
Array.Reverse(timestampBytes); Array.Reverse(timestampBytes);
} }
byte[] guidBytes = new byte[16]; byte[] guidBytes = new byte[16];
// For sequential-at-the-end versions, we copy the random data first,
// followed by the timestamp.
Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10); Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10);
Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6); Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6);

5
Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs

@ -32,10 +32,7 @@ namespace Gear.Infrastructure.Extensions
public static IServiceCollection AddGear(this IServiceCollection services) public static IServiceCollection AddGear(this IServiceCollection services)
{ {
var assemblies = DependencyContext.Default.CompileLibraries. var assemblies = DependencyContext.Default.CompileLibraries.
Where(lib => Where(lib => !lib.Serviceable && lib.Type != "package" && lib.Type != "referenceassembly").
!lib.Serviceable &&
lib.Type != "package" &&
lib.Type != "referenceassembly").
Select(lib => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name))).ToList(); Select(lib => AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name))).ToList();
if (!assemblies.Contains(typeof(ServiceCollectionServiceExtensions).Assembly)) if (!assemblies.Contains(typeof(ServiceCollectionServiceExtensions).Assembly))

4
Gear/Gear/Infrastructure/Wapper/Base/EventBase.cs

@ -1,4 +1,5 @@
using System; using AutoMapper;
using System;
namespace Gear.Wapper.Base namespace Gear.Wapper.Base
{ {
@ -12,7 +13,6 @@ namespace Gear.Wapper.Base
EventId = Guid.NewGuid(); EventId = Guid.NewGuid();
EventCreationDate = DateTime.UtcNow; EventCreationDate = DateTime.UtcNow;
} }
public Guid EventId { get; private set; } public Guid EventId { get; private set; }
public DateTime EventCreationDate { get; private set; } public DateTime EventCreationDate { get; private set; }

Loading…
Cancel
Save