1

I think I've almost got this all figured out. This question is the context of a .NET MVC application.

My entity: (I'll be switching to Fluent nHibernate soon)

[Serializable]
[Class(Schema = "dbo", Table = "LiveMedia")]
[Cache(1, Usage = CacheUsage.ReadWrite)]
public class Media : NhBase<Media>
{
    [Id(Name = "Id", Column = "ID"), Generator(1, Class = "native")]
    public virtual int Id { get; set; }

    [Property]
    public virtual string Title { get; set; }

    [Property]
    public virtual string Description { get; set; }

    [Property(Column = "TypeID")]
    public virtual MediaType MediaType { get; set; }

    [Property]
    public virtual DateTime CreatedAt { get; set; }

    [Property]
    public virtual DateTime UpdatedAt { get; set; }

    [Property]
    public virtual string EmbedUrl { get; set; }

    [Property]
    public virtual string ThumbnailUrl { get; set; }

    [Property]
    public virtual string S3Filename { get; set; }

    [XmlIgnore, Set(0, Table = "LiveCollectionMedia", Schema = "dbo", Cascade = CascadeStyle.AllDeleteOrphan, Lazy = true, Inverse = true), Key(1, Column = "MediaID"), OneToMany(2, Class = "BanffCentre.Business.Data.Live.MediaCollection, BanffCentre")]
    public virtual ISet<MediaCollection> Collections { get; set; }

    [XmlIgnore, Set(0, Table = "LiveMediaCategory", Schema = "dbo", Cascade = CascadeStyle.SaveUpdate, Lazy = true), Key(1, Column = "MediaID"), ManyToMany(2, Class = "BanffCentre.Business.Data.Live.Tag, BanffCentre", Column = "CategoryID")]
    public virtual ISet<Tag> Tags { get; set; }

    public Media()
    {
        Collections = new HashedSet<MediaCollection>();
        Tags = new HashedSet<Tag>();
    }
}

My ViewModel:

public class MediaAudioViewModel
{
    public int? Id { get; set; }

    [Required]
    public string Title { get; set; }

    public string Description { get; set; }
    public MediaType MediaType { get; set; }
    public string ThumbnailUrl { get; set; }
    public IList<Tag> Tags { get; set; }
    public string S3Filename { get; set; }
    public HttpPostedFileBase AudioFile { get; set; }
}

The question:

I know I shouldn't have a collection of "Tag" entities in my ViewModel:

public IList<Tag> Tags { get; set; }

So am I suppose to map a TagViewModel to this instead? Like this?

public IList<TagViewModel> Tags { get; set; }

And if this is a many-to-many relationship am I suppose to map a MediaViewModel from the TagViewModel when I start creating views for the tags?

I imagine so... I just feel like this abstraction stuff gets a little nutty. Just hoping for a quick confirmation!

Thanks.

EDIT

This post seems to answer my question. I just found it. I'd still be interested in any other thoughts.

4

1 回答 1

1

在做了更多阅读之后,我决定我的视图模型应该包含其他视图模型的集合。使用AutoMapper我通常可以为我完成这项工作。

Mapper.CreateMap<MediaDto, MediaVideoViewModel>();
Mapper.CreateMap<TagDto, TagViewModel>();
var videoModel = Mapper.Map<MediaDto, MediaVideoViewModel>(mediaDto);
于 2012-08-09T18:53:05.560 回答