4

在我的项目中发生了非常奇怪的事情,我有一个非常简单的CLR对象,第一个是Model另一个是ViewModel,在我编译项目后,我运行我的WebApi ASP.NET项目,使用所需的参数,我可以看到我的Model返回数据。

一旦我可以看到 Mapper 映射正常,第二次它返回的所有东西都带有空值。不是一直发生的问题。

非常重要:更新 14.03.2013
当我回收应用程序时它会停止这样做,但过了一段时间它又开始这样做了,我重新保存web.config文件然后它就可以了。

这是我的模型/视图模型:

public class Gallery : Entity
{
    public override long Id { get; set; }
    public virtual Settings SiteOwner { get; set; }
    public virtual Category Category { get; set; }
    public virtual string PageTitle { get; set; }
    public virtual string TitleDescription { get; set; }
    public virtual string GalleryTitle { get; set; }
    public virtual IList<UIItem> GalleryItems { get; set; }
}

public class UIItem : Entity
{
    public override long Id { get; set; }
    public virtual Product Product { get; set; }
    public virtual Gallery Gallery { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string Price { get; set; }
    public virtual string ImageUrl { get; set; }
    public virtual string VideoUrl { get; set; }
    public virtual string FileUrl { get; set; }
}
public class GalleryViewModel
{
    public virtual string PageTitle { get; set; }
    public virtual string TitleDescription { get; set; }
    public virtual string GalleryTitle { get; set; }
    public virtual IList<UIItemViewModel> GalleryItems { get; set; }
}

public class UIItemViewModel
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string Price { get; set; }
    public virtual string ImageUrl { get; set; }
    public virtual string VideoUrl { get; set; }
    public virtual string FileUrl { get; set; }
}

这是我使用它的方式

// my apicontroller

// FindGalleryByAppIdAndCategoryId returns => Gallery 
var source = _galleryRepository.FindGalleryByAppIdAndCategoryId(appId, catId);
return Mapper.DynamicMap<GalleryViewModel>(source);
4

3 回答 3

3
  1. 你为什么用virtual关键字标记你的非导航属性?只有涉及到一对一、一对多、多对多和复杂类型关系的属性可以用virtual关键字标记以应用延迟加载

2.

Mapper.CreateMap<UIItem,UIItemViewModel>();
...
UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem);

如果你需要特殊的映射逻辑,你可以这样做:

Mapper.CreateMap<UIItem,UIItemViewModel>().ConvertUsing(new ModelToViewModelConverter());
...
UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem);
...

转换器将如下所示:

public class ModelToViewModelConverter : ITypeConverter<UIItem,UIItemViewModel>
    {
        public UIItemViewModel Convert(ResolutionContext context)
        {
            UIItem item = (UIItem)context.SourceValue;
            // Perform your convertion logic 
        }
    }

以同样的方式,您可以将所有实体甚至集合映射到集合。

于 2013-03-15T13:38:22.330 回答
2

当 Gallery 和 UIItem 有交叉引用时,很可能会发生这种情况:

public class Gallery : Entity
{
    public virtual IList<UIItem> GalleryItems { get; set; }
}

public class UIItem : Entity
{
    public virtual Gallery Gallery { get; set; }
}

你能在你的代码中测试这个案例吗?

于 2013-03-14T09:04:10.310 回答
1

你可以尝试使用

Mapper.Map<Source,Destination>(source); 

或者

Mapper.Map(source);

并确保您已声明

CreateMap<Source, Destination>();

当您可以声明两种类型的 CreateMap 时,我看不出您使用 Dynamic Map 的原因。您不应该以这种方式体验这种行为。

于 2013-03-14T16:26:24.690 回答