using System;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using IQToolkit;
namespace IQToolkitContrib.Web {
public class DataSourceView : LinqDataSourceView {
private LinqDataSource owner;
public DataSourceView(LinqDataSource owner, string name, HttpContext context)
: base(owner, name, context) {
this.owner = owner;
}
/// <summary>
/// Make sure that the data context has a property that implements IEntityTable
/// </summary>
protected override void ValidateContextType(Type contextType, bool selecting) {
if (!selecting && contextType.GetProperties().Where(p => p.PropertyType.GetInterface("IEntityTable") != null).Count() == 0) {
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The data context used by IQToolkit-DataSourceView '{0}' must have an IEntityTable Property when the Delete, Insert or Update operations are enabled.", this.owner.ID));
}
}
/// <summary>
/// Make sure that the table implementes IEntityTable
/// </summary>
protected override void ValidateTableType(Type tableType, bool selecting) {
if (!selecting && (!tableType.IsGenericType || tableType.GetInterface("IEntityTable") == null)) {
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The table property used by IQToolkit-DataSourceView '{0}' must extend IEntityTable when the Delete, Insert or Update operations are enabled.", this.owner.ID));
}
}
protected override void DeleteDataObject(object dataContext, object table, object oldDataObject) {
((IEntityTable)table).Delete(oldDataObject);
}
protected override void UpdateDataObject(object dataContext, object table, object oldDataObject, object newDataObject) {
((IEntityTable)table).Update(newDataObject);
}
protected override void InsertDataObject(object dataContext, object table, object newDataObject) {
((IEntityTable)table).Insert(newDataObject);
}
}
}