在我的web應用程序中,我使用了來自同一提供程序的幾個asmx(Web服務),它們有一個用於此目的,其他用於此目的,但都需要SOAP Header與認證。在C#中使用DS創建一個「all-in-one」函數
這是簡單的添加驗證:
public static SoCredentialsHeader AttachCredentialHeader()
{
SoCredentialsHeader ch = new SoCredentialsHeader();
ch.AuthenticationType = SoAuthenticationType.CRM5;
ch.UserId = "myUsername";
ch.Secret = apUtilities.CalculateCredentialsSecret(
SoAuthenticationType.CRM5, apUtilities.GetDays(), "myUsername", "myPassword");
return ch;
}
問題是這樣的SoCredentialsHeader到來(偏差)從ONE web服務,我需要將相同的代碼添加到其他人,如:
public static wsContact.SoCredentialsHeader AttachContactCredentialHeader()
{
wsContact.SoCredentialsHeader ch = new wsContact.SoCredentialsHeader();
ch.AuthenticationType = wsContact.SoAuthenticationType.CRM5;
ch.UserId = "myUsername";
ch.Secret = apUtilities.CalculateCredentialsSecret(
wsContact.SoAuthenticationType.CRM5, apUtilities.GetDays(), "myUsername", "myPassword");
return ch;
}
public static wsDiary.SoCredentialsHeader AttachDiaryCredentialHeader()
{
wsDiary.SoCredentialsHeader ch = new wsDiary.SoCredentialsHeader();
ch.AuthenticationType = wsDiary.SoAuthenticationType.CRM5;
ch.UserId = "myUsername";
ch.Secret = apUtilities.CalculateCredentialsSecret(
wsDiary.SoAuthenticationType.CRM5, apUtilities.GetDays(), "myUsername", "myPassword");
return ch;
}
有沒有一種方法來實現設計模式,以便只使用一個功能但適合所有web服務?
有時我看到T字母,這是一種情況嗎?如果是的話,我該如何完成這樣的功能?
P.S.我可以通過枚舉並使用開關來檢查枚舉名稱並應用正確的Header,但是每次我需要添加新的WebService時,我需要添加枚舉和代碼,我正在爲此搜索高級技術。
謝謝。
我會給這個嘗試,我會搜索更多關於T4模板:) – balexandre 2009-01-04 18:59:59