0

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?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Andrew
  • 1,581
  • 3
  • 18
  • 31
  • What is the purpose of the encrypted id vs just using the id? – ProgrammingLlama Apr 17 '19 at 05:17
  • I am doing this to encrypt the query strings. So the URL would look something like: http://www.test.com/Controller/Action?id=fweifjewjfioejfewio... It's a requirement from the client. – Andrew Apr 17 '19 at 05:19
  • You could use extensions for your DTOs. There's a similar discussion here: https://stackoverflow.com/questions/619033/does-c-sharp-have-extension-properties – Mihai Apr 17 '19 at 05:38

1 Answers1

0

There is nothing wrong with your solution. Remapping DTOs is a common practice. If you think your mapping code is messy you can use Automapper for doing that stuff. Another workaround is to use a static class to delegate encryption/decryption. And whenever you use and id inside a view you would call that class' encrypt method so that only encrypted id would be rendered.