ExcutorBase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Diagnostics;
  3. using LJLib.Net.SPI.Com;
  4. namespace LJLib.Net.SPI.Server
  5. {
  6. internal abstract class ExcutorBase
  7. {
  8. public abstract LJResponse Excute(ILJRequest request, object state);
  9. }
  10. internal abstract class ExcutorBase<T1, T2> : ExcutorBase
  11. where T1:class,ILJRequest<T2>
  12. where T2:LJResponse,new()
  13. {
  14. protected abstract void ExcuteInternal(T1 request, object state, T2 rslt);
  15. protected virtual bool PreHandle(T1 request, T2 rslt) { return true; }
  16. public override LJResponse Excute(ILJRequest request, object state)
  17. {
  18. T2 rslt = new T2();
  19. try
  20. {
  21. T1 req = request as T1;
  22. if (req == null)
  23. {
  24. rslt.ErrMsg = "request不能转换成类型" + typeof(T1).Name;
  25. return rslt;
  26. }
  27. if (PreHandle(req, rslt))
  28. {
  29. ExcuteInternal(req, state, rslt);
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. Trace.Write(ex.ToString());
  35. rslt.ErrMsg = ex.ToString();
  36. }
  37. return rslt;
  38. }
  39. }
  40. }