3using Microsoft.VisualStudio.TestTools.UnitTesting;
5using System.Collections.Generic;
6using System.ComponentModel;
7using System.Diagnostics;
10using System.Threading.Tasks;
20 public enum enumEvent { OnPropertyChanged, OnChildChanged, PropertyChanged, ChildChanged };
32 return $
"{SequenceID}: {BO.ToString()} {UniqueID} {Depth} {Event.ToString()} {PropertyName}";
36 private static int _UniqueID = 0;
39 private static int _SequenceID = 0;
42 protected static List<EventDetail>
EventDetails =
new List<EventDetail>();
49 public int Depth {
get;
private set; }
75 base.AddBusinessRules();
88 BusinessRules.CheckRules();
103 BusinessRules.CheckRules();
109 base.OnPropertyChanged(propertyName);
115 base.OnChildChanged(e);
124 public int Depth {
get;
private set; }
130 Add(childDataPortal.FetchChild(depth));
136 base.OnPropertyChanged(e);
142 base.OnChildChanged(e);
147 private SimpleBO Fetch()
151 var result = dataPortal.
Fetch();
153 void HookEvents(SimpleBO bo)
155 bo.PropertyChanged += Result_PropertyChanged;
156 bo.ChildChanged += Result_ChildChanged;
158 if (bo.Child !=
null)
160 bo.ChildList.ChildChanged += ChildList_ChildChanged;
162 HookEvents(bo.Child);
163 bo.ChildList.ToList().ForEach(c => HookEvents(c));
179 var list = sender as SimpleBOList ??
throw new ArgumentNullException(
"Not a SimpleBOList");
185 var bo = sender as SimpleBO ??
throw new ArgumentNullException(
"Not a SimpleBO");
189 private void Result_PropertyChanged(
object sender, PropertyChangedEventArgs e)
191 var bo = sender as SimpleBO ??
throw new ArgumentNullException(
"Not a SimpleBO");
192 EventDetails.Add(
new EventDetail() { BO =
enumBO.SimpleBO, Depth = bo.Depth, Event =
enumEvent.PropertyChanged, UniqueID = bo.UniqueID, PropertyName = e.PropertyName });
198 _testDIContext = TestDIContextFactory.CreateDefaultContext();
204 var result = Fetch();
206 Assert.IsNotNull(result.Child);
207 Assert.IsNotNull(result.ChildList);
209 Assert.IsNotNull(result.Child.Child);
210 Assert.IsNotNull(result.Child.ChildList);
212 Assert.IsNotNull(result.Child.Child.Child);
213 Assert.IsNotNull(result.Child.Child.ChildList);
215 Assert.IsTrue(result.IsValid);
216 Assert.IsFalse(result.IsNew);
217 Assert.IsFalse(result.IsDirty);
224 var result = Fetch();
226 result.Name =
"Keith";
239 var result = Fetch();
241 result.Child.Name =
"Keith";
255 var result = Fetch();
257 result.Child.Child.Name =
"Keith";
272 var result = Fetch();
277 result.Child.Child.Child.Name =
"Keith";
293 var result = Fetch();
295 result.ChildList[0].Name =
"Keith";
309 var result = Fetch();
311 result.ChildList[0].ChildList[0].Name =
"Keith";
327 var result = Fetch();
332 result.ChildList[0].ChildList[0].ChildList[0].Name =
"Keith";
345 private void WriteEventDetails()
349 EventDetails.ForEach(ev => Debug.WriteLine(ev.ToString()));
350 Debug.WriteLine(
"Name events");
351 EventDetails.Where(ev => ev.PropertyName ==
"Name").ToList().ForEach(x => Debug.WriteLine(x.ToString()));
355 private void CheckBottomDepth(List<EventDetail> list,
int depth)
357 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.OnPropertyChanged && ev.PropertyName ==
"Name"));
358 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"Name"));
359 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsDirty"));
360 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsSelfDirty"));
361 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsValid"));
362 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsSelfValid"));
363 Assert.AreEqual(2, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsSavable"));
367 private void CheckBottomDepthList(List<EventDetail> list,
int depth)
369 CheckBottomDepth(list, depth);
370 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBOList && ev.Depth == depth && ev.Event ==
enumEvent.OnChildChanged && ev.PropertyName ==
"Name"));
371 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBOList && ev.Depth == depth && ev.Event ==
enumEvent.ChildChanged && ev.PropertyName ==
"Name"));
374 private void CheckMidDepth(List<EventDetail> list,
int depth)
376 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.OnChildChanged && ev.PropertyName ==
"Name"));
377 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsDirty"));
378 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsValid"));
379 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.PropertyChanged && ev.PropertyName ==
"IsSavable"));
380 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBO && ev.Depth == depth && ev.Event ==
enumEvent.ChildChanged && ev.PropertyName ==
"Name"));
383 private void CheckMidDepthList(List<EventDetail> list,
int depth)
385 CheckMidDepth(list, depth);
386 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBOList && ev.Depth == depth && ev.Event ==
enumEvent.OnChildChanged && ev.PropertyName ==
"Name"));
387 Assert.AreEqual(1, list.RemoveAll(ev => ev.BO ==
enumBO.SimpleBOList && ev.Depth == depth && ev.Event ==
enumEvent.ChildChanged && ev.PropertyName ==
"Name"));
This is the base class from which most business objects will be derived.
This is the base class from which most business collections or lists will be derived.
Contains event data about the changed child object.
PropertyChangedEventArgs PropertyChangedArgs
Gets the PropertyChangedEventArgs object from the child's PropertyChanged event, if the child is not ...
Maintains metadata about a property.
Business rule for a required string.
override void OnChildChanged(ChildChangedEventArgs e)
override void AddBusinessRules()
static readonly PropertyInfo< string > NameProperty
static readonly PropertyInfo< SimpleBO > ChildProperty
override void OnPropertyChanged(string propertyName)
static readonly PropertyInfo< SimpleBOList > ChildListProperty
override void OnChildChanged(ChildChangedEventArgs e)
override void OnPropertyChanged(PropertyChangedEventArgs e)
void OptimizeChildChangedTests_Fetch()
void OptimizeChildChangedTests_List_Name_Depth1()
void OptimizeChildChangedTests_Name_Depth3()
static int NextSequenceID
void OptimizeChildChangedTests_List_Name_Depth2()
static int NextUniqueID()
void OptimizeChildChangedTests_Name_Depth1()
void OptimizeChildChangedTests_Name_Depth2()
static List< EventDetail > EventDetails
void OptimizeChildChangedTests_Name_Depth0()
static void ClassInitialize(TestContext context)
void OptimizeChildChangedTests_List_Name_Depth3()
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
Interface defining the members of the data portal type.
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
@ Serializable
Prevents updating or inserting until the transaction is complete.
override string ToString()