扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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; } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者