使用している監査ライブラリがあります。問題のメソッドと同様のメソッドを記述する方法がわからないため、この質問はジェネリックに関係していると思います。
次の「連鎖」メソッドがあり、ルックアップ テーブルへのコンテキストを取得できないようです。
AuditProperty(m => m.StationTypeId)
.GetValueFrom(stationtypeId => GetStationType(stationtypeId))
.WithPropertyName("StationType");
アイデアは、ID を GetValueFrom() linq メソッドに渡し、(この場合は) stationtype テーブルから文字列を返すことです。実行時に実際の Stationtype テーブル (以下の stationTypeTable) に静的テーブルを割り当てることで機能するようになったので、次のようにルックアップを実行できます。
public string GetStationType(int? stationTypeID)
{
var stationtype = stationTypeTable.FirstOrDefault(st => object.Equals(st.Id, stationTypeID));
return stationtype != null ? stationtype.Value : String.Empty;
}
私はこれが悪い習慣であることを知っています。一部の主キー ID が存在しない場合、例外が発生します。しかし、linq メソッドを呼び出すと、実際にはどのテーブルにもコンテキストを取得できないようです。これを正しく行う方法はありますか?linq メソッドは以下のとおりです。
public class EntityAuditConfiguration<TEntity> : EntityAuditConfiguration
{
/// <summary>
/// Customize the default behavior when auditing a specific property
/// </summary>
public CustomPropertyAuditor<TEntity, T> AuditProperty<T>(Expression<Func<TEntity, T>> propertySelector)
{
var config = new CustomPropertyAuditor<TEntity, T>();
CustomizedProperties.Add(propertySelector.ToPropertyInfo(), config);
return config;
}
/// <summary>
/// Include an association (relationship) table to audit along with the parent table
/// </summary>
public RelatationshipConfiguration<TChildEntity, TEntity> AuditMany<TChildEntity>(Expression<Func<TEntity, IEnumerable<TChildEntity>>> selector)
where TChildEntity : class
{
var relationship = new RelatationshipConfiguration<TChildEntity, TEntity>();
((IEntityAuditConfiguration) this).Relationships.Add(relationship);
return relationship;
}
}
public class CustomPropertyAuditor<TEntity, TProp> : IPropertyAuditor
{
private Expression<Func<TProp, string>> _propertySelector;
private string _propertyName;
public CustomPropertyAuditor<TEntity, TProp> WithPropertyName(string propertyName)
{
if (propertyName == null) throw new ArgumentNullException("propertyName");
_propertyName = propertyName;
return this;
}
public CustomPropertyAuditor<TEntity, TProp> GetValueFrom(Expression<Func<TProp, string>> valueSelector)
{
if (valueSelector == null) throw new ArgumentNullException("valueSelector");
_propertySelector = valueSelector;
return this;
}
AuditedProperty IPropertyAuditor.AuditProperty(PropertyInfo property, object oldValue, object newValue)
{
var auditedProperty = new AuditedProperty(_propertyName ?? property.Name);
var func = _propertySelector.Compile();
if (oldValue != null)
auditedProperty.OldValue = func.DynamicInvoke(oldValue).ToString();
if (newValue != null)
auditedProperty.NewValue = func.DynamicInvoke(newValue).ToString();
return auditedProperty;
}
}
ありがとう!