Skip to content

Commit

Permalink
加上注释
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Oct 1, 2023
1 parent a9d563d commit 2b33f53
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/LightWorkFlowManager/Contexts/IWorkerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@

namespace DC.LightWorkFlowManager.Contexts;

/// <summary>
/// 工作器上下文信息
/// </summary>
/// 上下文信息是带有数据的,基本上只和当前的工作过程有关的数据。和依赖注入的服务是两个不同的方向,这里的上下文信息更多的是保存一些和当前正在工作的过程有关的数据,多个不同的任务的数据不互通,相互是分开的
public interface IWorkerContext
{
/// <summary>
/// 获取上下文信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>如果获取不到,返回空</returns>
T? GetContext<T>();

/// <summary>
/// 设置上下文信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
void SetContext<T>(T context);
}

/// <summary>
/// 工作器上下文信息扩展类型
/// </summary>
public static class MessageContextExtension
{
/// <summary>
/// 获取一定存在的上下文信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="workerContext"></param>
/// <returns></returns>
/// <exception cref="WorkerContextNotFoundException">如果上下文信息不存在,就抛出异常</exception>
public static T GetEnsureContext<T>(this IWorkerContext workerContext)
{
var context = workerContext.GetContext<T>();
Expand Down
32 changes: 32 additions & 0 deletions src/LightWorkFlowManager/Contexts/WorkFlowErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public WorkFlowErrorCode(int code, string message)
/// </summary>
public string Message { get; }

/// <summary>
/// 表示成功
/// </summary>
public static WorkFlowErrorCode Ok => new WorkFlowErrorCode(0, "Ok");

/// <summary>
/// 追加信息
/// </summary>
/// <param name="appendMessage"></param>
/// <returns></returns>
public WorkFlowErrorCode AppendMessage(string? appendMessage)
{
if (appendMessage == null)
Expand All @@ -44,11 +52,19 @@ public WorkFlowErrorCode AppendMessage(string? appendMessage)
}
}

/// <summary>
/// 隐式转换为 int 类型
/// </summary>
/// <param name="code"></param>
public static implicit operator int(WorkFlowErrorCode code)
{
return code.Code;
}

/// <summary>
/// 从 int 类型隐式转换为错误信息
/// </summary>
/// <param name="code"></param>
public static implicit operator WorkFlowErrorCode(int code)
{
if (ErrorCodeDictionary.TryGetValue(code, out var value))
Expand All @@ -64,31 +80,47 @@ public static implicit operator WorkFlowErrorCode(int code)
return new WorkFlowErrorCode(code, string.Empty);
}

/// <inheritdoc />
public override string ToString() => $"{Code} {Message}";

private static readonly ConcurrentDictionary<int, WorkFlowErrorCode> ErrorCodeDictionary =
new ConcurrentDictionary<int, WorkFlowErrorCode>();

/// <inheritdoc />
public bool Equals(WorkFlowErrorCode other)
{
return Code == other.Code;
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is WorkFlowErrorCode other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
return Code;
}

/// <summary>
/// 判断相等
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator ==(WorkFlowErrorCode left, WorkFlowErrorCode right)
{
return left.Equals(right);
}

/// <summary>
/// 判断不相等
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator !=(WorkFlowErrorCode left, WorkFlowErrorCode right)
{
return !left.Equals(right);
Expand Down
3 changes: 3 additions & 0 deletions src/LightWorkFlowManager/Exceptions/IWorkFlowException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace DC.LightWorkFlowManager.Exceptions;

/// <summary>
/// 表示当前是一个工作过程的异常
/// </summary>
public interface IWorkFlowException
{
}

0 comments on commit 2b33f53

Please sign in to comment.