0

对不起我的英语不好。

我正在进行一个糟糕而古老的项目。我尝试重构代码的几个部分。

我有一个大类,它在许多静态方法中调用 WebService 的方法(非常脏)。这些方法的特殊性是每次调用一个webservice方法之前,总是调用另一个webservice方法(这个调用是强制性的,我不能修改WebService)

我寻找一种方法来封装这个第一次调用,以避免代码冗余并促进未来的发展

这些方法类似于:

public static ObjectResult MethodeA(int methodParam1, string methodParam2, ...)
{
    MyWebService ws = new MyWebService();

    ObjectResult result = null;

    try
    {
        string aParam1 = GetParam1();

        ws.Credentials = System.Net.CredentialCache.DefaultCredentials;

        ObjectResultContext myContext = ws.GetUserContext(aParam1); // This method is always called before any webservice method call

        if (myContext.IsOK)
        {
            ws.UserProperties = myContext.UserProperties;
            result = ws.GetResultMethodA(methodParam1, methodeParam2);
        }
        else
        {
            throw new ...
        }

    }
    catch (Exception ex)
    {
       //...
        throw;
    }

    return result;
}
4

1 回答 1

1

我将在类构造函数中只加载一次userProperties ,以便您可以从静态 Web 服务调用中删除它:

你可以这样做:

public WebServiceClass
{
  private UserProperties userproperties;

  public WebServiceClass
  {
      ObjectResultContext myContext = ws.GetUserContext(aParam1); 
       if (myContext.IsOK)
        {
            userproperties = myContext.UserProperties;           
        }
        else
        {
            throw new ...
        }

  }

  public static ObjectResult MethodeA(int methodParam1, string methodParam2, ...)
  {
      MyWebService ws = new MyWebService();

      ObjectResult result = null;

      try
      {
          string aParam1 = GetParam1();

          ws.Credentials = System.Net.CredentialCache.DefaultCredentials;


          ws.UserProperties = myContext.UserProperties;
          result = ws.GetResultMethodA(methodParam1, methodeParam2);

      }
      catch (Exception ex)
      {
         //...
          throw;
      }

      return result;
  }

}
于 2013-10-02T10:22:06.870 回答