110,538
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 问题在于特性不能使用泛型,这里的BaseLimitEntity是model的基类型,包含需要过滤的属性。
/// </summary>
/// <typeparam name="T"></typeparam>
public class DataFilterAttribute<T> : HandlerAttribute where T : BaseLimitEntity
{
public string HospitalIds { get; set; }
public int Order { get; set; } // 这是ICallHandler的成员,表示执行顺序
public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
{
// 不用泛型的话,问题是这里怎么传T进去?
return new DataFilterHandler<T>(Order, HospitalIds);
}
}
public class DataFilterHandler<T> : ICallHandler where T : BaseLimitEntity
{
public string HospitalIds { get; set; }
public int Order { get; set; } // 这是ICallHandler的成员,表示执行顺序
private IList<int> hospitalIds;
public DataFilterHandler(int order, string hospitalIds)
{
this.Order = order;
this.HospitalIds = hospitalIds;
this.hospitalIds = new List<int>();
string[] ids = hospitalIds.Split(new char[1] { ',' });
foreach (string id in ids)
{
this.hospitalIds.Add(Convert.ToInt32(id));
}
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
// 调用被劫持的方法,返回的类型是IQueryable<T>
var returnValue = getNext()(input, getNext);
// 进行数据权限过滤
IQueryable<T> query = (IQueryable<T>)returnValue.ReturnValue;
returnValue.ReturnValue = query.Where(e => hospitalIds.Contains(e.HospitalId)).AsQueryable();
return returnValue;
}
}
[DataFilter(HospitalIds = "2,3", Order = 1)]
IQueryable<T> FindAll (Expression<Func<T, bool>> filter);
[SerializableAttribute]
[TypeConverterAttribute(typeof(PointConverter))]
public struct Point : IFormattable
{
...
}
这种在attribute中传type的例子太多了。