【发布时间】:2020-06-17 19:44:09
【问题描述】:
我知道这个一般性问题在很多领域都得到了解决,但不适用于这种特定情况。我有以下对象
public class AuditRecord
{
public long Id {get; private set; }
public Collaborator IssuedBy { get; private set; }
}
public class Collaborator
{
public Collaborator(Guid userId, string name, string email)
{
UserId = userId;
Name = name;
Email = email;
}
public Guid UserId { get; }
public string Name { get; }
public string Email { get; }
}
如您所见,IssuedBy 属性有一个私有设置器。有没有一种 Dapper 原生的方式来处理这个问题?由于私有访问器,传递具有这些部分的函数的典型示例将不起作用。
var resultData = await _connection.QueryAsync<AuditRecord, Collaborator, AuditRecord>(
AUDIT_INSERT_SQL,
(o, collaborator) =>
{
o.IssuedBy = collaborator;
// Error CS0272
// The property or indexer 'AuditRecord.IssuedBy' cannot be used in this
// context because the set accessor is inaccessible
return o;
}
);
将属性公开或添加执行相同操作的方法本质上违反了封装规则。
【问题讨论】: