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.
Controller.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Controller.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides methods that respond to HTTP requests</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12#if NETSTANDARD2_0 || NET5_0_OR_GREATER || NETCOREAPP3_1
13using System.Threading.Tasks;
14using Csla.Core;
15using Csla.Rules;
16using Microsoft.AspNetCore.Mvc;
17#else
18using System.Web.Mvc;
19#endif
20
21namespace Csla.Web.Mvc
22{
27#if NETSTANDARD2_0 || NET5_0_OR_GREATER || NETCOREAPP3_1
28 public class Controller : Microsoft.AspNetCore.Mvc.Controller
29#else
30 public class Controller : System.Web.Mvc.Controller
31#endif
32 {
37 public Controller(ApplicationContext applicationContext)
38 {
39 ApplicationContext = applicationContext;
40 }
41
45 protected ApplicationContext ApplicationContext { get; private set; }
46
47#if NETSTANDARD2_0 || NET5_0_OR_GREATER || NETCOREAPP3_1
57 protected async Task<bool> SaveObjectAsync<T>(T item, bool forceUpdate)
58 where T : class, Core.ISavable
59 {
60 return await SaveObjectAsync(item, null, forceUpdate);
61 }
62
73 protected virtual async Task<bool> SaveObjectAsync<T>(T item, Action<T> updateModel, bool forceUpdate)
74 where T : class, Core.ISavable
75 {
76 try
77 {
78 ViewData.Model = item;
79 updateModel?.Invoke(item);
80 if (item is BusinessBase bb && !bb.IsValid)
81 {
82 AddBrokenRuleInfo(item, null);
83 return false;
84 }
85 ViewData.Model = await item.SaveAsync(forceUpdate);
86 return true;
87 }
88 catch (ValidationException ex)
89 {
90 AddBrokenRuleInfo(item, ex.Message);
91 return false;
92 }
93 catch (DataPortalException ex)
94 {
95 if (ex.BusinessException != null)
96 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
97 else
98 ModelState.AddModelError(string.Empty, ex.Message);
99 return false;
100 }
101 catch (Exception ex)
102 {
103 ModelState.AddModelError(string.Empty, ex.Message);
104 return false;
105 }
106 }
107
108 private void AddBrokenRuleInfo<T>(T item, string defaultText) where T : class, ISavable
109 {
110 if (item is BusinessBase bb)
111 {
112 var errors = bb.BrokenRulesCollection.
113 Where(r => r.Severity == RuleSeverity.Error);
114 foreach (var rule in errors)
115 {
116 if (string.IsNullOrEmpty(rule.Property))
117 ModelState.AddModelError(string.Empty, rule.Description);
118 else
119 ModelState.AddModelError(rule.Property, rule.Description);
120 }
121 }
122 else
123 {
124 ModelState.AddModelError(string.Empty, defaultText);
125 }
126 }
127#else
137 protected bool SaveObject<T>(T item, bool forceUpdate)
138 where T : class, Core.ISavable
139 {
140 return SaveObject(item,
141 null,
142 forceUpdate);
143 }
144
155 protected virtual bool SaveObject<T>(T item, Action<T> updateModel, bool forceUpdate)
156 where T : class, Core.ISavable
157 {
158 try
159 {
160 ViewData.Model = item;
161 updateModel?.Invoke(item);
162#if NETSTANDARD1_6
163 ViewData.Model = item.SaveAsync(forceUpdate).Result;
164#else
165 ViewData.Model = item.Save(forceUpdate);
166#endif
167 return true;
168 }
169 catch (DataPortalException ex)
170 {
171 if (ex.BusinessException != null)
172 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
173 else
174 ModelState.AddModelError(string.Empty, ex.Message);
175 return false;
176 }
177 catch (Exception ex)
178 {
179 ModelState.AddModelError(string.Empty, ex.Message);
180 return false;
181 }
182 }
183#endif
184
206 protected void LoadProperty<P>(object obj, PropertyInfo<P> propertyInfo, P newValue)
207 {
208 new ObjectManager(ApplicationContext).LoadProperty(obj, propertyInfo, newValue);
209 }
210
211 private class ObjectManager : Server.ObjectFactory
212 {
213 public ObjectManager(ApplicationContext applicationContext)
214 : base(applicationContext) { }
215
216 public new void LoadProperty<P>(object obj, PropertyInfo<P> propertyInfo, P newValue)
217 {
218 base.LoadProperty(obj, propertyInfo, newValue);
219 }
220 }
221 }
222}
Provides consistent context information between the client and server DataPortal objects.
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
virtual bool IsValid
Returns true if the object and its child objects are currently valid, false if the object or any of i...
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.
Provides methods that respond to HTTP requests in an ASP.NET MVC web site.
Definition: Controller.cs:32
Controller(ApplicationContext applicationContext)
Creates a new instance of the type.
Definition: Controller.cs:37
bool SaveObject< T >(T item, bool forceUpdate)
Performs a Save() operation on an editable business object, with appropriate validation and exception...
Definition: Controller.cs:137
void LoadProperty< P >(object obj, PropertyInfo< P > propertyInfo, P newValue)
Loads a property's managed field with the supplied value calling PropertyHasChanged if the value does...
Definition: Controller.cs:206
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16