2012-11-20 35 views
2

我想擴展webservice plygin以便從nopcommerce獲得產品列表。NopCommerce webservice DTO的

我所做的是以下幾點。我創建了一個類似ProductDto,和內部NopService.cs我創建的方法等:

public List<ProductDto> GetProductCollection(string usernameOrEmail, string userPassword) 

ProductDto使用AutoMapper從Product取。 (如下圖所示)

,但它不工作:(我缺少什麼?

任何想法?這一切背後的想法是nopcommerce我的ERP通過web服務

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Nop.Core.Domain.Catalog 
{ 
    public class ProductDto 
    { 
     public virtual string Name { get; set; } 

     // Instance members must be virtual on data table objects like Affiliate.cs 
     // Virtual is required by data access frameworks so that these frameworks 
     // can implement more complex features like lazy loading. 

     public virtual string ProductGID { get; set; } 

     /// <summary> 
     /// Gets or sets the short description 
     /// </summary> 

     public virtual string ShortDescription { get; set; } 

     /// <summary> 
     /// Gets or sets the full description 
     /// </summary> 

     public virtual string FullDescription { get; set; } 

     /// <summary> 
     /// Gets or sets the admin comment 
     /// </summary> 

     public virtual string AdminComment { get; set; } 

     /// <summary> 
     /// Gets or sets a value of used product template identifier 
     /// </summary> 

     public virtual int ProductTemplateId { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether to show the product on home page 
     /// </summary> 

     public virtual bool ShowOnHomePage { get; set; } 

     /// <summary> 
     /// Gets or sets the meta keywords 
     /// </summary> 

     public virtual string MetaKeywords { get; set; } 

     /// <summary> 
     /// Gets or sets the meta description 
     /// </summary> 

     public virtual string MetaDescription { get; set; } 

     /// <summary> 
     /// Gets or sets the meta title 
     /// </summary> 

     public virtual string MetaTitle { get; set; } 

     /// <summary> 
     /// Gets or sets the search-engine name 
     /// </summary> 

     public virtual string SeName { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the product allows customer reviews 
     /// </summary> 

     public virtual bool AllowCustomerReviews { get; set; } 

     /// <summary> 
     /// Gets or sets the rating sum (approved reviews) 
     /// </summary> 

     public virtual int ApprovedRatingSum { get; set; } 

     /// <summary> 
     /// Gets or sets the rating sum (not approved reviews) 
     /// </summary> 

     public virtual int NotApprovedRatingSum { get; set; } 

     /// <summary> 
     /// Gets or sets the total rating votes (approved reviews) 
     /// </summary> 

     public virtual int ApprovedTotalReviews { get; set; } 

     /// <summary> 
     /// Gets or sets the total rating votes (not approved reviews) 
     /// </summary> 

     public virtual int NotApprovedTotalReviews { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the entity is published 
     /// </summary> 

     public virtual bool Published { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the entity has been deleted 
     /// </summary> 

     public virtual bool Deleted { get; set; } 

     /// <summary> 
     /// Gets or sets the date and time of product creation 
     /// </summary> 

     public virtual DateTime CreatedOnUtc { get; set; } 

     /// <summary> 
     /// Gets or sets the date and time of product update 
     /// </summary> 

     public virtual DateTime UpdatedOnUtc { get; set; } 
    } 
} 

與內部連接NopService.cs我創造了這樣的

public List<ProductDto> GetProductCollection(string usernameOrEmail, string userPassword) 
    { 
     CheckAccess(usernameOrEmail, userPassword); 
     if (!_permissionSettings.Authorize(StandardPermissionProvider.ManageCatalog)) 
      throw new ApplicationException("Not allowed to manage Catalog"); 

     var productslist = new List<Product>(); 
     productslist.AddRange(_productService.GetAllProducts(false)); 
     List<Product> products = productslist; 
     List<ProductDto> productsDtos = Mapper.Map<List<Product>, List<ProductDto>>(products); 

     return productsDtos; 
    } 
} 
+0

你可以請張貼你的映射配置。 – Mightymuke

回答

0

我設法使它工作,但在映射嵌套類問題的一種方法。

產品類

public partial class Product : BaseEntity, ILocalizedEntity 
{ 


    private ICollection<ProductVariant> _productVariants; 


    private ICollection<ProductCategory> _productCategories; 


    private ICollection<ProductManufacturer> _productManufacturers; 


    private ICollection<ProductPicture> _productPictures; 


    private ICollection<ProductReview> _productReviews; 


    private ICollection<ProductSpecificationAttribute> _productSpecificationAttributes; 


    private ICollection<ProductTag> _productTags; 

    /// <summary> 
    /// Gets or sets the name 
    /// </summary> 

    public virtual string Name { get; set; } 

    // Instance members must be virtual on data table objects like Affiliate.cs 
    // Virtual is required by data access frameworks so that these frameworks 
    // can implement more complex features like lazy loading. 

    public virtual string ProductGID { get; set; } 

    /// <summary> 
    /// Gets or sets the short description 
    /// </summary> 

    public virtual string ShortDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the full description 
    /// </summary> 

    public virtual string FullDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the admin comment 
    /// </summary> 

    public virtual string AdminComment { get; set; } 

    /// <summary> 
    /// Gets or sets a value of used product template identifier 
    /// </summary> 

    public virtual int ProductTemplateId { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether to show the product on home page 
    /// </summary> 

    public virtual bool ShowOnHomePage { get; set; } 

    /// <summary> 
    /// Gets or sets the meta keywords 
    /// </summary> 

    public virtual string MetaKeywords { get; set; } 

    /// <summary> 
    /// Gets or sets the meta description 
    /// </summary> 

    public virtual string MetaDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the meta title 
    /// </summary> 

    public virtual string MetaTitle { get; set; } 

    /// <summary> 
    /// Gets or sets the search-engine name 
    /// </summary> 

    public virtual string SeName { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the product allows customer reviews 
    /// </summary> 

    public virtual bool AllowCustomerReviews { get; set; } 

    /// <summary> 
    /// Gets or sets the rating sum (approved reviews) 
    /// </summary> 

    public virtual int ApprovedRatingSum { get; set; } 

    /// <summary> 
    /// Gets or sets the rating sum (not approved reviews) 
    /// </summary> 

    public virtual int NotApprovedRatingSum { get; set; } 

    /// <summary> 
    /// Gets or sets the total rating votes (approved reviews) 
    /// </summary> 

    public virtual int ApprovedTotalReviews { get; set; } 

    /// <summary> 
    /// Gets or sets the total rating votes (not approved reviews) 
    /// </summary> 

    public virtual int NotApprovedTotalReviews { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the entity is published 
    /// </summary> 

    public virtual bool Published { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the entity has been deleted 
    /// </summary> 

    public virtual bool Deleted { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of product creation 
    /// </summary> 

    public virtual DateTime CreatedOnUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of product update 
    /// </summary> 

    public virtual DateTime UpdatedOnUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the product variants 
    /// </summary> 


    public virtual ICollection<ProductVariant> ProductVariants 
    { 
     get { return _productVariants ?? (_productVariants = new List<ProductVariant>()); } 
     protected set { _productVariants = value; } 
    } 
    /// <summary> 
    /// Gets or sets the collection of ProductCategory 
    /// </summary> 
    public virtual ICollection<ProductCategory> ProductCategories 
    { 
     get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); } 
     protected set { _productCategories = value; } 
    } 

    /// <summary> 
    /// Gets or sets the collection of ProductManufacturer 
    /// </summary> 
    public virtual ICollection<ProductManufacturer> ProductManufacturers 
    { 
     get { return _productManufacturers ?? (_productManufacturers = new List<ProductManufacturer>()); } 
     protected set { _productManufacturers = value; } 
    } 

    /// <summary> 
    /// Gets or sets the collection of ProductPicture 
    /// </summary> 

    public virtual ICollection<ProductPicture> ProductPictures 
    { 
     get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); } 
     protected set { _productPictures = value; } 
    } 

    /// <summary> 
    /// Gets or sets the collection of product reviews 
    /// </summary> 

    public virtual ICollection<ProductReview> ProductReviews 
    { 
     get { return _productReviews ?? (_productReviews = new List<ProductReview>()); } 
     protected set { _productReviews = value; } 
    } 

    /// <summary> 
    /// Gets or sets the product specification attribute 
    /// </summary> 

    public virtual ICollection<ProductSpecificationAttribute> ProductSpecificationAttributes 
    { 
     get { return _productSpecificationAttributes ?? (_productSpecificationAttributes = new List<ProductSpecificationAttribute>()); } 
     protected set { _productSpecificationAttributes = value; } 
    } 

    /// <summary> 
    /// Gets or sets the product specification attribute 
    /// </summary> 

    public virtual ICollection<ProductTag> ProductTags 
    { 
     get { return _productTags ?? (_productTags = new List<ProductTag>()); } 
     protected set { _productTags = value; } 
    } 

} 

}

ProductDto類

public class ProductDto 
{ 



    private ICollection<ProductCategoryDto> _productCategories; 




    public virtual string Name { get; set; } 

    // Instance members must be virtual on data table objects like Affiliate.cs 
    // Virtual is required by data access frameworks so that these frameworks 
    // can implement more complex features like lazy loading. 

    public virtual string ProductGID { get; set; } 

    /// <summary> 
    /// Gets or sets the short description 
    /// </summary> 

    public virtual string ShortDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the full description 
    /// </summary> 

    public virtual string FullDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the admin comment 
    /// </summary> 

    public virtual string AdminComment { get; set; } 

    /// <summary> 
    /// Gets or sets a value of used product template identifier 
    /// </summary> 

    public virtual int ProductTemplateId { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether to show the product on home page 
    /// </summary> 

    public virtual bool ShowOnHomePage { get; set; } 

    /// <summary> 
    /// Gets or sets the meta keywords 
    /// </summary> 

    public virtual string MetaKeywords { get; set; } 

    /// <summary> 
    /// Gets or sets the meta description 
    /// </summary> 

    public virtual string MetaDescription { get; set; } 

    /// <summary> 
    /// Gets or sets the meta title 
    /// </summary> 

    public virtual string MetaTitle { get; set; } 

    /// <summary> 
    /// Gets or sets the search-engine name 
    /// </summary> 

    public virtual string SeName { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the product allows customer reviews 
    /// </summary> 

    public virtual bool AllowCustomerReviews { get; set; } 

    /// <summary> 
    /// Gets or sets the rating sum (approved reviews) 
    /// </summary> 

    public virtual int ApprovedRatingSum { get; set; } 

    /// <summary> 
    /// Gets or sets the rating sum (not approved reviews) 
    /// </summary> 

    public virtual int NotApprovedRatingSum { get; set; } 

    /// <summary> 
    /// Gets or sets the total rating votes (approved reviews) 
    /// </summary> 

    public virtual int ApprovedTotalReviews { get; set; } 

    /// <summary> 
    /// Gets or sets the total rating votes (not approved reviews) 
    /// </summary> 

    public virtual int NotApprovedTotalReviews { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the entity is published 
    /// </summary> 

    public virtual bool Published { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the entity has been deleted 
    /// </summary> 

    public virtual bool Deleted { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of product creation 
    /// </summary> 

    public virtual DateTime CreatedOnUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of product update 
    /// </summary> 

    public virtual DateTime UpdatedOnUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the collection of ProductCategory 
    /// </summary> 
    public virtual ICollection<ProductCategoryDto> ProductCategories 
    { 
     get { return _productCategories ?? (_productCategories = new List<ProductCategoryDto>()); } 
     protected set { _productCategories = value; } 
    } 



} 

}

產品分類類

public partial class ProductCategory : BaseEntity 
{ 
    public virtual int ProductId { get; set; } 

    /// <summary> 
    /// Gets or sets the category identifier 
    /// </summary> 
    public virtual int CategoryId { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the product is featured 
    /// </summary> 
    public virtual bool IsFeaturedProduct { get; set; } 

    /// <summary> 
    /// Gets or sets the display order 
    /// </summary> 
    public virtual int DisplayOrder { get; set; } 

    /// <summary> 
    /// Gets the category 
    /// </summary> 
    public virtual Category Category { get; set; } 

    /// <summary> 
    /// Gets the product 
    /// </summary> 
    public virtual Product Product { get; set; } 

} 

而ProductCategoryDto類

public class ProductCategoryDto 

{ 
    /// <summary> 
    /// Gets or sets the product identifier 
    /// </summary> 
    public virtual int ProductId { get; set; } 

    /// <summary> 
    /// Gets or sets the category identifier 
    /// </summary> 
    public virtual int CategoryId { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the product is featured 
    /// </summary> 
    public virtual bool IsFeaturedProduct { get; set; } 

    /// <summary> 
    /// Gets or sets the display order 
    /// </summary> 
    public virtual int DisplayOrder { get; set; } 

    /// <summary> 
    /// Gets the category 
    /// </summary> 
    public virtual CategoryDto Category { get; set; } 

    /// <summary> 
    /// Gets the product 
    /// </summary> 
    public virtual ProductDto Product { get; set; } 
} 

映射是一個方法內部做過這樣的

public List<ProductDto> GetProductCollection(string usernameOrEmail, string userPassword) 

    { 
     CheckAccess(usernameOrEmail, userPassword); 
     if (!_permissionSettings.Authorize(StandardPermissionProvider.ManageCatalog)) 
      throw new ApplicationException("Not allowed to manage Catalog"); 

     List<Product> productslist = new List<Product>(); 
     productslist.AddRange(_productService.GetAllProducts(false)); 
     List<ProductCategory> productCategorylist = new List<ProductCategory>(); 
     foreach (var product in productslist) 
     { 
      productCategorylist.AddRange(_categoryService.GetProductCategoriesByProductId(product.Id, false)); 


     } 



     Mapper.CreateMap<Product, ProductDto>(); 

     Mapper.CreateMap<ProductCategory, ProductCategoryDto>(); 
     List<ProductDto> productsDto = Mapper.Map<List<Product>, List<ProductDto>>(productslist); 
     List<ProductCategoryDto> productCategoriesDto = Mapper.Map<List<ProductCategory>, List<ProductCategoryDto>>(productCategorylist); 


     return productsDto; 

    } 

的問題是,我從來沒有得到ProductDto映射到將ICollection的ProductCategoriesDto。其他一切正常。

+0

我設法通過更改此私有ICollection _productCategories;在ProductDto類中放入公共ICollection _productCategories;現在,我可以從產品導航到productCategories,但是當我嘗試通過Web服務傳輸ProductDto對象時,出現Http錯誤 –

1

您的映射只能在啓動過程中配置一次。因此,嘗試將其移動到一個配置文件:

namespace MyNamespace 
{ 
    using System.Collections.Generic; 

    using AutoMapper; 

    public class ProductProfile : Profile 
    { 
     public override string ProfileName 
     { 
      get 
      { 
       return "ProductProfile"; 
      } 
     } 

     protected override void Configure() 
     { 
      Mapper.CreateMap<Product, ProductDto>(); 
      Mapper.CreateMap<ProductCategory, ProductCategoryDto>(); 
      // etc 
     } 
    } 
} 

然後初始化它在啓動時(Application_Start等):

Mapper.Initialize(m => m.AddProfile<ProductProfile>()); 

創建單元測試,以檢查你的映射:

namespace MyNamespace 
{ 
    using System.Collections.Generic; 

    using AutoMapper; 

    using NUnit.Framework; 

    [TestFixture] 
    public class AutoMapperTests 
    { 
     [Test] 
     public void AutoMapper_Configuration_IsValid() 
     { 
      Mapper.Initialize(m => m.AddProfile<ProductProfile>()); 
      Mapper.AssertConfigurationIsValid(); 
     } 
    } 
} 

這將有助於確定映射配置中是否存在任何錯誤,並告訴您需要解決的問題。一旦完成,您可以專注於使功能正常工作。