CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
HasChildren.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="HasChildren.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.Collections.Generic;
10using System.Linq;
11using System.Text;
14using Csla.Core;
15using System.Runtime.Serialization;
16using System.Threading.Tasks;
17
19{
21 public class HasChildren : BusinessBase<HasChildren>
22 {
23 private static PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id, "Id");
24 public int Id
25 {
26 get { return GetProperty(IdProperty); }
27 set { SetProperty(IdProperty, value); }
28 }
29
30 private static PropertyInfo<ChildList> ChildListProperty = RegisterProperty<ChildList>(c => c.ChildList, "Child list", null, RelationshipTypes.LazyLoad);
32 {
33 get
34 {
35 return LazyGetProperty(ChildListProperty, () => GetDataPortal<ChildList>().Create());
36 }
37 }
38
39 protected override void AddBusinessRules()
40 {
41 BusinessRules.AddRule(new OneItem<HasChildren> { PrimaryProperty = ChildListProperty });
42 }
43
44 public class OneItem<T> : Rules.BusinessRule
45 where T : HasChildren
46 {
47 protected override void Execute(Rules.IRuleContext context)
48 {
49 var target = (T)context.Target;
50 if (target.ChildList.Count < 1)
51 context.AddErrorResult("At least one item required");
52 }
53 }
54
55 protected override void Initialize()
56 {
57 base.Initialize();
58 ChildList.ListChanged += new System.ComponentModel.ListChangedEventHandler(ChildList_ListChanged);
59 this.ChildChanged += new EventHandler<ChildChangedEventArgs>(HasChildren_ChildChanged);
60 }
61
62 protected override void OnDeserialized(StreamingContext context)
63 {
64 base.OnDeserialized(context);
65 ChildList.ListChanged += new System.ComponentModel.ListChangedEventHandler(ChildList_ListChanged);
66 this.ChildChanged += new EventHandler<ChildChangedEventArgs>(HasChildren_ChildChanged);
67 }
68
69 void ChildList_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
70 {
71 //ValidationRules.CheckRules(ChildListProperty);
72 }
73
74 void HasChildren_ChildChanged(object sender, ChildChangedEventArgs e)
75 {
76 BusinessRules.CheckRules(ChildListProperty);
77 }
78
79 [Create]
80 private async Task Create()
81 {
82 await BusinessRules.CheckRulesAsync();
83 }
84
85 #region Private Helper Methods
86
92 private IDataPortal<T> GetDataPortal<T>() where T : class
93 {
94 return ApplicationContext.GetRequiredService<IDataPortal<T>>();
95 }
96
97 #endregion
98
99 }
100}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Contains event data about the changed child object.
Maintains metadata about a property.
override void Execute(Rules.IRuleContext context)
Definition: HasChildren.cs:47
override void OnDeserialized(StreamingContext context)
Definition: HasChildren.cs:62
RelationshipTypes
List of valid relationship types between a parent object and another object through a managed propert...
@ Serializable
Prevents updating or inserting until the transaction is complete.