- Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExtensions.cs
More file actions
Latest commit
86 lines (75 loc) · 6.64 KB
/
Copy pathExtensions.cs
File metadata and controls
86 lines (75 loc) · 6.64 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
usingSystem;
usingSystem.Linq;
usingSystem.Reflection;
usingMicrosoft.AspNetCore.Builder;
usingNETCoreSyncServer;
namespaceMicrosoft.Extensions.DependencyInjection
{
publicstaticclassNETCoreSyncServerServiceCollectionExtensions
{
publicstaticIServiceCollectionAddNETCoreSyncServer(thisIServiceCollectionservices,SyncEvent?syncEvent=null)
{
Assembly?assembly=Assembly.GetEntryAssembly();
if(assembly==null)thrownewNETCoreSyncServerException("Unexpected null return value from Assembly.GetEntryAssembly(), are you calling from unmanaged code?");
returnAddNETCoreSyncServer(services,new[]{(Assembly)assembly},syncEvent);
}
publicstaticIServiceCollectionAddNETCoreSyncServer(thisIServiceCollectionservices,Assembly[]assemblies,SyncEvent?syncEvent=null)
{
Type[]types=assemblies.SelectMany(sm =>sm.GetTypes()).Where(w =>Attribute.IsDefined(w,typeof(SyncTableAttribute))).ToArray();
returnAddNETCoreSyncServer(services,types,syncEvent);
}
publicstaticIServiceCollectionAddNETCoreSyncServer(thisIServiceCollectionservices,Type[]types,SyncEvent?syncEvent=null)
{
if(types.Length==0)thrownewNETCoreSyncServerException("Cannot find classes annotated with SyncTableAttribute");
Type[]invalidTypes=types.Where(w =>!Attribute.IsDefined(w,typeof(SyncTableAttribute))).ToArray();
if(invalidTypes.Length>0)thrownewNETCoreSyncServerException($"These classes are invalid: {string.Join(", ",invalidTypes.Select(s =>s.FullName).ToArray())}. To continue, they should be annotated with SyncTableAttribute");
SyncServicesyncService=newSyncService();
syncService.Types=types.OrderBy(o =>o.GetCustomAttribute<SyncTableAttribute>()!.Order).ToList();
for(inti=0;i<syncService.Types.Count;i++)
{
Typetype=types[i];
TableInfotableInfo=newTableInfo();
syncService.TableInfos[type]=tableInfo;
tableInfo.SyncTable=type.GetCustomAttribute<SyncTableAttribute>()!;
if(string.IsNullOrEmpty(tableInfo.SyncTable.ClientClassName))tableInfo.SyncTable.ClientClassName=type.Name;
PropertyInfo?propertyInfoID=type.GetProperties().Where(w =>w.GetCustomAttribute<SyncPropertyAttribute>()!=null&&w.GetCustomAttribute<SyncPropertyAttribute>()!.PropertyIndicator==SyncPropertyAttribute.PropertyIndicatorEnum.ID).FirstOrDefault();
if(propertyInfoID==null)thrownewNETCoreSyncServerMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.ID,type);
tableInfo.PropertyInfoID=propertyInfoID;
PropertyInfo?propertyInfoSyncID=type.GetProperties().Where(w =>w.GetCustomAttribute<SyncPropertyAttribute>()!=null&&w.GetCustomAttribute<SyncPropertyAttribute>()!.PropertyIndicator==SyncPropertyAttribute.PropertyIndicatorEnum.SyncID).FirstOrDefault();
if(propertyInfoSyncID==null)thrownewNETCoreSyncServerMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.SyncID,type);
if(propertyInfoSyncID.PropertyType!=typeof(string))thrownewNETCoreSyncServerMismatchPropertyTypeException(propertyInfoSyncID,typeof(string),type);
tableInfo.PropertyInfoSyncID=propertyInfoSyncID;
PropertyInfo?propertyInfoKnowledgeID=type.GetProperties().Where(w =>w.GetCustomAttribute<SyncPropertyAttribute>()!=null&&w.GetCustomAttribute<SyncPropertyAttribute>()!.PropertyIndicator==SyncPropertyAttribute.PropertyIndicatorEnum.KnowledgeID).FirstOrDefault();
if(propertyInfoKnowledgeID==null)thrownewNETCoreSyncServerMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.KnowledgeID,type);
if(propertyInfoKnowledgeID.PropertyType!=typeof(string))thrownewNETCoreSyncServerMismatchPropertyTypeException(propertyInfoKnowledgeID,typeof(string),type);
tableInfo.PropertyInfoKnowledgeID=propertyInfoKnowledgeID;
PropertyInfo?propertyInfoTimeStamp=type.GetProperties().Where(w =>w.GetCustomAttribute<SyncPropertyAttribute>()!=null&&w.GetCustomAttribute<SyncPropertyAttribute>()!.PropertyIndicator==SyncPropertyAttribute.PropertyIndicatorEnum.TimeStamp).FirstOrDefault();
if(propertyInfoTimeStamp==null)thrownewNETCoreSyncServerMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.TimeStamp,type);
if(propertyInfoTimeStamp.PropertyType!=typeof(long))thrownewNETCoreSyncServerMismatchPropertyTypeException(propertyInfoTimeStamp,typeof(long),type);
tableInfo.PropertyInfoTimeStamp=propertyInfoTimeStamp;
PropertyInfo?propertyInfoDeleted=type.GetProperties().Where(w =>w.GetCustomAttribute<SyncPropertyAttribute>()!=null&&w.GetCustomAttribute<SyncPropertyAttribute>()!.PropertyIndicator==SyncPropertyAttribute.PropertyIndicatorEnum.Deleted).FirstOrDefault();
if(propertyInfoDeleted==null)thrownewNETCoreSyncServerMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.Deleted,type);
if(propertyInfoDeleted.PropertyType!=typeof(bool))thrownewNETCoreSyncServerMismatchPropertyTypeException(propertyInfoDeleted,typeof(bool),type);
tableInfo.PropertyInfoDeleted=propertyInfoDeleted;
}
if(!services.Any(w =>w.ServiceType==(typeof(SyncEngine))))
{
thrownewNETCoreSyncServerException("Cannot find SyncEngine subclass in the registered services. Please subclass SyncEngine first, and register it (with serviceType: SyncEngine, and implementationType: your subclass type) in services.");
}
syncService.SyncEvent=syncEvent;
services.AddSingleton(syncService);
returnservices;
}
}
publicstaticclassNETCoreSyncServerApplicationBuilderExtensions
{
publicstaticIApplicationBuilderUseNETCoreSyncServer(thisIApplicationBuilderapp,NETCoreSyncServerOptions?options=null)
{
if(options==null)options=newNETCoreSyncServerOptions();
WebSocketOptionswebSocketOptions=newWebSocketOptions();
webSocketOptions.KeepAliveInterval=TimeSpan.FromSeconds(options.KeepAliveIntervalInSeconds);
app.UseWebSockets(webSocketOptions);
returnapp.UseMiddleware<NETCoreSyncServer.SyncMiddleware>(options);
}
}
}