From 51ed593acbdf57969c6af0dacfac3f4b68c604e0 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 16 Feb 2026 20:43:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cqrs):=20=E9=87=8D=E6=9E=84CQRS?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E5=9F=BA=E7=A1=80=E7=BB=84=E4=BB=B6=E5=B9=B6?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A0=B8=E5=BF=83=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将命令相关抽象接口从command目录迁移至cqrs.command目录 - 新增CommandBase、NotificationBase、QueryBase和RequestBase通用基类 - 统一所有CQRS组件的命名空间为GFramework.Core.Abstractions.cqrs - 更新所有引用位置的using语句指向新的命名空间路径 - 为命令和查询输入接口添加IInput基接口继承 - 在测试文件中同步更新相关的引用路径修改 --- GFramework.Core.Abstractions/cqrs/IInput.cs | 20 ++++++++++++ .../{ => cqrs}/command/ICommandInput.cs | 4 +-- .../cqrs/notification/INotificationInput.cs | 20 ++++++++++++ .../{ => cqrs}/query/IQueryInput.cs | 4 +-- .../cqrs/request/IRequestInput.cs | 20 ++++++++++++ .../command/AbstractAsyncCommandTests.cs | 1 + .../command/CommandExecutorTests.cs | 2 +- .../query/AbstractAsyncQueryTests.cs | 1 + .../query/AsyncQueryExecutorTests.cs | 2 +- .../query/QueryExecutorTests.cs | 2 +- .../command/AbstractAsyncCommandWithInput.cs | 1 + .../command/AbstractAsyncCommandWithResult.cs | 1 + .../command/AbstractCommandWithInput.cs | 1 + .../command/AbstractCommandWithResult.cs | 1 + GFramework.Core/command/EmptyCommandInput.cs | 2 +- GFramework.Core/cqrs/command/CommandBase.cs | 32 +++++++++++++++++++ .../cqrs/notification/NotificationBase.cs | 31 ++++++++++++++++++ GFramework.Core/cqrs/query/QueryBase.cs | 32 +++++++++++++++++++ GFramework.Core/cqrs/request/RequestBase.cs | 32 +++++++++++++++++++ .../query/AbstractAsyncQueryWithResult.cs | 3 +- .../query/AbstractQueryWithResult.cs | 3 +- GFramework.Core/query/EmptyQueryInput.cs | 2 +- 22 files changed, 206 insertions(+), 11 deletions(-) create mode 100644 GFramework.Core.Abstractions/cqrs/IInput.cs rename GFramework.Core.Abstractions/{ => cqrs}/command/ICommandInput.cs (64%) create mode 100644 GFramework.Core.Abstractions/cqrs/notification/INotificationInput.cs rename GFramework.Core.Abstractions/{ => cqrs}/query/IQueryInput.cs (51%) create mode 100644 GFramework.Core.Abstractions/cqrs/request/IRequestInput.cs create mode 100644 GFramework.Core/cqrs/command/CommandBase.cs create mode 100644 GFramework.Core/cqrs/notification/NotificationBase.cs create mode 100644 GFramework.Core/cqrs/query/QueryBase.cs create mode 100644 GFramework.Core/cqrs/request/RequestBase.cs diff --git a/GFramework.Core.Abstractions/cqrs/IInput.cs b/GFramework.Core.Abstractions/cqrs/IInput.cs new file mode 100644 index 0000000..18a0789 --- /dev/null +++ b/GFramework.Core.Abstractions/cqrs/IInput.cs @@ -0,0 +1,20 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace GFramework.Core.Abstractions.cqrs; + +/// +/// 表示输入数据的标记接口。 +/// 该接口用于标识各类CQRS模式中的输入参数类型。 +/// +public interface IInput; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/command/ICommandInput.cs b/GFramework.Core.Abstractions/cqrs/command/ICommandInput.cs similarity index 64% rename from GFramework.Core.Abstractions/command/ICommandInput.cs rename to GFramework.Core.Abstractions/cqrs/command/ICommandInput.cs index 7763fa4..97ec861 100644 --- a/GFramework.Core.Abstractions/command/ICommandInput.cs +++ b/GFramework.Core.Abstractions/cqrs/command/ICommandInput.cs @@ -1,7 +1,7 @@ -namespace GFramework.Core.Abstractions.command; +namespace GFramework.Core.Abstractions.cqrs.command; /// /// 命令输入接口,定义命令模式中输入数据的契约 /// 该接口作为标记接口使用,不包含任何成员定义 /// -public interface ICommandInput; \ No newline at end of file +public interface ICommandInput : IInput; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/cqrs/notification/INotificationInput.cs b/GFramework.Core.Abstractions/cqrs/notification/INotificationInput.cs new file mode 100644 index 0000000..05fa05d --- /dev/null +++ b/GFramework.Core.Abstractions/cqrs/notification/INotificationInput.cs @@ -0,0 +1,20 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace GFramework.Core.Abstractions.cqrs.notification; + +/// +/// 表示通知输入数据的标记接口。 +/// 该接口继承自 IInput,用于标识CQRS模式中通知类型的输入参数。 +/// +public interface INotificationInput : IInput; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/query/IQueryInput.cs b/GFramework.Core.Abstractions/cqrs/query/IQueryInput.cs similarity index 51% rename from GFramework.Core.Abstractions/query/IQueryInput.cs rename to GFramework.Core.Abstractions/cqrs/query/IQueryInput.cs index 1aec2b6..1494f36 100644 --- a/GFramework.Core.Abstractions/query/IQueryInput.cs +++ b/GFramework.Core.Abstractions/cqrs/query/IQueryInput.cs @@ -1,6 +1,6 @@ -namespace GFramework.Core.Abstractions.query; +namespace GFramework.Core.Abstractions.cqrs.query; /// /// 查询输入接口,定义了查询操作的输入规范 /// -public interface IQueryInput; \ No newline at end of file +public interface IQueryInput : IInput; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/cqrs/request/IRequestInput.cs b/GFramework.Core.Abstractions/cqrs/request/IRequestInput.cs new file mode 100644 index 0000000..cec8802 --- /dev/null +++ b/GFramework.Core.Abstractions/cqrs/request/IRequestInput.cs @@ -0,0 +1,20 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace GFramework.Core.Abstractions.cqrs.request; + +/// +/// 表示请求输入数据的标记接口。 +/// 该接口继承自 IInput,用于标识CQRS模式中请求类型的输入参数。 +/// +public interface IRequestInput : IInput; \ No newline at end of file diff --git a/GFramework.Core.Tests/command/AbstractAsyncCommandTests.cs b/GFramework.Core.Tests/command/AbstractAsyncCommandTests.cs index 6f445f1..c2e3375 100644 --- a/GFramework.Core.Tests/command/AbstractAsyncCommandTests.cs +++ b/GFramework.Core.Tests/command/AbstractAsyncCommandTests.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.Abstractions.rule; using GFramework.Core.architecture; using GFramework.Core.command; diff --git a/GFramework.Core.Tests/command/CommandExecutorTests.cs b/GFramework.Core.Tests/command/CommandExecutorTests.cs index 908e258..ae9b39b 100644 --- a/GFramework.Core.Tests/command/CommandExecutorTests.cs +++ b/GFramework.Core.Tests/command/CommandExecutorTests.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.command; using NUnit.Framework; diff --git a/GFramework.Core.Tests/query/AbstractAsyncQueryTests.cs b/GFramework.Core.Tests/query/AbstractAsyncQueryTests.cs index 0019970..40e5333 100644 --- a/GFramework.Core.Tests/query/AbstractAsyncQueryTests.cs +++ b/GFramework.Core.Tests/query/AbstractAsyncQueryTests.cs @@ -1,3 +1,4 @@ +using GFramework.Core.Abstractions.cqrs.query; using GFramework.Core.Abstractions.query; using GFramework.Core.Abstractions.rule; using GFramework.Core.architecture; diff --git a/GFramework.Core.Tests/query/AsyncQueryExecutorTests.cs b/GFramework.Core.Tests/query/AsyncQueryExecutorTests.cs index f63cbdd..a3cfb1c 100644 --- a/GFramework.Core.Tests/query/AsyncQueryExecutorTests.cs +++ b/GFramework.Core.Tests/query/AsyncQueryExecutorTests.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.cqrs.query; using GFramework.Core.query; using NUnit.Framework; diff --git a/GFramework.Core.Tests/query/QueryExecutorTests.cs b/GFramework.Core.Tests/query/QueryExecutorTests.cs index 06a2770..d94dea6 100644 --- a/GFramework.Core.Tests/query/QueryExecutorTests.cs +++ b/GFramework.Core.Tests/query/QueryExecutorTests.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.cqrs.query; using GFramework.Core.query; using NUnit.Framework; diff --git a/GFramework.Core/command/AbstractAsyncCommandWithInput.cs b/GFramework.Core/command/AbstractAsyncCommandWithInput.cs index 32017b9..0719b86 100644 --- a/GFramework.Core/command/AbstractAsyncCommandWithInput.cs +++ b/GFramework.Core/command/AbstractAsyncCommandWithInput.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.rule; namespace GFramework.Core.command; diff --git a/GFramework.Core/command/AbstractAsyncCommandWithResult.cs b/GFramework.Core/command/AbstractAsyncCommandWithResult.cs index 2f0b3e8..bd22233 100644 --- a/GFramework.Core/command/AbstractAsyncCommandWithResult.cs +++ b/GFramework.Core/command/AbstractAsyncCommandWithResult.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.rule; namespace GFramework.Core.command; diff --git a/GFramework.Core/command/AbstractCommandWithInput.cs b/GFramework.Core/command/AbstractCommandWithInput.cs index 01de19b..4836b14 100644 --- a/GFramework.Core/command/AbstractCommandWithInput.cs +++ b/GFramework.Core/command/AbstractCommandWithInput.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.rule; namespace GFramework.Core.command; diff --git a/GFramework.Core/command/AbstractCommandWithResult.cs b/GFramework.Core/command/AbstractCommandWithResult.cs index da49177..b93b32e 100644 --- a/GFramework.Core/command/AbstractCommandWithResult.cs +++ b/GFramework.Core/command/AbstractCommandWithResult.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; using GFramework.Core.rule; namespace GFramework.Core.command; diff --git a/GFramework.Core/command/EmptyCommandInput.cs b/GFramework.Core/command/EmptyCommandInput.cs index fc47ce2..e510964 100644 --- a/GFramework.Core/command/EmptyCommandInput.cs +++ b/GFramework.Core/command/EmptyCommandInput.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.cqrs.command; namespace GFramework.Core.command; diff --git a/GFramework.Core/cqrs/command/CommandBase.cs b/GFramework.Core/cqrs/command/CommandBase.cs new file mode 100644 index 0000000..781b08f --- /dev/null +++ b/GFramework.Core/cqrs/command/CommandBase.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using GFramework.Core.Abstractions.cqrs.command; +using Mediator; + +namespace GFramework.Core.cqrs.command; + +/// +/// 表示一个基础命令类,用于处理带有输入和响应的命令模式实现。 +/// 该类实现了 ICommand<TResponse> 接口,提供了通用的命令结构。 +/// +/// 命令输入数据的类型 +/// 命令执行后返回结果的类型 +/// 命令执行所需的输入数据 +public class CommandBase(TInput input) : ICommand where TInput : ICommandInput +{ + /// + /// 获取命令的输入数据。 + /// + public TInput Input => input; +} \ No newline at end of file diff --git a/GFramework.Core/cqrs/notification/NotificationBase.cs b/GFramework.Core/cqrs/notification/NotificationBase.cs new file mode 100644 index 0000000..fe976a8 --- /dev/null +++ b/GFramework.Core/cqrs/notification/NotificationBase.cs @@ -0,0 +1,31 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using GFramework.Core.Abstractions.cqrs.notification; +using Mediator; + +namespace GFramework.Core.cqrs.notification; + +/// +/// 表示一个基础通知类,用于处理带有输入的通知模式实现。 +/// 该类实现了 INotification 接口,提供了通用的通知结构。 +/// +/// 通知输入数据的类型,必须实现 INotificationInput 接口 +/// 通知执行所需的输入数据 +public class NotificationBase(TInput input) : INotification where TInput : INotificationInput +{ + /// + /// 获取通知的输入数据。 + /// + public TInput Input => input; +} \ No newline at end of file diff --git a/GFramework.Core/cqrs/query/QueryBase.cs b/GFramework.Core/cqrs/query/QueryBase.cs new file mode 100644 index 0000000..77d4b0f --- /dev/null +++ b/GFramework.Core/cqrs/query/QueryBase.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using GFramework.Core.Abstractions.cqrs.query; +using Mediator; + +namespace GFramework.Core.cqrs.query; + +/// +/// 表示一个基础查询类,用于处理带有输入和响应的查询模式实现。 +/// 该类继承自 Mediator.IQuery<TResponse> 接口,提供了通用的查询结构。 +/// +/// 查询输入数据的类型,必须实现 IQueryInput 接口 +/// 查询执行后返回结果的类型 +/// 查询执行所需的输入数据 +public class QueryBase(TInput input) : IQuery where TInput : IQueryInput +{ + /// + /// 获取查询的输入数据。 + /// + public TInput Input => input; +} \ No newline at end of file diff --git a/GFramework.Core/cqrs/request/RequestBase.cs b/GFramework.Core/cqrs/request/RequestBase.cs new file mode 100644 index 0000000..9143c8d --- /dev/null +++ b/GFramework.Core/cqrs/request/RequestBase.cs @@ -0,0 +1,32 @@ +// Copyright (c) 2026 GeWuYou +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using GFramework.Core.Abstractions.cqrs.request; +using Mediator; + +namespace GFramework.Core.cqrs.request; + +/// +/// 表示一个基础请求类,用于处理带有输入和响应的请求模式实现。 +/// 该类实现了 IRequest<TResponse> 接口,提供了通用的请求结构。 +/// +/// 请求输入数据的类型,必须实现 IRequestInput 接口 +/// 请求执行后返回结果的类型 +/// 请求执行所需的输入数据 +public class RequestBase(TInput input) : IRequest where TInput : IRequestInput +{ + /// + /// 获取请求的输入数据。 + /// + public TInput Input => input; +} \ No newline at end of file diff --git a/GFramework.Core/query/AbstractAsyncQueryWithResult.cs b/GFramework.Core/query/AbstractAsyncQueryWithResult.cs index 3020ef9..8799149 100644 --- a/GFramework.Core/query/AbstractAsyncQueryWithResult.cs +++ b/GFramework.Core/query/AbstractAsyncQueryWithResult.cs @@ -1,4 +1,5 @@ -using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.cqrs.query; +using GFramework.Core.Abstractions.query; using GFramework.Core.rule; namespace GFramework.Core.query; diff --git a/GFramework.Core/query/AbstractQueryWithResult.cs b/GFramework.Core/query/AbstractQueryWithResult.cs index 1f30940..b142fe4 100644 --- a/GFramework.Core/query/AbstractQueryWithResult.cs +++ b/GFramework.Core/query/AbstractQueryWithResult.cs @@ -1,4 +1,5 @@ -using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.cqrs.query; +using GFramework.Core.Abstractions.query; using GFramework.Core.rule; namespace GFramework.Core.query; diff --git a/GFramework.Core/query/EmptyQueryInput.cs b/GFramework.Core/query/EmptyQueryInput.cs index 80d9eea..57c0fe8 100644 --- a/GFramework.Core/query/EmptyQueryInput.cs +++ b/GFramework.Core/query/EmptyQueryInput.cs @@ -1,4 +1,4 @@ -using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.cqrs.query; namespace GFramework.Core.query;