时间:2021-07-01 10:21:17 帮助过:34人阅读
public abstract class DataSourceView { public virtual void Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback); protected abstract IEnumerable ExecuteSelect( DataSourceSelectArguments arguments); ... } |
public abstract class AsyncDataSourceControl : DataSourceControl, IAsyncDataSource { private bool _performAsyncDataAccess; protected AsyncDataSourceControl() { _performAsyncDataAccess = true; } public virtual bool PerformAsyncDataAccess { get; set; } bool IAsyncDataSource.IsAsync { get { return _performAsyncDataAccess && Page.IsAsync; } } } public abstract class AsyncDataSourceView : DataSourceView { protected abstract IAsyncResult BeginExecuteSelect( DataSourceSelectArguments arguments, AsyncCallback asyncCallback, object asyncState); protected abstract IEnumerable EndExecuteSelect( IAsyncResult asyncResult); protected override IEnumerable ExecuteSelect( DataSourceSelectArguments arguments) { //实现从 DataSourceView 中继承的 //抽象 ExecuteSelect 方法, //方法是使用 BeginExecuteSelect 和 EndExecuteSelect, //以便通过阻止来 //进行同步数据访问。 } private IAsyncResult OnBeginSelect(object sender, EventArgs e, AsyncCallback asyncCallback, object extraData); private void OnEndSelect(IAsyncResult asyncResult); public override void Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) { if (_owner.IsAsync) { //使用 OnBeginSelect 和 OnEndSelect //作为 BeginEventHandler 和 EndEventHandler 方法, //来调用 Page.RegisterAsyncTask, //以指明需要 //进行异步工作。这些方法将依次 //调用特定的 //数据源实现,方法是调用 //已在此类中引入的 //抽象 BeginExecuteSelect 和 EndExecuteSelect //方法。 } else { //执行同步数据访问 base.Select(arguments, callback); } } ... } |
public class AsyncWeatherDataSource : AsyncDataSourceControl { //与 WeatherDataSource 相同 } private sealed class AsyncWeatherDataSourceView : AsyncDataSourceView { private AsyncWeatherDataSource _owner; private WeatherService _weatherService; public AsyncWeatherDataSourceView(AsyncWeatherDataSource owner, string viewName) : base(owner, viewName) { _owner = owner; } protected override IAsyncResult BeginExecuteSelect(DataSourceSelectArguments arguments, AsyncCallback asyncCallback, object asyncState) { arguments.RaiseUnsupportedCapabilitiesError(this); string zipCode = _owner.GetSelectedZipCode(); if (zipCode.Length == 0) { return new SynchronousAsyncSelectResult(/* selectResult */ null, asyncCallback, asyncState); } _weatherService = new WeatherService(zipCode); return _weatherService.BeginGetWeather(asyncCallback, asyncState); } protected override IEnumerable EndExecuteSelect(IAsyncResult asyncResult) { SynchronousAsyncSelectResult syncResult = asyncResult as SynchronousAsyncSelectResult; if (syncResult != null) { return syncResult.SelectResult; } else { Weather weatherObject = _weatherService.EndGetWeather(asyncResult); _weatherService = null; if (weatherObject != null) { return new Weather[] { weatherObject }; } } return null; } } |