using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class AttachmentModel : IBaseModel { public Guid Id { get; set; } public string Name { get; set; } public int Size { get; set; } public string ContentType { get; set; } public bool IsNew { get; set; } public bool IsLink { get; set; } public string Link { get; set; } /// /// Casts a obect to the object of type . /// /// An object /// A object filled with data from db public static explicit operator AttachmentModel(Attachment obj) { if (obj == null) return null; bool islink = false; if (!string.IsNullOrEmpty(obj.Link)) { islink = true; } var model = new AttachmentModel { Id = obj.Id, Name = obj.SourceFileName, Size = obj.FileSize.HasValue ? obj.FileSize.Value : 0, ContentType = obj.ContentType, IsNew = false, IsLink = islink, Link = obj.Link }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object public void CopyTo(Attachment dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Id = Id; dbObj.SourceFileName = Name; dbObj.ContentType = ContentType; dbObj.FileSize = Size; dbObj.Link = (Link !=null?Link.ToString():string.Empty); } public AttachmentModel Copy() { return new AttachmentModel() { Id = Guid.NewGuid(), IsNew = true, Link = this.Link, IsLink = this.IsLink, ContentType = this.ContentType, Name = this.Name, Size = this.Size }; } } }