mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-06 16:16:44 +08:00
- 更新入口页的 reader-facing 骨架,统一起步路线、阅读顺序与站内导航 - 收口公开 README 与 Godot 页面中的内部口吻、文件名式表述和术语噪音 - 移除 docs/zh-CN 中残留的 GitHub README 外链,并同步刷新文档治理恢复状态
111 lines
2.7 KiB
Markdown
111 lines
2.7 KiB
Markdown
---
|
||
title: 快速开始
|
||
description: 通过只依赖 Core 的最小示例快速跑通 GFramework 基础架构。
|
||
---
|
||
|
||
# 快速开始
|
||
|
||
本页给出一个只依赖 `Core` 的最小路径,用来确认你已经成功接入 `Architecture`、`Model`、`System` 与旧版命令执行器。
|
||
|
||
> 说明:当前仓库同时存在旧版 `Command` / `Query` 执行器与新版 `CQRS` runtime。本页先用最短路径说明基础架构如何跑起来;新功能优先采用 [CQRS 运行时](../core/cqrs.md)。
|
||
|
||
## 1. 安装最小依赖
|
||
|
||
```bash
|
||
dotnet add package GeWuYou.GFramework.Core
|
||
dotnet add package GeWuYou.GFramework.Core.Abstractions
|
||
```
|
||
|
||
## 2. 定义一个最小架构
|
||
|
||
```csharp
|
||
using GFramework.Core.Architectures;
|
||
|
||
public sealed class CounterArchitecture : Architecture
|
||
{
|
||
protected override void OnInitialize()
|
||
{
|
||
RegisterModel(new CounterModel());
|
||
RegisterSystem(new CounterSystem());
|
||
}
|
||
}
|
||
```
|
||
|
||
这里要点只有两个:
|
||
|
||
- 架构入口是 `Architecture`
|
||
- 当前版本使用 `protected override void OnInitialize()` 注册模型、系统和工具
|
||
|
||
## 3. 定义一个模型
|
||
|
||
```csharp
|
||
using GFramework.Core.Model;
|
||
using GFramework.Core.Property;
|
||
|
||
public sealed class CounterModel : AbstractModel
|
||
{
|
||
public BindableProperty<int> Count { get; } = new(0);
|
||
|
||
protected override void OnInit()
|
||
{
|
||
}
|
||
}
|
||
```
|
||
|
||
`BindableProperty<T>` 适合承载可观察状态;如果你只需要一个最小例子,保持 `OnInit()` 为空即可。
|
||
|
||
## 4. 定义一个系统
|
||
|
||
```csharp
|
||
using GFramework.Core.Systems;
|
||
|
||
public sealed class CounterSystem : AbstractSystem
|
||
{
|
||
protected override void OnInit()
|
||
{
|
||
}
|
||
}
|
||
```
|
||
|
||
## 5. 定义一个命令
|
||
|
||
```csharp
|
||
using GFramework.Core.Command;
|
||
|
||
public sealed class IncreaseCountCommand : AbstractCommand
|
||
{
|
||
protected override void OnExecute()
|
||
{
|
||
var model = GetModel<CounterModel>();
|
||
model.Count.Value += 1;
|
||
}
|
||
}
|
||
```
|
||
|
||
`AbstractCommand` 继承自 `ContextAwareBase`,所以命令内部可以直接访问 `GetModel<T>()`、`GetSystem<T>()` 等上下文方法。
|
||
|
||
## 6. 初始化并执行
|
||
|
||
```csharp
|
||
var architecture = new CounterArchitecture();
|
||
architecture.Initialize();
|
||
|
||
architecture.Context.SendCommand(new IncreaseCountCommand());
|
||
|
||
var count = architecture.Context.GetModel<CounterModel>().Count.Value;
|
||
Console.WriteLine(count); // 1
|
||
```
|
||
|
||
如果你能走通这一步,说明以下链路已经成立:
|
||
|
||
- 架构初始化
|
||
- 模型 / 系统注册
|
||
- 上下文访问
|
||
- 旧版命令执行
|
||
|
||
## 下一步
|
||
|
||
- 新功能的请求 / 通知处理:看 [CQRS 运行时](../core/cqrs.md)
|
||
- 游戏层能力与接入顺序:看 [Game 模块总览](../game/index.md)
|
||
- 包目录与最小安装组合:看 [安装配置](./installation.md)
|