我正在尝试让所有实体实现'IEquatable',但我仍然坚持使用T4模板:
我已经将开场方法改为以下内容:
public string EntityClassOpening(EntityType entity)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1}partial class {2}{3}{4}{5}",
Accessibility.ForType(entity),
_code.SpaceAfter(_code.AbstractOption(entity)),
_code.Escape(entity),
_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)),
string.IsNullOrEmpty(_code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))) ? _code.StringBefore(" : ", "BaseEntity<" + _code.Escape(entity) + ">") : "",
string.IsNullOrEmpty(_code.StringBefore(", ", _typeMapper.GetTypeName(entity.BaseType))) ? _code.StringBefore(", ", "IEquatable<" + _code.Escape(entity) + ">") : "");
}
哪个让我好,例如public partial class TableName : BaseEntity<TableName>, IEquatable<TableName>
而BaseEntity只是一个包装类。
我现在需要的是创建IEquatable
的实现,它应该是:
#region implementation of IEquatable
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(TableName other) => TableNameId == other.TableNameId;
#endregion
包含区域,注释和语句本身。我的表命名约定允许通过在Id
前加上表的名称来构建id-property,因此MyTable - &gt; MyTableId,OtherTable - &gt; OtherTableId。
只有2或3个表不遵循这些约定,所以我还需要检查当前entity
,如:
// check if we have an 'special' entity
if(currentEntity.Name == "NonDefaultTable"){
// set Equals to return NonDefaultTableKey == other.NonDefaultTableKey;
}
非常感谢任何帮助。