From 5af11cc200dd5ebe474b9c0475883b0e6d1e3759 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 11 三月 2026 10:00:49 +0800
Subject: [PATCH] 重构整个项目:改进代码质量和架构

---
 Code/WCS/WIDESEAWCS_Server/docs/superpowers/specs/2026-03-11-wcs-ddd-refactor-design.md |   98 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/docs/superpowers/specs/2026-03-11-wcs-ddd-refactor-design.md b/Code/WCS/WIDESEAWCS_Server/docs/superpowers/specs/2026-03-11-wcs-ddd-refactor-design.md
index 2dfea6f..881505e 100644
--- a/Code/WCS/WIDESEAWCS_Server/docs/superpowers/specs/2026-03-11-wcs-ddd-refactor-design.md
+++ b/Code/WCS/WIDESEAWCS_Server/docs/superpowers/specs/2026-03-11-wcs-ddd-refactor-design.md
@@ -79,6 +79,15 @@
     // 棰嗗煙浜嬩欢
     private List<IDomainEvent> _domainEvents = new();
 
+    // 鍏叡灞炴�ц闂櫒
+    public DeviceId Id => _id;
+    public DeviceName Name => _name;
+    public DeviceType Type => _type;
+    public DeviceStatus Status => _status;
+    public DeviceAddress Address => _address;
+    public DateTime LastConnectedAt => _lastConnectedAt;
+    public DateTime LastHeartbeatAt => _lastHeartbeatAt;
+
     // 琛屼负鏂规硶
     public void Connect()
     {
@@ -112,6 +121,12 @@
             _properties.Add(DeviceProperty.Create(key, value));
     }
 
+    // 鐘舵�佽缃柟娉曪紙渚涚姸鎬佹満浣跨敤锛�
+    internal void SetStatus(DeviceStatus status)
+    {
+        _status = status;
+    }
+
     public IReadOnlyCollection<IDomainEvent> GetDomainEvents() => _domainEvents.AsReadOnly();
     public void ClearDomainEvents() => _domainEvents.Clear();
 }
@@ -124,7 +139,9 @@
     private List<DeviceId> _deviceIds;
     private GroupStrategy _strategy;
     private int _currentIndex;
+    private static readonly Random _random = Random.Shared;
 
+// 璁惧缁勮仛鍚堟牴锛堢户缁級
     public void AddDevice(DeviceId deviceId)
     {
         if (_deviceIds.Contains(deviceId))
@@ -148,7 +165,7 @@
             case GroupStrategy.RoundRobin:
                 return _deviceIds[_currentIndex++ % _deviceIds.Count];
             case GroupStrategy.Random:
-                return _deviceIds[new Random().Next(_deviceIds.Count)];
+                return _deviceIds[_random.Next(_deviceIds.Count)];
             default:
                 return _deviceIds[0];
         }
@@ -342,7 +359,84 @@
 }
 ```
 
-### 2.4 浠撳偍鎺ュ彛璁捐
+### 2.4 鍩虹璁炬柦绫诲畾涔�
+
+```csharp
+// 鑱氬悎鏍瑰熀绫�
+public abstract class AggregateRoot<TId>
+{
+    public TId Id { get; protected set; }
+    public int Version { get; private set; }
+    private readonly List<IDomainEvent> _domainEvents = new();
+
+    protected AggregateRoot() { }
+
+    protected AggregateRoot(TId id)
+    {
+        Id = id;
+    }
+
+    public IReadOnlyCollection<IDomainEvent> GetDomainEvents() => _domainEvents.AsReadOnly();
+    public void ClearDomainEvents() => _domainEvents.Clear();
+
+    protected void AddDomainEvent(IDomainEvent domainEvent)
+    {
+        _domainEvents.Add(domainEvent);
+    }
+}
+
+// 棰嗗煙浜嬩欢鎺ュ彛
+public interface IDomainEvent
+{
+    DateTime OccurredOn { get; }
+}
+
+// 棰嗗煙浜嬩欢鍩虹被
+public abstract record DomainEvent : IDomainEvent
+{
+    public DateTime OccurredOn { get; init; } = DateTime.UtcNow;
+}
+
+// 棰嗗煙浜嬩欢璋冨害鍣ㄦ帴鍙�
+public interface IDomainEventDispatcher
+{
+    Task Dispatch(IDomainEvent domainEvent);
+    Task DispatchAsync(IEnumerable<IDomainEvent> domainEvents);
+}
+
+// 浠诲姟闃熷垪鎺ュ彛
+public interface ITaskQueue
+{
+    Task Enqueue<T>(T item) where T : class;
+    Task<T> Dequeue<T>() where T : class;
+    Task<int> Count();
+}
+
+// 浠撳偍鍩烘帴鍙�
+public interface IRepository<TEntity, TId> where TEntity : AggregateRoot<TId>
+{
+    Task<TEntity?> GetById(TId id);
+    Task Add(TEntity entity);
+    Task Update(TEntity entity);
+    Task Delete(TId id);
+}
+
+// 宸ヤ綔鍗曞厓鎺ュ彛
+public interface IUnitOfWork : IDisposable
+{
+    ITransaction BeginTransaction();
+    Task Commit();
+    Task Rollback();
+}
+
+public interface ITransaction : IDisposable
+{
+    Task Commit();
+    Task Rollback();
+}
+```
+
+### 2.5 浠撳偍鎺ュ彛璁捐
 
 ```csharp
 // 璁惧浠撳偍鎺ュ彛

--
Gitblit v1.9.3