How to create multiple file attachments

Apr 2, 2013 at 7:25 AM
Hi,

I want that my Account class may have multiple file attachment.I am able to create single file attachment. I am facing problem when trying to create multiple file attachment with coll snippet in my account class.
file attachment code:

AttachFile Class contains:

[Key, Hidden]
    public virtual int AttachFileId { get; set; }
    [Hidden]
    public virtual byte[] AttContent { get; set; }
    [Hidden]
    public virtual string AttName { get; set; }
    [Hidden]
    public virtual string AttMime { get; set; }

    public virtual FileAttachment Attachment
    {
        get
        {
            if (AttContent == null) return null;
            return new FileAttachment(AttContent, AttName, AttMime);
        }

    }

    public void AddOrChangeFile(FileAttachment newAttachment)
    {
        AttContent = newAttachment.GetResourceAsByteArray();
        AttName = newAttachment.Name;
        AttMime = newAttachment.MimeType;
    }
And the Account class contains the code for the collection AttachFile:

#region File (collection)
    private ICollection<AttachFile> _File = new List<AttachFile>();
    public virtual ICollection<AttachFile> File
    {
        get
        {  return _File; }
        set
        { _File = value; }
    }

public void AddToFile(AttachFile value)
    {
        if (_File.Contains(value))
        {
            _File.Add(value);
        }
    }

    public void RemoveFromFile(AttachFile value)
    {
        if (_File.Contains(value))
        {
            _File.Remove(value);
        }
    }

    public IList<AttachFile> Choices0RemoveFromFile()
    {
        return _File.ToList();
    }
    #endregion
I am not able create multiple file attachment with this code.

Please help me by giving solutions in which part the code is wrong.

Thanks,
Elma.
Coordinator
Apr 2, 2013 at 11:02 AM
I believe you have a simple bug - in 'AddToFile' it should be 'if (!_File.Contains(value))' rather than 'if (_File.Contains(value))'
Apr 3, 2013 at 9:00 AM
I add if (!_File.Contains(value)). But the result is same as before. I am not able to create multiple file attachment.

Do you have any suggestion to create multiple file attachment?
Coordinator
Apr 3, 2013 at 10:09 AM
Elma,

with that simple change I tested out your code and as far as I could see it worked OK.

In what way do you find that it doesn't work ? Please fully explain what you see and what you would expect to see.

Stef
Apr 3, 2013 at 10:48 AM
Stef,

I have solved the problem by adding a new class "AttachFileRepository":

[DisplayName("Attach Files")]
public class AttachFileRepository : AbstractFactoryAndRepository
{
    public IDomainObjectContainer Container { set; protected get; }


    public AttachFile CreateNewFile(FileAttachment newAttachment)
    {

        AttachFile newFile = Container.NewTransientInstance<AttachFile>();
        newFile.AttContent = newAttachment.GetResourceAsByteArray();
        newFile.AttName = newAttachment.Name;
        newFile.AttMime = newAttachment.MimeType;
        Container.Persist<AttachFile>(ref newFile);
        //return Container.NewTransientInstance<AttachFile>();
        return newFile;
    }

  #region AllFiles
  public IQueryable<AttachFile> AllFiles()
  {
     return Container.Instances<AttachFile>();
  }


}
With this code in the "Find" option menu "create new file" and "all files" options appears.And i am now able to add multiple file attachment.


Thanks,
Elma