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.
DataPortalExceptionTest.cs
Go to the documentation of this file.
1using System;
2using System.Threading.Tasks;
3using Csla;
4using Csla.TestHelpers;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
8{
9 [TestClass]
11 {
12 private static TestDIContext _testDIContext;
13
15 public static void ClassInitialize(TestContext context)
16 {
17 _testDIContext = TestDIContextFactory.CreateDefaultContext();
18 }
19
20 [TestMethod]
21 [TestCategory("SkipWhenLiveUnitTesting")]
23 {
24 IDataPortal<EditableRoot1> dataPortal = _testDIContext.CreateDataPortal<EditableRoot1>();
25
26 try
27 {
28 var bo = dataPortal.Create();
29
30 bo.Save();
31 }
32 catch (DataPortalException e)
33 {
34 Assert.IsInstanceOfType(e.BusinessException, typeof(CustomException));
35 }
36 }
37
38 [TestMethod]
39 [TestCategory("SkipWhenLiveUnitTesting")]
41 {
42 IDataPortal<EditableRoot1> dataPortal = _testDIContext.CreateDataPortal<EditableRoot1>();
43 string expected = "A suitable constructor for type 'Csla.Test.DataPortal.EditableChild2' could not be located.";
44
45 try
46 {
47 var bo = await dataPortal.FetchAsync();
48 }
49 catch (DataPortalException e)
50 {
51 Assert.AreEqual(e.BusinessException.Message.Substring(0, expected.Length), expected);
52 }
53 }
54 }
55
57 public class EditableRoot1 : BusinessBase<EditableRoot1>
58 {
60 RegisterProperty<EditableChild1>(c => c.Child);
61
63 {
64 get { return GetProperty(ChildProperty); }
65 private set { LoadProperty(ChildProperty, value); }
66 }
67
68 [RunLocal]
69 [Create]
70 protected void DataPortal_Create([Inject] IChildDataPortal<EditableChild1> childDataPortal)
71 {
72 using (BypassPropertyChecks)
73 {
74 LoadProperty(ChildProperty, childDataPortal.CreateChild());
75 }
76 BusinessRules.CheckRules();
77 }
78
79 [Fetch]
80 protected async Task DataPortal_FetchAsync([Inject] IChildDataPortal<EditableChild2> childDataPortal)
81 {
82 using (BypassPropertyChecks)
83 {
84 LoadProperty(ChildProperty, await childDataPortal.FetchChildAsync(1));
85 }
86 BusinessRules.CheckRules();
87 }
88
89 [Transactional(TransactionalTypes.TransactionScope)]
90 [Insert]
91 protected void DataPortal_Insert()
92 {
93 using (BypassPropertyChecks)
94 {
95 FieldManager.UpdateChildren(this);
96 }
97 }
98 }
99
101 public class EditableChild1 : BusinessBase<EditableChild1>
102 {
103 #region Data Access
104
105 [CreateChild]
106 protected override void Child_Create()
107 {
108 using (BypassPropertyChecks)
109 {
110 }
111 base.Child_Create();
112 }
113
114 [FetchChild]
115 private Task Child_FetchAsync(int id)
116 {
117 using (BypassPropertyChecks)
118 {
119 throw new CustomException("Fetch not allowed");
120 }
121 }
122
123 [InsertChild]
124 private void Child_Insert(object parent)
125 {
126 using (BypassPropertyChecks)
127 {
128 throw new CustomException("Insert not allowed");
129 }
130 }
131
132 #endregion
133
134 }
135
138 {
139 private EditableChild2()
140 {
141 // Disallow creation of the object, to test what happens in this scenario
142 }
143
144 [Fetch]
145 private Task Child_FetchAsync(int id)
146 {
147 return Task.CompletedTask;
148 }
149 }
150
152 public class CustomException : Exception
153 {
154 public CustomException(string message) : base(message) { }
155 }
156}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Exception BusinessException
Gets the original server-side exception.
Maintains metadata about a property.
static void ClassInitialize(TestContext context)
void DataPortal_Create([Inject] IChildDataPortal< EditableChild1 > childDataPortal)
async Task DataPortal_FetchAsync([Inject] IChildDataPortal< EditableChild2 > childDataPortal)
static readonly PropertyInfo< EditableChild1 > ChildProperty
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.
Definition: IDataPortalT.cs:17
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Task< object > FetchAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.
TransactionalTypes
Provides a list of possible transactional technologies to be used by the server-side DataPortal.
@ Serializable
Prevents updating or inserting until the transaction is complete.
@ Fetch
Fetch operation.
@ Create
Create operation.