AspNet不同的webservices,一个自定义类,在客户端生成不同的自定义类

时间:2017-05-03 14:01:13

标签: asp.net web-services namespaces

先生,早上好,

我正在试图找出为什么我的客户端为我的自定义类生成不同的命名空间(好吧,问题不是确切原因,但如果我能以某种方式改变这种行为)。

一般的例子在这里: 在我的后端,我有两个不同的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都有返回自定义类的方法: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);

很明显,这仅仅是一个例子,如果一个代码行,这不是一个真正的问题。 问题出现在项目开始增长的时候,在这种情况下,我将会有很多来自很多地方的类的实例。

提前致谢,祝你有个美好的一天! 请忽略一些语法错误,我正在努力学习英语。

修改

所以,我发现我可以“转换”我的对象,或者我可以使用反射编写转换器(new object.properties = oher object.properties)但我不想这样做,如果有的话不这样做的方法,我会很高兴

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

1 个答案:

答案 0 :(得分:0)

好吧,因为我的类有自定义属性,我不能简单地创建一个Reflection Mapper,在这种情况下,我必须创建自己的&#34; mapper&#34;对于我想要转换的每个自定义类:

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

修改

我没有将自己的答案标记为正确答案,因为这是解决问题的方法。