我想使用ASP。. NET动态数据4.0与EF POCO ObjectContext作为系统管理员的简单实体管理控制台(我正在考虑放弃这项技术…)。
插入/更新工作
我已经弄清楚如何为额外的数据类型添加过滤器(因为内置的过滤器是无用的)。我不知道如何使QueryableFilterRepeater暴露我的列作为过滤。我不能将FilterUIHints添加到所有适用的类型,所以这不是一个可接受的解决方案。我确实想利用内置的实体框架元数据模型提供程序。我不想自己写。问题似乎归结为QueryableFilterIterator调用mettable . getfilteredcolumns(),它只返回bool/int/DateTime列(这是无用的)。
我检查了ASP。. NET动态数据过滤(http://dynamicdatafiltering.codeplex.com/),但它似乎没有维护4.0。
我想我有两个问题:
- 是否有办法做到这一点(让我的列可过滤)?
- 动态数据实际上适合在现实世界中使用吗?
感到沮丧,选择了直接路线。如果没有更好的解决办法…那么,微软在这方面做得真的很糟糕:
public class QueryableFilterRepeater : System.Web.DynamicData.QueryableFilterRepeater
{
private static readonly FieldInfo FiltersField = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetField("_filters", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo DataSourceField = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetField("_dataSource", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly MethodInfo FilterInitializeMethod = typeof(DynamicFilter).GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly PropertyInfo FilterContextProperty = typeof(DynamicFilter).GetProperty("Context", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
private static readonly MethodInfo PageInitCompleteMethod = typeof(System.Web.DynamicData.QueryableFilterRepeater).GetMethod("Page_InitComplete", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
private static readonly EventInfo InitCompleteEvent = typeof(Page).GetEvent("InitComplete");
private readonly List<DynamicFilter> filters;
private int filterCount;
private bool initialized;
public IEnumerable<DynamicFilter> Filters
{
get { return filters; }
}
public QueryableFilterRepeater()
{
filters = (List<DynamicFilter>)FiltersField.GetValue(this);
}
private IQueryableDataSource DataSource
{
get { return DataSourceField.GetValue(this) as IQueryableDataSource; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitCompleteEvent.RemoveEventHandler(Page, Delegate.CreateDelegate(typeof(EventHandler), this, PageInitCompleteMethod));
Page.InitComplete += new EventHandler(Page_InitComplete);
}
protected override void OnLoad(EventArgs e)
{
filters.Select((f, i) => new { f, i }).Where(x => x.i > filterCount - 1).ToList().ForEach(x => filters.Remove(x.f));
Controls.OfType<Control>().Select((c, i) => new { c, i }).Where(x => x.i > filterCount - 1).ToList().ForEach(x => Controls.Remove(x.c));
base.OnLoad(e);
}
private void Page_InitComplete(object sender, EventArgs e)
{
if (!initialized)
{
Controls.Clear();
filters.Clear();
MetaTable metaTable = DataSource.GetMetaTable();
int num = 0;
foreach (MetaColumn column in metaTable.Columns)
{
string filterUIHint = GetFilterUIHint(column);
if (filterUIHint == null) continue;
var filterRepeaterItem = new FilterRepeaterItem();
filterRepeaterItem.DataItemIndex = num;
filterRepeaterItem.DisplayIndex = num;
FilterRepeaterItem container = filterRepeaterItem;
num++;
ItemTemplate.InstantiateIn(container);
Controls.Add(container);
var dynamicFilter = container.FindControl(DynamicFilterContainerId) as DynamicFilter;
if (dynamicFilter == null)
{
throw new InvalidOperationException();
}
FilterContextProperty.SetValue(dynamicFilter, new HttpContextWrapper(Context), null);
dynamicFilter.DataField = column.Name;
container.DataItem = column;
container.DataBind();
container.DataItem = null;
dynamicFilter.FilterUIHint = filterUIHint;
filters.Add(dynamicFilter);
filterCount++;
}
filters.ForEach(f => FilterInitializeMethod.Invoke(f, new[] { DataSource }));
initialized = true;
}
}
private string GetFilterUIHint(MetaColumn column)
{
if (GetUnderlyingType(column.ColumnType) == typeof(string))
{
return "String";
}
if (GetUnderlyingType(column.ColumnType) == typeof(bool))
{
return "Boolean";
}
if (GetUnderlyingType(column.ColumnType).IsEnum)
{
return "Enumeration";
}
if (GetUnderlyingType(column.ColumnType) == typeof(DateTime))
{
return "DateTime";
}
if (column is MetaForeignKeyColumn)
{
return "ForeignKey";
}
if (column is MetaChildrenColumn)
{
return "Children";
}
return null;
}
private Type GetUnderlyingType(Type type)
{
return Nullable.GetUnderlyingType(type) ?? type;
}
// Nested Types
#region Nested type: FilterRepeaterItem
private class FilterRepeaterItem : Control, IDataItemContainer
{
// Properties
#region IDataItemContainer Members
public object DataItem { get; internal set; }
public int DataItemIndex { get; internal set; }
public int DisplayIndex { get; internal set; }
#endregion
}
#endregion
}