CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
AsyncLoadManager.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AsyncLoadManager.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Linq;
10using System.Collections.ObjectModel;
11using Csla.Properties;
12
14{
15 internal class AsyncLoadManager : INotifyBusy
16 {
17 private IManageProperties _target;
18 private Action<IPropertyInfo> _onPropertyChanged;
19
20 public AsyncLoadManager(IManageProperties target, Action<IPropertyInfo> onPropertyChanged)
21 {
22 _target = target;
23 _onPropertyChanged = onPropertyChanged;
24
25 }
26
27 private object _syncRoot = new object();
28 private readonly ObservableCollection<IAsyncLoader> _loading = new ObservableCollection<IAsyncLoader>();
29
30 public bool IsLoading
31 {
32 get
33 {
34 lock (_syncRoot)
35 return _loading.Any();
36 }
37 }
38
39 private bool IsAlreadyLoadingProperty(IPropertyInfo property)
40 {
41 lock (_syncRoot)
42 {
43 return _loading.Any(l => l.Property == property);
44 }
45 }
46
47 public void BeginLoad(IAsyncLoader loader)
48 {
49 if (IsAlreadyLoadingProperty(loader.Property)) return;
50
51 lock (_syncRoot)
52 {
53 _loading.Add(loader);
54 }
55 // notify property busy
56 OnPropertyBusy(loader.Property.Name, true);
57
58 // start async loading
59 loader.Load(LoaderComplete);
60 }
61
62 void LoaderComplete(IAsyncLoader loader, IDataPortalResult e)
63 {
64 // remove from loading list
65 lock (_syncRoot)
66 {
67 _loading.Remove(loader);
68 }
69
70 // no error then load new property value and notify property changed
71 if (e.Error == null)
72 {
73 _target.LoadProperty(loader.Property, e.Object);
74 _onPropertyChanged(loader.Property);
75 }
76
77 // mark property as not busy
78 OnPropertyBusy(loader.Property.Name, false);
79
80 // if error raise OnUnhandledAsyncException event
81 if (e.Error != null)
82 OnUnhandledAsyncException(this, new AsyncLoadException(loader.Property,string.Format(Resources.AsyncLoadException, loader.Property.FriendlyName), e.Error));
83 }
84
85 #region INotifyPropertyBusy Members
86
87 public event BusyChangedEventHandler BusyChanged;
88 protected void OnPropertyBusy(string propertyName, bool busy)
89 {
90 if (BusyChanged != null)
91 BusyChanged(this, new BusyChangedEventArgs(propertyName, busy));
92 }
93
94 #endregion
95
96 #region INotifyBusy Members
97
98 bool INotifyBusy.IsBusy
99 {
100 get { return (this as INotifyBusy).IsSelfBusy; }
101 }
102
103 bool INotifyBusy.IsSelfBusy
104 {
105 get { return IsLoading; }
106 }
107
108 #endregion
109
110 #region INotifyUnhandledAsyncException Members
111
112 [NotUndoable]
113 [NonSerialized]
114 private EventHandler<ErrorEventArgs> _unhandledAsyncException;
115
116 public event EventHandler<ErrorEventArgs> UnhandledAsyncException
117 {
118 add { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Combine(_unhandledAsyncException, value); }
119 remove { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Remove(_unhandledAsyncException, value); }
120 }
121
122 protected virtual void OnUnhandledAsyncException(ErrorEventArgs error)
123 {
124 if (_unhandledAsyncException != null)
125 _unhandledAsyncException(this, error);
126 }
127
128 protected void OnUnhandledAsyncException(object originalSender, Exception error)
129 {
130 OnUnhandledAsyncException(new ErrorEventArgs(originalSender, error));
131 }
132
133 #endregion
134 }
135}
A strongly-typed resource class, for looking up localized strings, etc.
static string AsyncLoadException
Looks up a localized string similar to Failed on async load of property {0}.
delegate void BusyChangedEventHandler(object sender, BusyChangedEventArgs e)
Delegate for handling the BusyChanged event.