在这一节我们正式将Query整合到架构里面

添加IQuery

我们在FrameworkDesign——Framework文件夹内新建Query文件夹,并在其中新建IQuery文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using FrameWorkDesign;

namespace ShootingEditor2D
{
//注意接口一定要用public修饰,否则会出现访问不一致的问题
public interface IQuery<TResult> : IBelongToArchitecture,ICanSetArchitecture,ICanGetModel,ICanGetSystem
{
TResult Do();
}

public abstract class AbstractQuery<TResult> : IQuery<TResult>
{
private IArchitecture mArchitecture;
IArchitecture IBelongToArchitecture.GetArchitecture()
{
return mArchitecture;
}

void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
{
mArchitecture = architecture;
}
TResult IQuery<TResult>.Do()
{
return OnDo();
}

protected abstract TResult OnDo();

}
}

Architecture文件中支持Query

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using ShootingEditor2D;
using System;
using System.Collections.Generic;

namespace FrameWorkDesign
{
public interface IArchitecture
{
//...

void SendCommand<T>() where T : ICommand, new();
void SendCommand<T>(T command) where T : ICommand;

TResult SendQuery<TResult>(IQuery<TResult> query);//注意写法,泛型的指示<TResult>一定要写在SendQuery后面

//...
}
public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,new()
{
//...

public void SendCommand<T>() where T : ICommand, new()
{
var command = new T();
command.SetArchitecture(this);
command.Execute();
}

public void SendCommand<T>(T command) where T : ICommand
{
command.SetArchitecture(this);
command.Execute();
}

public TResult SendQuery<TResult>(IQuery<TResult> query)
{
query.SetArchitecture(this);
return query.Do();
}

//...

}
}

添加Rule

在FrameworkDesign——Framework——Architecture——Rule中新建ICanSendQuery文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using ShootingEditor2D;

namespace FrameWorkDesign
{
public interface ICanSendQuery : IBelongToArchitecture
{

}
public static class CanSendQueryExtension
{
public static TResult SendQuery<TResult>(this ICanSendQuery self,IQuery<TResult> query)
{
return self.GetArchitecture().SendQuery<TResult>(query);
}
}
}

然后我们给IControllerIQueryICommand继承上面的接口

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
public interface ICommand : IBelongToArchitecture , ICanSetArchitecture , ICanGetUtility , ICanGetModel , ICanGetSystem , ICanSendEvent , ICanSendCommand , ICanSendQuery//
{
void Execute();
}
public interface IQuery<TResult> : IBelongToArchitecture,ICanSetArchitecture,ICanGetModel,ICanGetSystem,ICanSendQuery//
{
TResult Do();
}
public interface IController : IBelongToArchitecture , ICanSendCommand , ICanGetSystem , ICanGetModel , ICanRegisterEvent , ICanSendQuery//
{

}

重构MaxBulletQuery

修改MaxBulletQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using FrameWorkDesign;

namespace ShootingEditor2D
{
public class MaxBulletCountQuery : AbstractQuery<int>
{
private readonly string mGunName;

public MaxBulletCountQuery(string gunName)
{
mGunName = gunName;
}
protected override int OnDo()
{
var gunConfigModel = this.GetModel<IGunConfigModel>();
var gunItem = gunConfigModel.GetItemByName(mGunName);
return gunItem.BulletMaxCount;
}
}
}

修改UIController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using FrameWorkDesign;
using System;

namespace ShootingEditor2D
{
public class UIController : MonoBehaviour,IController
{
//...
private int m_MaxBulletCount;

private void Awake()
{
//...

m_MaxBulletCount = this.SendQuery(new MaxBulletCountQuery(m_GunSystem.CurrentGun.Name.Value));
}

//...


}
}

我们使用SendQuery来统一进行管理,方便以后针对每个Query进行一些拦截或输出