Reputation: 1298
I have a class as follows:
public class RecipientEmailDetails
{
public string Name { get; set; }
public string Email { get; set; }
public List<VacancyEmailDetails> Vacancies { get; set; }
}
This class needs to be changed to allow Vacancies to take in generic data for example as below:
public class RecipientEmailDetails<T>
{
public string Name { get; set; }
public string Email { get; set; }
public List<T> Vacancies { get; set; }
}
Unfortunately at present if I do this I'll need to change all the code that instantiates this class (which is many, many locations) to include this new generic type for example changing this:
List<RecipientEmailDetails> recipientEmailDetails = wrapper.GetRecipientEmails(emailCount);
To this:
List<RecipientEmailDetails<VacancyEmailDetails> recipientEmailDetails = wrapper.GetRecipientEmails(emailCount);
Ultimately this is a lot of changes and many of these current locations will have no knowledge (and ideally should have no knowledge) of what type of data is stored in Vacancies. They will simply forward this information on until eventually the Vacancies list is checked and processed.
Anyway cut to the chase I'm looking for clever ways to store generic data in Vacancies without having to change everywhere how the RecipientEmailDetails object is initialised. I'd rather not have to box it and unbox it.
I should add that VacancyEmailDetails is currently like this:
public class VacancyEmailDetails
{
public string Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string Salary { get; set; }
public string Notes { get; set; }
}
In all cases Vacancies will be rquired to hold objects similar to this one i.e. mainly data members with little or few methods. However the number, names and types of these members will vary considerably.
Upvotes: 0
Views: 89
Reputation: 1298
Thanks for the reponses. I decided against the Interface and instead redefined the class to this:
public class RecipientEmailDetails
{
public string Name { get; set; }
public string Email { get; set; }
public List<Dictionary <string, string>> Vacancies { get; set; }
}
The only thing then that had to change where a few lines such as these:
new RecipientEmailDetails{
Name = "Test1",
Email = "[email protected]",
Vacancies = vacancies.Select(z => new VacancyEmailDetails{
Id = z.Id.ToString(),
Notes = z.Notes
}).ToList()};
To these:
new RecipientEmailDetails{
Name = "Test1",
Email = "[email protected]",
Vacancies = vacancies.Select(z => new []{
new {Key = "Id", Value = z.Id.ToString()},
new {Key = "Notes", Value = z.Notes}}.ToDictionary(d => d.Key, d => d.Value)).ToList()})
This allowed me to do what I required and only to have to be explicitly concerned with the contents of Vacancies when I was populating and extracting the data from it and not everywhere RecipientEmailDetails was referenced.
Upvotes: 0
Reputation: 50728
You could use an interface to define what's allowed, like:
public class RecipientEmailDetails
{
public string Name { get; set; }
public string Email { get; set; }
public List<ISomeCommonInterface> Vacancies { get; set; }
}
And all classes implement this.
Upvotes: 1
Reputation: 4489
Introduce an interface IRecipientEmailDetails
, make your class to implement it and change the GetRecipientEmails
method to return this interface.
public interface IRecipientEmailDetails {}
And the method in your class:
public IRecipientEmailDetails GetRecipientEmails(string emailCount) { }
One way or another, you will have to change your code.
Upvotes: 1