2016-08-23 75 views
0

我正在努力尋找最好的設置我的項目。我有一個GameSessionServer - 類負責JSON請求處理和註冊的處理程序..現在我有底部插槽處理器處理器(類的一部分):泛型vs基類

public WebSocketResponse HandleRequest(RequestDto request, GameSessionServer server) 
    { 
     TrySetThreadCulture(request); 
     EnsureUserStoredInSession(server.SessionContext, false, SlotIsEngineRNG(server.SessionContext.SystemGameId)); 

     // custom request handling 
     WebSocketResponse wsr = HandleRequestInternal(request, server); 

     // save the last handler 
     server.SessionContext.Items[typeof(WebSocketHandler)] = this; 
     return wsr; 
    } 

它應該是一個RequestDto基類參數或通用RequestDto ?????? 在一個具體的處理我也鑄造RequestDto的具體類型喜歡這裏:

protected override WebSocketResponse HandleRequestInternal(RequestDto request, GameSessionServer server) 
    { 
     CasinoSpinRequestDto spinRequest = request as CasinoSpinRequestDto; 

     int TotalBetInCents = (spinRequest.SpinType != "free") ? spinRequest.TotalBet : 0; 
     JsonResult JR = null; 

     CasinoSpinResponseDto spinResponse = new CasinoSpinResponseDto(); 
     switch (spinRequest.SpinType) 
     { 
      case "regular": 
       JR = PlayBetSpin(spinRequest, server); 
       break; 
      case "free": 
       JR = PlayFreeSpin(spinRequest, server.SessionContext); 
       break; 
     } 

     // set balance in cents 
     spinResponse.Credit = JR.BalanceInCents; 

     SlotEngineRNG engineRNG; 
     sbyte[,] wheels = (JR is SpinJsonResult) ? ((SpinJsonResult) JR).Wheels : ((BonusJsonResult) JR).FreeSpin.Wheels; 
     int rsId = (JR is SpinJsonResult) ? 0 : ((BonusJsonResult) JR).FreeSpin.ReelId; 

     if (SlotIsEngineRNG(server.SessionContext.SystemGameId, out engineRNG)) 
     { 
      GameSettingInfo gsi = DataBuffer.Instance.GetGameSetting(server.SessionContext.UserGroupId, (int) server.SessionContext.SystemGameId); 
      spinResponse.Results = CalculateWheelPositionsRNG(wheels , rsId , engineRNG, gsi); 
     } else 
     { 
      int[] pos; 
      PosHoldersFactory.GetSlotPositions(server.SessionContext.SystemGameId, wheels, rsId, out pos); 
      spinResponse.Results = pos; 
     } 

..........................如果你需要更多的澄清

interface IWebRequestHandler 
{ 
    bool CanHandle(RequestDto request); // assuming the request dto is the base class 

    WebSocketResponse Handle(RequestDto request, GameSessionServer server); 
} 

class CasinoSpinRequestHandler : IWebRequestHandler 
{ 
    bool CanHandle(RequestDto request) 
    { 
     return request is CasinoSpinRequestDto; 
    } 

    WebSocketResponse Handle(RequestDto request, GameSessionServer server) 
    { 
     var spinRequest = request as CasinoSpinRequestDto; 

     if (spinRequest == null) throw new ArgumentNullException("Incorrect usage of the handler"); 

     // do your specific spin request code here 
    } 
} 

public WebSocketResponse HandleRequest(RequestDto request, GameSessionServer server) 
{ 
    TrySetThreadCulture(request); 
    EnsureUserStoredInSession(server.SessionContext, false, SlotIsEngineRNG(server.SessionContext.SystemGameId)); 

    var handlerStrategies = this.GetType().Assembly.GetTypes().Where(x => typeof(IWebRequestHandler).IsAssignableFrom(x)).Select(x => Activator.CreateInstance(x)).Cast<IWebRequestHandler>().ToList(); 
    // the above is poor man DI, you can just put in the constructor ctor(IEnumerable<IWebRequestHandler>) 
    // if your DI supports it, otherwise you can abstract the code above to a 
    // static class so it only gets called once. 

    var handler = handlerStrategies.FirstOrDefault(x => x.CanHandle(request)); 

    if (handler == null) throw new Exception("Unable to handle this particular request"); 

    // custom request handling 
    var wsr = handler.Handle(request, server); 

    // the consuming of the handler can also be abstracted to a delegate class 
    // which merely has the responsibility of sorting out which handler to use 

    // save the last handler 
    server.SessionContext.Items[typeof(WebSocketHandler)] = this; 
    return wsr; 
} 

,只是具體哪一部分:......

+1

基類對於不重複過程或過程是很好的,所以如果有重複,並且**不依賴於特定的RequestDto,那麼確定它是通用的。您通常可以知道是否需要泛型,因爲您需要訪問RequestDto上的某些屬性(例如,如果它只是一個「Id」字段,那麼您可以將它放在一個接口上並約束泛型參數) –

+0

這取決於你的用例,我認爲我們沒有足夠的上下文來評論。據推測,如果你當前的代碼編譯和工作,那麼你不需要任何方法或屬性在你的類的通用版本? –

+0

這似乎很奇怪,你似乎正在編寫自己的網絡服務器....我認爲微軟有一個內置的HttpServer爲此... –

回答

0

所以,你可以使用策略模式。

+0

好吧,我現在看到了。謝謝 –