2016-08-23 10 views
0

私は自分のプロジェクトをセットアップするのに苦労しています。ジェネリックスとベースクラス

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基本クラスのパラメータまたはあるべき:今、私はベースソケットハンドラハンドラ(クラスの一部)を持っている.. JSONリクエストの処理および登録されているハンドラを担当するクラス - 私はGameSessionServerを持っていますジェネリック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; 
     } 

.......................... ......

+1

基本クラスは、プロシージャやプロセスを繰り返さない場合に適しています。したがって、重複があり、特定のRequestDに**依存していない場合は、それをgenericにしてください。あなたはアクセスする必要があるRequestDtoの特定のプロパティを使用するためジェネリックが必要かどうかを普通は知ることができます(例えば 'Id'フィールドの場合はそれをインターフェイスに置き、汎用引数を制約できます) –

+0

あなたのユースケースにもよりますが、コメントするのに十分なコンテキストがないと思います。おそらくあなたの現在のコードがコンパイルされ、動作するならば、クラスのジェネリックバージョンにあるメソッドやプロパティは必要ありません。 –

+0

あなた自身のWebサーバーを書いているようですが、私はMSにはHttpServerが組み込まれていると思います... –

答えて

0

だから、戦略パターンを使用することができます。

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; 
} 

あなたはより多くの明確化が必要な場合は、単にどの部分について具体的に。

+0

OK、今すぐ表示されます。ありがとう –

関連する問題