tags:

views:

58

answers:

2

I have an object defined as

public class EmailData
    {
        public string TemplateId { get; set;}
        public IEnumerable<Recipient> To { get; set; }
        public IEnumerable<Recipient> CC { get; set; }
        public IEnumerable<Recipient> BCC { get; set; }
    }

public class Recipient
{
    public string Key { get; set; }
    public string Type { get; set; }
    public string Email { get; set; }
    public string Id { get; set; }
}

I have that populated in a constructor, then I need to finish populating the Recipient values and in a nutshell I'm going this (after various attempts):

public EmailData EmailObject;

public void BuildEmailData()
{
   EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
   EmailObject = builder.BuildObject();
}

public void PopulateEmailAddresses()
{
   SetToValues();
}

private void SetToValues()
{
   foreach (Recipient t in EmailObject.To)
   {
       var _user = RepoUser.GetUserById(t.Key);
       t.Email = _user.Email;
       t.Id = _user.Id;
   }
}

So you see that I have an object and I need to populate a couple more fields on it. Now, I get that the foreach look creates a variable that I'm using so when I assign the Email value, it's not the the data arg, but rather to the variable. But how do I get around that? Am I missing something simpler with the foreach?

Many thanks, this was bombing my unit tests before I realized that the assignments weren't working.

What I've gotten working that I don't like is to use a collection of Recipients instead of the IEnumerable directly:

public class EmailData
    {
        public string TemplateId { get; set;}
        public RecipientCollection To { get; set; }
        public RecipientCollection CC { get; set; }
        public RecipientCollection BCC { get; set; }
    }

 public class RecipientCollection: IEnumerable<Recipient>
    {
        private readonly List<Recipient> variableList = new List<Recipient>();

        public IEnumerator<Recipient> GetEnumerator()
        {
            return variableList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(Recipient v)
        {
            variableList.Add(v);
        }
    }

 public EmailData BuildObject()
        {
            // get the template.
            EmailObj.TemplateId = _templateRepo.GetCSGEmailTemplate(CSGTemplateId).TemplateId;

            // populate the people
            RecipientCollection _toCollection = new RecipientCollection();
            RecipientCollection _ccCollection = new RecipientCollection();
            RecipientCollection _bccCollection = new RecipientCollection();

            foreach (var r in _recipRepo.GetRecipientByAction("To"))
            { _toCollection.Add(r); }
            EmailObj.To = _toCollection;

            foreach (var r in _recipRepo.GetRecipientByAction("CC"))
            { _ccCollection.Add(r); }
            EmailObj.CC = _ccCollection;

            foreach (var r in _recipRepo.GetRecipientByAction("BCC"))
            { _bccCollection.Add(r); }
            EmailObj.BCC = _bccCollection;

            return EmailObj;
        }

 public void BuildEmailData()
        {
            EmailDataBuilder builder = new EmailDataBuilder(EmailObject,CSGTemplateId);
            EmailObject = builder.BuildObject();
        }

public void PopulateEmailAddresses()
        {
            foreach (Recipient t in EmailObject.To)
            {
               var _user = RepoUser.GetUserById(t.Key);
               t.Email = _user.Email;
               t.Id = _user.Id;
             }
        }

It's not ideal as is breaks another piece to this which uses Linq to XML but I can figure that out.

+1  A: 

Is Recipient a Struct? Change it to a class perhaps.

Noel Abrahams
it's a public class
BryanGrimes
+1  A: 

Since .To has set, do:

  • create a new list of recipients
  • foreach original recipient construct new and insert it into the new list
  • assign new list to .To

That should work.

Daniel Mošmondor