public static Attachment AttachmentInsert(Attachment attachment)
        {
            attachment = attachment.Save();

            FeedService.FeedAdd("Created", attachment);

            return attachment;
        }
        public static bool AttachmentDelete(Attachment attachment)
        {
            Attachment.DeleteAttachment(
                new AttachmentCriteria
                    {
                        AttachmentId = attachment.AttachmentId
                    });

            FeedService.FeedAdd("Deleted", attachment);

            return true;
        }
        public static Attachment AttachmentSave(Attachment attachment)
        {
            if (!attachment.IsValid)
            {
                return attachment;
            }

            Attachment result;

            if (attachment.IsNew)
            {
                result = AttachmentService.AttachmentInsert(attachment);
            }
            else
            {
                result = AttachmentService.AttachmentUpdate(attachment);
            }

            return result;
        }
        public static Attachment AttachmentUpdate(Attachment attachment)
        {
            attachment = attachment.Save();

            FeedService.FeedAdd("Updated", attachment);

            return attachment;
        }
        public static Attachment AttachmentInsert(Attachment attachment)
        {
            attachment = attachment.Save();

            return(attachment);
        }
        public AttachmentFormModel Map(Attachment attachment, AttachmentFormModel model, bool ignoreBrokenRules)
        {
            Csla.Data.DataMapper.Map(attachment, model, true);

            model.Tab = "Task";
            model.IsNew = attachment.IsNew;
            model.IsValid = attachment.IsValid;

            if (!ignoreBrokenRules)
            {
                foreach (var brokenRule in attachment.BrokenRulesCollection)
                {
                    this.ModelState.AddModelError(string.Empty, brokenRule.Description);
                }
            }

            return model;
        }
        private void Map(FormCollection source, Attachment destination)
        {
            destination.Name = source["Name"];

            var file = this.Request.Files["FileData"];

            if (file != null)
            {
                var fileData = new byte[file.ContentLength];

                file.InputStream.Read(fileData, 0, file.ContentLength);

                destination.FileData = fileData;

                if (string.IsNullOrEmpty(destination.Name))
                {
                    destination.Name = Path.GetFileName(file.FileName);
                }

                destination.FileType = file.ContentType;
            }
        }