2017-05-03 36 views
0

早上好先生,ASPNET不同的web服務,一個自定義類,在客戶端產生不同的自定義類

我試圖找出爲什麼我的客戶對我的自定義類產生不同的命名空間(好吧,這個問題是不是究竟是爲什麼,但如果我能以某種方式改變這種行爲)。

一般的例子是在這裏: 在我的後臺,我有兩個不同的web服務

  • Menu.asmx
  • Category.asmx

論文wbservices的類的聲明看起來這:

using System.Collections.Generic; 
using System.Web.Services; 

namespace backend.webservices 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    public class Category : WebService 
    { 
     [WebMethod] 
     public List<CategoryType> GetFeaturedCategories(int idRegion, int idLanguage) 
     { 
      return CategoryService.GetFeaturedCategories(idRegion, idLanguage); 
     } 

     [WebMethod] 
     public CategoryType GetCategory(int idCategory, int idLanguage, int idRole, int idUser, int idRegion, bool generateBreadCrumb) 
     { 
      return CategoryService.GetCategory(idCategory, idLanguage, idRole, idUser, idRegion, generateBreadCrumb); 
     } 

    } 
} 

這兩個webservices讓我返回自定義類thods:CategoryType.cs

而且我CategoryType聞起來像這樣:

using System.Collections.Generic; 
[Table("CATEGORY")] 
public class CategoryType 
{ 
    [Column("ID_CATEGORY")] 
    public int? idCategory { get; set; } 

    [Column("ID_REGION")] 
    public int? idRegion { get; set; } 

    [Column("IN_STATUS")] 
    public int? inStatus { get; set; } 

    [Column("IN_MENU_SUPERIOR")] 
    public int? inMenuSuperior { get; set; } 

    [Column("NR_ORDER")] 
    public int? nrOrder { get; set; } 

    [Column("IN_CATALOG")] 
    public int? inCatalog { get; set; } 

    [Column("IN_FEATURED")] 
    public int? inFeatured { get; set; } 

    [Column("TX_MONGO_ID")] 
    public string txMongoId { get; set; } 

    //EXTERNAL 
    [Column("NM_CATEGORY")] 
    public string nmCategory { get; set; } 

    //NON ENTITY 
    public List<CategoryType> subCategories = new List<CategoryType>(); 

    public List<BreadCrumbType> breadCrumb; 
} 

「問題」是,當我在我的前端(服務引用)導入這些web服務它複製我的自定義類的客戶端,因此,在代碼中,我有兩個類CategoryType.cs:

  • WSMenu.CategoryType
  • WSCategory.CategoryType

什麼可氣的是,它不能從一種類型轉變成另一種,就像這樣:

WSMenu.CategoryType myExampleCategory = WSCategory.GetMyExampleCategory(idCategory); 

Obviusly,這只是一個例子,如果有一個單一的代碼行,這是不是一個真正的問題。 問題出現在項目開始增長的時候,在這種情況下,我會有很多來自很多地方的這些類的實例。

預先感謝,祝你有美好的一天! 拜託,請忽略一些格式錯誤,我努力學習英語。

編輯:

所以,我發現,我可以「轉換」我的目標,我也可以使用反射寫一個轉換器(新object.properties = oher object.properties),但我不知道要做到這一點,如果有一種方法可以不這樣做,我會很高興

2 objects, exactly the same (except namespace) c#

回答

0

嗯,因爲我的課有自定義屬性,我不能簡單地創建一個反射映射,在這種情況下,我必須爲我想要轉換的每個自定義類創建自己的「映射器」:

using MenuCategoryType = frontend.WSMenu.CategoryType; 
using MenuBreadCrumbType = frontend.WSMenu.BreadCrumbType; 
using CategoryCategoryType = frontend.WSCategory.CategoryType; 
using CategoryBreadCrumbType = frontend.WSCategory.BreadCrumbType; 

/// <summary> 
/// converte objetos CategoryType de namespaces diferentes 
/// </summary> 
public class CategoryMapper 
{ 
    public static MenuCategoryType Convert(CategoryCategoryType origin) 
    { 
     if (origin == null) 
      return null; 

     return new MenuCategoryType() 
     { 
      idCategory = origin.idCategory, 
      idRegion = origin.idRegion, 
      inCatalog = origin.inCatalog, 
      inFeatured = origin.inFeatured, 
      inMenuSuperior = origin.inMenuSuperior, 
      inStatus = origin.inStatus, 
      nmCategory = origin.nmCategory, 
      nrOrder = origin.nrOrder, 
      txMongoId = origin.txMongoId, 
      breadCrumb = Convert(origin.breadCrumb), 
      subCategories = Convert(origin.subCategories) 
     }; 
    } 

    public static CategoryCategoryType Convert(MenuCategoryType origin) 
    { 
     if (origin == null) 
      return null; 

     return new CategoryCategoryType() { 
      idCategory = origin.idCategory, 
      idRegion = origin.idRegion, 
      inCatalog = origin.inCatalog, 
      inFeatured = origin.inFeatured, 
      inMenuSuperior = origin.inMenuSuperior, 
      inStatus = origin.inStatus, 
      nmCategory = origin.nmCategory, 
      nrOrder = origin.nrOrder, 
      txMongoId = origin.txMongoId, 
      breadCrumb = Convert(origin.breadCrumb), 
      subCategories = Convert(origin.subCategories) 
     }; 
    } 

    public static MenuBreadCrumbType[] Convert(CategoryBreadCrumbType[] origin) 
    { 
     if (origin == null) 
      return null; 

     MenuBreadCrumbType[] target = new MenuBreadCrumbType[origin.Length]; 
     for(int i=0; i < origin.Length; i++) 
     { 
      target[i] = new MenuBreadCrumbType() { 
       idCategory = origin[i].idCategory, 
       nmCategory = origin[i].nmCategory 
      }; 
     } 
     return target;    
    } 

    public static CategoryBreadCrumbType[] Convert(MenuBreadCrumbType[] origin) 
    { 
     if (origin == null) 
      return null; 

     CategoryBreadCrumbType[] target = new CategoryBreadCrumbType[origin.Length]; 
     for (int i = 0; i < origin.Length; i++) 
     { 
      target[i] = new CategoryBreadCrumbType() 
      { 
       idCategory = origin[i].idCategory, 
       nmCategory = origin[i].nmCategory 
      }; 
     } 
     return target; 
    } 

    public static MenuCategoryType[] Convert(CategoryCategoryType[] origin) 
    { 
     if (origin == null) 
      return null; 

     MenuCategoryType[] target = new MenuCategoryType[origin.Length]; 
     for(int i = 0; i < origin.Length; i++) 
     { 
      target[i] = Convert(origin[i]); 
     } 

     return target; 
    } 

    public static CategoryCategoryType[] Convert(MenuCategoryType[] origin) 
    { 
     if (origin == null) 
      return null; 

     CategoryCategoryType[] target = new CategoryCategoryType[origin.Length]; 
     for (int i = 0; i < origin.Length; i++) 
     { 
      target[i] = Convert(origin[i]); 
     } 

     return target; 
    } 
} 

編輯

我不是標誌着我自己的答案是正確的答案,只是因爲這是一種變通方法的問題。

相關問題