LinqtoSql中的链接方法:如何获取实体上下文

时间:2012-12-12 18:23:45

标签: c# linq generics audit

我有一个我正在使用的审计库。我相信这个问题更多地与泛型有关,因为我不确定如何编写类似于有问题的方法。

我有以下“链式”方法,似乎无法获取查找表的上下文:

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;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我只是创建了一个列表,并在我班级的oncreate()方法中将每一行添加到列表中。这些查找表应该在上下文中可用,我不应该创建一个单独的查找列表(但它现在100%工作)。任何人都有其他更好的想法吗?