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.
TransactionalDataPortal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="TransactionalDataPortal.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Implements the server-side Serviced </summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Threading.Tasks;
10using System.Transactions;
11
12namespace Csla.Server
13{
19 {
20 private readonly DataPortalBroker _portal;
21 private readonly TransactionalAttribute _transactionalAttribute;
22
30 public TransactionalDataPortal(DataPortalBroker dataPortalBroker, TransactionalAttribute transactionalAttribute)
31 {
32 _portal = dataPortalBroker;
33 _transactionalAttribute = transactionalAttribute;
34 }
35
55 public async Task<DataPortalResult> Create(
56 Type objectType, object criteria, DataPortalContext context, bool isSync)
57 {
58 DataPortalResult result;
59 using (TransactionScope tr = CreateTransactionScope())
60 {
61 result = await _portal.Create(objectType, criteria, context, isSync).ConfigureAwait(false);
62 tr.Complete();
63 }
64 return result;
65 }
66
67 private TransactionScope CreateTransactionScope()
68 {
69 return new TransactionScope(TransactionScopeOption.Required, GetTransactionOptions(), _transactionalAttribute.AsyncFlowOption);
70 }
71
72 private TransactionOptions GetTransactionOptions()
73 {
74 var option = new TransactionOptions
75 {
76 IsolationLevel = GetIsolationLevel(_transactionalAttribute.TransactionIsolationLevel),
77 Timeout = TimeSpan.FromSeconds(_transactionalAttribute.TimeoutInSeconds)
78 };
79 return option;
80 }
81
82 private IsolationLevel GetIsolationLevel(TransactionIsolationLevel transactionIsolationLevel)
83 {
84 switch (transactionIsolationLevel)
85 {
86 case TransactionIsolationLevel.Unspecified:
87 return IsolationLevel.Unspecified;
88 case TransactionIsolationLevel.Serializable:
89 return IsolationLevel.Serializable;
90 case TransactionIsolationLevel.RepeatableRead:
91 return IsolationLevel.RepeatableRead;
92 case TransactionIsolationLevel.ReadCommitted:
93 return IsolationLevel.ReadCommitted;
94 case TransactionIsolationLevel.ReadUncommitted:
95 return IsolationLevel.ReadUncommitted;
96 default:
97 return IsolationLevel.Unspecified;
98 }
99 }
100
117 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
118 {
119 DataPortalResult result;
120 using (TransactionScope tr = CreateTransactionScope())
121 {
122 result = await _portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false);
123 tr.Complete();
124 }
125 return result;
126 }
127
143 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
144 {
145 DataPortalResult result;
146 using (TransactionScope tr = CreateTransactionScope())
147 {
148 result = await _portal.Update(obj, context, isSync).ConfigureAwait(false);
149 tr.Complete();
150 }
151 return result;
152 }
153
169 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
170 {
171 DataPortalResult result;
172 using (TransactionScope tr = CreateTransactionScope())
173 {
174 result = await _portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);
175 tr.Complete();
176 }
177 return result;
178 }
179 }
180}
Allows the Data Portal call to be intercepted by a custom IDataPortalServer implementation.
Provides consistent context information between the client and server DataPortal objects.
Returns data from the server-side DataPortal to the client-side DataPortal.
Implements the server-side Serviced DataPortal described in Chapter 4.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by the client-side DataPortal to delete an object.
TransactionalDataPortal(DataPortalBroker dataPortalBroker, TransactionalAttribute transactionalAttribute)
Initializes a new instance of the TransactionalDataPortal class.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Called by the client-side DataProtal to retrieve an object.
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Wraps a Create call in a TransactionScope
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Called by the client-side DataPortal to update an object.
Marks a DataPortal_XYZ method to run within the specified transactional context.
Interface implemented by server-side data portal components.
@ TransactionScope
Causes the server-side DataPortal to use System.Transactions TransactionScope style transactions.
TransactionIsolationLevel
Specifies an isolation level for transactions controlled by TransactionalAttribute