Вот упрощенный пример: < /p>
Код: Выделить всё
Employee
Код: Выделить всё
namespace ProjectName.DataAccess.Entities;
public class Employee
{
private int _employeeId;
private byte[]? _taxCode;
// other properties ...
public required int EmployeeId
{
get => _employeeId;
set => _employeeId = value;
}
public required byte[]? TaxCode
{
get => _taxCode;
set => _taxCode = value;
}
}
< /code>
EmployeeDto
Код: Выделить всё
using System.ComponentModel;
namespace ProjectName.DTOs;
public class EmployeeDto
{
private int _employeeId;
private string? _taxCode;
// other properties ...
public int EmployeeId
{
get => _employeeId;
set => _employeeId = value;
}
public string? TaxCode
{
get => _taxCode;
set => _taxCode = value;
}
}
< /code>
EmployeeMapper
Код: Выделить всё
using ProjectName.DataAccess.Entities;
using ProjectName.DTOs;
namespace ProjectName.Business.Mappings;
static class EmployeeMapper
{
public static EmployeeDto ToDto(this Employee entity)
{
return new EmployeeDto()
{
EmployeeId = entity.EmployeeId,
// I intend to put a decrypt method directly here.
// For example: TaxCode = AesHelper.Decrypt(entity.TaxCode)
TaxCode = entity.TaxCode,
// other properties ...
};
}
}
< /code>
AesHelper
Код: Выделить всё
static class AesHelper
{
public static string Decrypt(byte[] cipherText)
{
return /* data decrypted */;
}
}
< /code>
My current code looks like this:
EmployeeBusiness
Код: Выделить всё
namespace ProjectName.Business;
public class EmployeeBusiness
{
public EmployeeDto? GetEmployeeByEmployeeId(int employeeID)
{
Employee? employee = EmployeeDataAccess
.Instance
.GetEmployeeByEmployeeId(employeeID);
// I'm using ProjectName.Business.Mappings.EmployeeMapper here
return employee?.ToDto();
}
}
< /code>
EmployeeDataAccess
Код: Выделить всё
namespace ProjectName.DataAccess;
public class EmployeeDataAccess
{
public Employee? GetEmployeeByEmployeeId(int employeeId)
{
string query = @"
SELECT EmployeeId
, TaxCode
-- other columns
FROM Employee
WHERE EmployeeId = u/EmployeeId
";
List parameters = [];
parameters.Add("EmployeeId", SqlDbType.Int, employeeId);
DataTable dataTable = DataProvider.Instance.ExecuteQuery(
query,
[.. parameters]
);
if (dataTable.Rows.Count == 0)
{
return null;
}
DataRow row = dataTable.Rows[0];
// this is another mapper in ProjectName.DataAccess.Mappings
// that maps from DataRow -> Entities.Employee
return EmployeeMapper.FromDataRow(row);
}
}
< /code>
My questions are:
[*]Where should I put the encryption/decryption logic?
If I put it directly inside the mapper (e.g., calling AesHelper.Decrypt
Каково ваше мнение? Как вы обычно обрабатываете картирование DTO при зашифрованных полях?
Подробнее здесь: https://stackoverflow.com/questions/797 ... pper-class