EnVisageOnline/Main-RMO/Source/EnVisage/Models/AttachmentModel.cs

55 lines
1.6 KiB
C#

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<Attachment>
{
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; }
/// <summary>
/// Casts a <see cref="Attachment"/> obect to the object of type <see cref="AttachmentModel"/>.
/// </summary>
/// <param name="obj">An <see cref="Attachment"/> object</param>
/// <returns>A <see cref="AttachmentModel"/> object filled with data from db</returns>
public static explicit operator AttachmentModel(Attachment obj)
{
if (obj == null)
return null;
var model = new AttachmentModel
{
Id = obj.Id,
Name = obj.SourceFileName,
Size = obj.FileSize.HasValue ? obj.FileSize.Value : 0,
ContentType = obj.ContentType,
IsNew = false,
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object</param>
public void CopyTo(Attachment dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Id = Id;
dbObj.SourceFileName = Name;
dbObj.ContentType = ContentType;
dbObj.FileSize = Size;
}
}
}