wanshenmean
2026-03-11 5af11cc200dd5ebe474b9c0475883b0e6d1e3759
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
// 设备仓储接口