fastCSharp代碼生成的底層應(yīng)用框架
fastCSharp是一個(gè)基于.NET元數(shù)據(jù)的代碼生成的底層應(yīng)用框架,目標(biāo)是打造一個(gè)“開(kāi)發(fā)+運(yùn)行”效率雙優(yōu)的開(kāi)源框架。
經(jīng)過(guò)半年多的時(shí)間,除了與web開(kāi)發(fā)直接相關(guān)的部分,都已經(jīng)在fastCSharp part 1.5中完成了重寫(xiě)工作。
fastCSharp現(xiàn)在實(shí)現(xiàn)的代碼生成實(shí)例主要有5個(gè):
1、基于緩存查詢模式的ORM代碼生成實(shí)例(現(xiàn)在只支持MSSQL),自定義配置類(lèi)是fastCSharp.setup.cSharp.sqlTable,同時(shí)支持反射模式fastCSharp.setup.cSharp.sqlTable.sqlTool。
下面是ORM的model定義示例
|
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
45
46
47
48
49
|
[fastCSharp.setup.cSharp.sqlTable(ConnectionName = "Connection1")]
public partial class model1
{
///
/// 自增列,一個(gè)表格只允許一個(gè),如果不配置IsIdentity = true,將自動(dòng)匹配名稱為 id 的成員
///
[fastCSharp.setup.sqlMember(IsIdentity = true)]
public int id;
///
/// 關(guān)鍵字1,多個(gè)關(guān)鍵字成員按照成員定義順序一致
///
[fastCSharp.setup.sqlMember(IsPrimaryKey = true)]
public int key1;
///
/// 關(guān)鍵字2
///
[fastCSharp.setup.sqlMember(IsPrimaryKey = true, IsAscii = true, MaxLength = 32)]
public string key2;
public enum EnumByte : byte
{
Enum1
}
///
/// 直接支持枚舉類(lèi)型轉(zhuǎn)換,可以不指定SqlType = typeof(byte)
///
[fastCSharp.setup.sqlMember(SqlType = typeof(byte))]
public EnumByte key2;
///
/// 指定隱式類(lèi)型轉(zhuǎn)換
///
[fastCSharp.setup.sqlMember(SqlType = typeof(string))]
public partial struct image
{
public string url;
///
/// 如果不能隱式類(lèi)型轉(zhuǎn)換,必須實(shí)現(xiàn)互轉(zhuǎn)函數(shù)
///
public static implicit operator image(string url) { return new image { url = url }; }
public static implicit operator string(image image) { return image.url; }
}
///
/// 支持隱式類(lèi)型轉(zhuǎn)換
///
[fastCSharp.setup.sqlMember(IsAscii = true, MaxLength = 64)]
public image icon = string.Empty;
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
{
sql: {
checkConnection:["Connection1"],
Connection1:
{
Diantou:{
Type:"sql2008",
Connection:"server=192.168.0.100;database=dbname;uid=username;pwd=password"
}
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public partial class bModel1 : model1.sqlTable
{
static bModel1()
{
if (SqlTool != null)
{
//定義緩存
Cache = new fastCSharp.sql.cache.whole.identityArray(SqlTool);
//定義緩存同步個(gè)性化處理事件
Cache.OnInserted += XXX;
Cache.OnUpdated += XXX;
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
|
public abstract class bModelBase : model1.sqlTable
where tableType : bModelBase
{
}
public partial class bModel1_1 : bModelBase
{
}
public partial class bModel1_N : bModelBase
{
}
|
|
1
2
3
4
5
6
7
8
9
10
11
|
public class bModel1 : model1
{
private static readonly fastCSharp.setup.cSharp.sqlTable.sqlTool sqlTool = fastCSharp.setup.cSharp.sqlTable.sqlTool.Default;
static bModel1()
{
if (sqlTool != null)
{
//緩存定義與事件定義 和 代碼生成模式示例一樣
}
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
return diantou.dataProxy.questionTopic.getTopicCache(id)
.getArray(value => diantou.dataProxy.question.get(value.linkId))
.getHash(value => value.bestAnswerId)
.getArray(value => diantou.dataProxy.answer.get(value))
.getFind(value => value != null)
.group(value => value.userId)
.getArray(value => new keyValue(diantou.dataProxy.user.get(value.Key), value.Value.Count))
.group(value => value.Key.grade0)
.getArray(value => new userStat
{
grade0 = value.Key,
count = value.Value.Count,
path = topic.path.bestAnswer,
users = value.Value.rangeSort((left, right) => right.Value - left.Value, 0, 6)
.getArray(user => diantou.dataProxy.user.get(user.Key, userId))
});
|
我個(gè)人認(rèn)為基于ORM的查詢更簡(jiǎn)單流暢。如果用SQL實(shí)現(xiàn)這個(gè)需求,該是一個(gè)多么復(fù)雜的SQL語(yǔ)句?如果需求有一點(diǎn)點(diǎn)變化,修改這個(gè)SQL語(yǔ)句該是多麻煩的事?
2、數(shù)據(jù)類(lèi)快速序列化代碼生成實(shí)例,自定義配置類(lèi)是fastCSharp.setup.cSharp.serialize,同時(shí)支持反射模式。
|
1
2
3
4
5
6
7
|
///
/// 僅僅選擇字段成員,反射模式可以不必配置
///
[fastCSharp.setup.cSharp.serialize(Filter = fastCSharp.setup.memberFilter.InstanceField)]
public partial class model1
{
}
|
|
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
|
///
/// 序列化接口
///
public interface ISerialize
{
///
/// 對(duì)象序列化
///
/// 序列化數(shù)據(jù)
byte[] Serialize();
///
/// 對(duì)象序列化
///
/// 數(shù)據(jù)流
void Serialize(memoryStream stream);
///
/// 對(duì)象序列化
///
/// 對(duì)象序列化器
void Serialize(dataSerializer serializer);
///
/// 反序列化
///
/// 序列化數(shù)據(jù)
bool DeSerialize(byte[] data);
///
/// 反序列化
///
/// 序列化數(shù)據(jù)
/// 起始位置
/// 結(jié)束位置
bool DeSerialize(byte[] data, int startIndex, out int endIndex);
///
/// 反序列化
///
/// 對(duì)象反序列化器
void DeSerialize(deSerializer deSerializer);
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
///
/// 序列化流接口
///
public interface IStreamSerialize : ISerialize
{
///
/// 對(duì)象序列化
///
/// 數(shù)據(jù)流
void Serialize(Stream stream);
///
/// 對(duì)象序列化
///
/// 對(duì)象序列化器
void Serialize(streamSerializer serializer);
}
|
|
1
2
3
|
fastCSharp.setup.cSharp.serialize.dataSerialize.Get(value);
fastCSharp.setup.cSharp.serialize.streamSerialize.Get(value);
fastCSharp.setup.cSharp.serialize.deSerialize.Get(data);
|
3、TCP(靜態(tài)方法)調(diào)用代碼生成實(shí)例,自定義配置類(lèi)是fastCSharp.setup.cSharp.tcpCall,支持泛型,支持跨類(lèi)(只能支持單例),不支持反射模式。下面是示例:
|
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
|
///
/// 序列化流接口
///
[fastCSharp.setup.cSharp.tcpCall(Service = "tcpCallVerifyAsynchronous", VerifyType = typeof(tcpVerifyAsynchronous))]
public partial class tcpCallVerifyAsynchronous
{
[fastCSharp.setup.cSharp.tcpCall(IsClientAsynchronous = false)]
protected static void action()
{
isServer = true;
}
[fastCSharp.setup.cSharp.tcpCall(IsClientAsynchronous = true)]
protected static void actionAsynchronous()
{
isServer = true;
}
[fastCSharp.setup.testCase]
internal static bool Test()
{
using (fastCSharp.testCase.tcpServer.tcpCallVerifyAsynchronous server = new fastCSharp.testCase.tcpServer.tcpCallVerifyAsynchronous())
{
if (!server.Start()) throw new Exception("tcpCallVerifyAsynchronous");
if (fastCSharp.testCase.tcpClient.tcpCallVerifyAsynchronous.defaultTcpServer.IsServer) throw new Exception("IsServer");
isServer = false;
tcpCall.tcpCallVerifyAsynchronous.action();
checkServer("action");
isServer = false;
isReturn = null;
tcpCall.tcpCallVerifyAsynchronous.actionAsynchronous(actionAsynchronousReturn);
checkIsReturn("actionAsynchronous");
}
return true;
}
}
|
4、TCP(動(dòng)態(tài)方法)服務(wù)代碼生成實(shí)例,自定義配置類(lèi)是fastCSharp.setup.cSharp.tcpServer,支持泛型,不支持跨類(lèi),不支持反射模式。下面是示例:
|
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
|
[fastCSharp.setup.cSharp.tcpServer(Service = "tcpServerVerifyAsynchronous", VerifyType = typeof(tcpVerifyAsynchronous))]
public partial class tcpServerVerifyAsynchronous
{
[fastCSharp.setup.cSharp.tcpServer(IsClientAsynchronous = false)]
protected void action()
{
isServer = true;
}
[fastCSharp.setup.cSharp.tcpServer(IsClientAsynchronous = true)]
protected void actionAsynchronous()
{
isServer = true;
}
[fastCSharp.setup.testCase]
internal static bool Test()
{
using (fastCSharp.testCase.tcpServer.tcpServerVerifyAsynchronous server = new fastCSharp.testCase.tcpServer.tcpServerVerifyAsynchronous())
{
if (!server.Start()) throw new Exception("tcpServerVerifyAsynchronous");
using (fastCSharp.testCase.tcpClient.tcpServerVerifyAsynchronous client = new fastCSharp.testCase.tcpClient.tcpServerVerifyAsynchronous())
{
isServer = false;
client.action();
checkServer("action");
isServer = false;
isReturn = null;
client.actionAsynchronous(actionAsynchronousReturn);
checkIsReturn("actionAsynchronous");
}
}
return true;
}
}
|
5、快速json處理代碼生成實(shí)例,自定義配置類(lèi)是fastCSharp.setup.cSharp.ajax,同時(shí)支持反射模式。
|
1
2
3
4
5
6
7
|
///
/// 可以選擇只生成 序列化 或者 反序列化 的代碼,反射模式可以不必配置
///
[fastCSharp.setup.cSharp.ajax(IsToJson = true, IsParseJson = true)]
public partial class model1
{
}
|
代碼生成模式將實(shí)現(xiàn)接口fastCSharp.setup.cSharp.ajax.IToJson與fastCSharp.setup.cSharp.ajax.IParseJson
|
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
|
///
/// 對(duì)象轉(zhuǎn)換成JSON字符串接口
///
public interface IToJson
{
///
/// 對(duì)象轉(zhuǎn)換成JSON字符串
///
/// JSON字符串
string ToJson();
///
/// 對(duì)象轉(zhuǎn)換成JSON字符串
///
/// 對(duì)象轉(zhuǎn)換JSON字符串
void ToJson(toJsoner toJsoner);
}
///
/// JSON字符串轉(zhuǎn)換成對(duì)象接口
///
public interface IParseJson
{
///
/// JSON字符串轉(zhuǎn)換成對(duì)象
///
/// JSON字符串
void FromJson(string json);
///
/// JSON字符串解析節(jié)點(diǎn)換成對(duì)象
///
/// JSON字符串解析節(jié)點(diǎn)
void FromJson(jsonNode node);
}
|
反射模式的話,直接調(diào)用反射函數(shù)
|
1
2
|
fastCSharp.setup.cSharp.ajax.toJson.Get(value);
fastCSharp.setup.cSharp.ajax.parseJson.Get(json);
|
由于很多人不需要代碼生成那么好的運(yùn)行效率,也不想配置初始環(huán)境,所以某些代碼生成實(shí)例提供了基于反射的實(shí)例實(shí)現(xiàn)。有的代碼生成實(shí)例需要生成代理實(shí)例(比如TCP調(diào)用),沒(méi)有辦法使用反射實(shí)現(xiàn)相同的效果。
今天是我在現(xiàn)在這個(gè)公司工作的最后一天了,感謝李陶冶大牛這幾年對(duì)我的照顧,感謝你對(duì)于這個(gè)項(xiàng)目開(kāi)源發(fā)展的支持。也許過(guò)幾天我就回老家“休息”了,在這里默默的祝福你和51nod能夠一切順利。
最后歡迎對(duì)算法有興趣的朋友到基于fastCSharp開(kāi)發(fā)的51nod OJ上去AC問(wèn)題或者討論算法問(wèn)題。
評(píng)論
圖片
表情
