I have the following classes:
QuestionnaireListDto.cs
namespace Services.DTOs
{
public class QuestionnaireListDto
{
public IList<QuestionnaireEntryDto> Questionnaires;
public QuestionnaireListDto(IList<QuestionnaireEntryDto> questionnaires)
{
Questionnaires = questionnaires;
}
}
}
QuestionnaireEntryDto.cs
namespace Services.DTOs
{
public class QuestionnaireEntryDto
{
// Omitting several properties here for brevity...
public int Id { get; }
public int QnreId { get; }
// Other properties here...
public QuestionnaireEntryDto(QnrsMailed qnrsMailed)
{
Id = qnrsMailed.Id;
QnreId = qnrsMailed.QnreId;
// Initialize other members...
}
}
}
These DTO classes are initialized by a separate library. Then these DTOs are passed to a Razor view. I am using ASP.NET Core Data Protection to encrypt/decrypt the Id values above.
What I'm trying to do is "attach" an encrypted version of the Id property above. The Data Protection API creates a string version of the Id. I would like to be able to do this without having to create a separate "EncryptedId" property. When the DTOs are constructed, I do not know the encrypted ID at this point. I also want the encryption/decryption to occur in the Controller class if possible.
I can't think of a clean way to do this. The closest thing I can come up with so far is create a generic wrapper class that includes an EncryptedId property, something like:
public class EncryptedDto<T> where T : class
{
public string EncryptedId { get; }
public T Dto { get; }
public EncryptedDto(string encryptedId, T dto)
{
EncryptedId = encryptedId
Dto = dto;
}
}
Of course, to do this, I would have to loop through the list of DTOs and construct one of these for each DTO object. And also create a new DTO class that can store a list of these generic objects instead. This still just seems messy/cumbersome. Is there a better way?