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.
TransactionalRoot.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="TransactionalRoot.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.Text;
11using System.Data;
12using System.Data.SqlClient;
13using System.Configuration;
14
16{
17 [Serializable()]
18 public class TransactionalRoot : BusinessBase<TransactionalRoot>
19 {
20 #region "Business methods"
21
22 //get the configurationmanager to work right
23 //public static string CONNECTION_STRING = WellKnownValues.ConnectionStrings.DataPortalTestDatabase;
24
25 public static PropertyInfo<int> IDProperty = RegisterProperty<int>(c => c.ID);
26 public int ID
27 {
28 get { return GetProperty(IDProperty); }
29 private set { LoadProperty(IDProperty, value); }
30 }
31
32 public static PropertyInfo<string> FirstNameProperty = RegisterProperty<string>(c => c.FirstName);
33 public string FirstName
34 {
35 get { return GetProperty(FirstNameProperty); }
36 set { SetProperty(FirstNameProperty, value); }
37 }
38
39 public static PropertyInfo<string> LastNameProperty = RegisterProperty<string>(c => c.LastName);
40 public string LastName
41 {
42 get { return GetProperty(LastNameProperty); }
43 set { SetProperty(LastNameProperty, value); }
44 }
45
46 public static PropertyInfo<string> SmallColumnProperty = RegisterProperty<string>(c => c.SmallColumn);
47 public string SmallColumn
48 {
49 get { return GetProperty(SmallColumnProperty); }
50 set { SetProperty(SmallColumnProperty, value); }
51 }
52
53 #endregion
54
55 protected override void AddBusinessRules()
56 {
57 //normally, we would add a rule that prevents SmallColumn from being too long
58 //but to easily test the transactional functionality of the server-side dataportal
59 //we are going to allow strings that are longer than what the database allows
60 }
61
62 #region "Factory Methods"
63
65 {
66 return dataPortal.Create();
67 }
68
70 {
71 return dataPortal.Fetch(new Criteria(ID));
72 }
73
74 public static void DeleteTransactionalRoot(int ID, IDataPortal<TransactionalRoot> dataPortal)
75 {
76 dataPortal.Delete(new Criteria(ID));
77 }
78
79 #endregion
80
81 #region "Criteria"
82
83 [Serializable()]
84 private class Criteria
85 {
86 public int _id;
87
88 public Criteria(int id)
89 {
90 this._id = id;
91 }
92 }
93
94 #endregion
95
96 #region "Data Access"
97
98 [RunLocal()]
99 [Create]
100 protected void DataPortal_Create()
101 {
103 TestResults.Add("TransactionalRoot", "Created");
104 BusinessRules.CheckRules();
105 Console.WriteLine("DataPortal_Create");
106 }
107
108 protected void DataPortal_Fetch(object criteria)
109 {
110 Criteria crit = (Criteria)(criteria);
111
112 if (crit._id == 13)
113 {
114 throw new System.ApplicationException("DataPortal_Fetch: you chose an unlucky number");
115 }
116
117 Console.WriteLine("DataPortal_Fetch");
119 TestResults.Add("TransactionalRoot", "Fetched");
120 BusinessRules.CheckRules();
121 }
122
123 [Transactional(TransactionalTypes.TransactionScope)]
124 [Insert]
125 protected void DataPortal_Insert()
126 {
127 SqlConnection cn = new SqlConnection(WellKnownValues.DataPortalTestDatabase);
128 string firstName = this.FirstName;
129 string lastName = this.LastName;
130 string smallColumn = this.SmallColumn;
131
132 //this command will always execute successfully
133 //since it inserts a string less than 5 characters
134 //into SmallColumn
135 SqlCommand cm1 = new SqlCommand();
136 cm1.Connection = cn;
137 cm1.CommandText = "INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('Bill', 'Thompson', 'abc')";
138
139 //this command will throw an exception
140 //if SmallColumn is set to a string longer than
141 //5 characters
142 SqlCommand cm2 = new SqlCommand();
143 cm2.Connection = cn;
144 //use stringbuilder
145 cm2.CommandText = "INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('";
146 cm2.CommandText += firstName;
147 cm2.CommandText += "', '" + lastName + "', '" + smallColumn + "')";
148
149 cn.Open();
150 cm1.ExecuteNonQuery();
151 cm2.ExecuteNonQuery();
152 cn.Close();
153
155 TestResults.Add("TransactionalRoot", "Inserted");
156 Console.WriteLine("DataPortal_Insert");
157 }
158
159 [Transactional(TransactionalTypes.TransactionScope)]
160 [Update]
161 protected void DataPortal_Update()
162 {
163 Console.WriteLine("DataPortal_Update");
165 TestResults.Add("TransactionalRoot", "Updated");
166 }
167
168 [DeleteSelf]
169 protected void DataPortal_DeleteSelf()
170 {
171 Console.WriteLine("DataPortal_DeleteSelf");
173 TestResults.Add("TransactionalRoot", "Deleted Self");
174 }
175
176 [Delete]
177 protected void DataPortal_Delete(object criteria)
178 {
179 Criteria crit = (Criteria)(criteria);
180 if (crit._id == 13)
181 {
182 throw new System.ApplicationException("DataPortal_Delete: you chose an unlucky number");
183 }
184
185 Console.WriteLine("DataPortal_Delete");
187 TestResults.Add("TransactionRoot", "Deleted");
188 }
189
190 #endregion
191
192 #region "DataPortalException"
193
194 protected override void DataPortal_OnDataPortalException(DataPortalEventArgs e, Exception ex)
195 {
197 TestResults.Add("OnDataPortalException", "Called");
198 Console.WriteLine("OnDataPortalException called");
199 }
200
201 #endregion
202 }
203}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Provides information about the DataPortal call.
Maintains metadata about a property.
static PropertyInfo< int > IDProperty
static PropertyInfo< string > SmallColumnProperty
static TransactionalRoot NewTransactionalRoot(IDataPortal< TransactionalRoot > dataPortal)
static PropertyInfo< string > LastNameProperty
static PropertyInfo< string > FirstNameProperty
static void DeleteTransactionalRoot(int ID, IDataPortal< TransactionalRoot > dataPortal)
static TransactionalRoot GetTransactionalRoot(int ID, IDataPortal< TransactionalRoot > dataPortal)
override void DataPortal_OnDataPortalException(DataPortalEventArgs e, Exception ex)
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
Definition: TestResults.cs:21
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Definition: TestResults.cs:69
static void Add(string key, string value)
Add an item to the test results, to indicate an outcome of a particular operation
Definition: TestResults.cs:29
static string DataPortalTestDatabase
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
void Delete(params object[] criteria)
Called by a Shared (static in C#) method in the business class to cause immediate deletion of a speci...
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
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...
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.
@ Update
Update operation (includes insert, update and delete self).
@ Create
Create operation.
@ Delete
Delete operation.